Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
|
|
3 |
from gradio_webrtc import WebRTC
|
4 |
-
|
5 |
-
import
|
|
|
6 |
import spaces
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
# else:
|
12 |
-
# class spaces:
|
13 |
-
# @staticmethod
|
14 |
-
# def GPU(func):
|
15 |
-
# def wrapper(*args, **kwargs):
|
16 |
-
# return func(*args, **kwargs)
|
17 |
-
# return wrapper
|
18 |
|
19 |
-
|
20 |
-
# def fake_gpu():
|
21 |
-
# pass
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
mp_drawing = mp.solutions.drawing_utils
|
26 |
-
hands = mp_hands.Hands(min_detection_confidence=0.3, min_tracking_confidence=0.3) # 降低置信度提升速度
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
"iceServers": [{"urls": "stun:stun.l.google.com:19302"}],
|
31 |
-
"iceTransportPolicy": "relay"
|
32 |
-
}
|
33 |
|
34 |
-
|
35 |
-
last_process_time = time.time()
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
global last_process_time
|
44 |
-
current_time = time.time()
|
45 |
-
|
46 |
-
# 只每隔一定时间(比如0.1秒)才进行一次处理,减少计算负担
|
47 |
-
if current_time - last_process_time < 0.1:
|
48 |
-
return image # 如果时间间隔太短,则直接返回原图像
|
49 |
-
|
50 |
-
last_process_time = current_time
|
51 |
-
|
52 |
-
# 将图像从 BGR 转换为 RGB(MediaPipe 需要 RGB 格式)
|
53 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
54 |
-
|
55 |
-
# 将图像大小缩小到一个较小的尺寸,降低计算负担
|
56 |
-
image = cv2.resize(image, (640, 480))
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
for hand_landmarks in results.multi_hand_landmarks:
|
64 |
-
mp_drawing.draw_landmarks(
|
65 |
-
image, hand_landmarks, mp_hands.HAND_CONNECTIONS
|
66 |
-
)
|
67 |
|
68 |
-
# 返回带注释的图像
|
69 |
-
return image
|
70 |
|
71 |
-
# Gradio 界面
|
72 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
73 |
-
|
|
|
74 |
|
75 |
with gr.Blocks(css=css) as demo:
|
76 |
gr.HTML(
|
77 |
"""
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
)
|
83 |
gr.HTML(
|
84 |
"""
|
85 |
<h3 style='text-align: center'>
|
86 |
-
<a href='https://
|
87 |
</h3>
|
88 |
"""
|
89 |
)
|
@@ -95,11 +62,12 @@ with gr.Blocks(css=css) as demo:
|
|
95 |
minimum=0.0,
|
96 |
maximum=1.0,
|
97 |
step=0.05,
|
98 |
-
value=0.
|
99 |
)
|
100 |
|
101 |
-
|
102 |
-
|
|
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
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 |
import spaces
|
9 |
|
10 |
+
model_file = hf_hub_download(
|
11 |
+
repo_id="onnx-community/yolov10n", filename="onnx/model.onnx"
|
12 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
model = YOLOv10(model_file)
|
|
|
|
|
15 |
|
16 |
+
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
|
17 |
+
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
|
|
|
|
|
18 |
|
19 |
+
if account_sid and auth_token:
|
20 |
+
client = Client(account_sid, auth_token)
|
|
|
|
|
|
|
21 |
|
22 |
+
token = client.tokens.create()
|
|
|
23 |
|
24 |
+
rtc_configuration = {
|
25 |
+
"iceServers": token.ice_servers,
|
26 |
+
"iceTransportPolicy": "relay",
|
27 |
+
}
|
28 |
+
else:
|
29 |
+
rtc_configuration = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
@spaces.GPU
|
32 |
+
def detection(image, conf_threshold=0.3):
|
33 |
+
image = cv2.resize(image, (model.input_width, model.input_height))
|
34 |
+
new_image = model.detect_objects(image, conf_threshold)
|
35 |
+
return cv2.resize(new_image, (500, 500))
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
37 |
|
|
|
38 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
39 |
+
.my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
|
40 |
+
|
41 |
|
42 |
with gr.Blocks(css=css) as demo:
|
43 |
gr.HTML(
|
44 |
"""
|
45 |
+
<h1 style='text-align: center'>
|
46 |
+
YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
|
47 |
+
</h1>
|
48 |
+
"""
|
49 |
)
|
50 |
gr.HTML(
|
51 |
"""
|
52 |
<h3 style='text-align: center'>
|
53 |
+
<a href='https://arxiv.org/abs/2405.14458' target='_blank'>arXiv</a> | <a href='https://github.com/THU-MIG/yolov10' target='_blank'>github</a>
|
54 |
</h3>
|
55 |
"""
|
56 |
)
|
|
|
62 |
minimum=0.0,
|
63 |
maximum=1.0,
|
64 |
step=0.05,
|
65 |
+
value=0.30,
|
66 |
)
|
67 |
|
68 |
+
image.stream(
|
69 |
+
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
70 |
+
)
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch()
|