Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
hands = mp_hands.Hands(min_detection_confidence=0.3, min_tracking_confidence=0.3)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
def detection(image, conf_threshold=0.5):
|
| 12 |
-
"""
|
| 13 |
-
使用 MediaPipe Hands 进行手势检测。
|
| 14 |
-
"""
|
| 15 |
-
# 将图像从 BGR 转换为 RGB(MediaPipe 需要 RGB 格式)
|
| 16 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 如果检测到手,绘制手部关键点
|
| 25 |
-
if results.multi_hand_landmarks:
|
| 26 |
-
for hand_landmarks in results.multi_hand_landmarks:
|
| 27 |
-
mp_drawing.draw_landmarks(
|
| 28 |
-
image, hand_landmarks, mp_hands.HAND_CONNECTIONS
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Gradio 界面
|
| 35 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
with gr.Blocks(css=css) as demo:
|
| 39 |
gr.HTML(
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
)
|
| 46 |
gr.HTML(
|
| 47 |
"""
|
| 48 |
<h3 style='text-align: center'>
|
| 49 |
-
<a href='https://
|
| 50 |
</h3>
|
| 51 |
"""
|
| 52 |
)
|
| 53 |
with gr.Column(elem_classes=["my-column"]):
|
| 54 |
with gr.Group(elem_classes=["my-group"]):
|
| 55 |
-
image =
|
| 56 |
conf_threshold = gr.Slider(
|
| 57 |
label="Confidence Threshold",
|
| 58 |
minimum=0.0,
|
| 59 |
maximum=1.0,
|
| 60 |
step=0.05,
|
| 61 |
-
value=0.
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from gradio_webrtc import WebRTC
|
| 5 |
+
from twilio.rest import Client
|
| 6 |
+
import os
|
| 7 |
+
from inference import YOLOv10
|
| 8 |
|
| 9 |
+
model_file = hf_hub_download(
|
| 10 |
+
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
| 11 |
+
)
|
|
|
|
| 12 |
|
| 13 |
+
model = YOLOv10(model_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
| 16 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
| 17 |
|
| 18 |
+
if account_sid and auth_token:
|
| 19 |
+
client = Client(account_sid, auth_token)
|
| 20 |
+
|
| 21 |
+
token = client.tokens.create()
|
| 22 |
+
|
| 23 |
+
rtc_configuration = {
|
| 24 |
+
"iceServers": token.ice_servers,
|
| 25 |
+
"iceTransportPolicy": "relay",
|
| 26 |
+
}
|
| 27 |
+
else:
|
| 28 |
+
rtc_configuration = None
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def detection(image, conf_threshold=0.3):
|
| 32 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
| 33 |
+
new_image = model.detect_objects(image, conf_threshold)
|
| 34 |
+
return cv2.resize(new_image, (500, 500))
|
| 35 |
+
|
| 36 |
|
|
|
|
| 37 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
| 38 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
| 39 |
+
|
| 40 |
|
| 41 |
with gr.Blocks(css=css) as demo:
|
| 42 |
gr.HTML(
|
| 43 |
"""
|
| 44 |
+
<h1 style='text-align: center'>
|
| 45 |
+
YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
|
| 46 |
+
</h1>
|
| 47 |
+
"""
|
| 48 |
)
|
| 49 |
gr.HTML(
|
| 50 |
"""
|
| 51 |
<h3 style='text-align: center'>
|
| 52 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
| 53 |
</h3>
|
| 54 |
"""
|
| 55 |
)
|
| 56 |
with gr.Column(elem_classes=["my-column"]):
|
| 57 |
with gr.Group(elem_classes=["my-group"]):
|
| 58 |
+
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
| 59 |
conf_threshold = gr.Slider(
|
| 60 |
label="Confidence Threshold",
|
| 61 |
minimum=0.0,
|
| 62 |
maximum=1.0,
|
| 63 |
step=0.05,
|
| 64 |
+
value=0.30,
|
| 65 |
)
|
| 66 |
|
| 67 |
+
image.stream(
|
| 68 |
+
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
| 69 |
+
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
demo.launch()
|