Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
import cv2
|
3 |
from gradio_webrtc import WebRTC
|
4 |
-
import mediapipe as mp
|
5 |
-
|
6 |
-
# 初始化 MediaPipe Hands
|
7 |
-
mp_hands = mp.solutions.hands
|
8 |
-
mp_drawing = mp.solutions.drawing_utils
|
9 |
-
hands = mp_hands.Hands(min_detection_confidence=0.3, min_tracking_confidence=0.3) # 降低置信度提升速度
|
10 |
|
11 |
# WebRTC 配置
|
12 |
rtc_configuration = {
|
@@ -14,30 +7,6 @@ rtc_configuration = {
|
|
14 |
"iceTransportPolicy": "relay"
|
15 |
}
|
16 |
|
17 |
-
# 手势检测函数
|
18 |
-
def detection(image, conf_threshold=0.5):
|
19 |
-
"""
|
20 |
-
使用 MediaPipe Hands 进行手势检测。
|
21 |
-
"""
|
22 |
-
# 将图像从 BGR 转换为 RGB(MediaPipe 需要 RGB 格式)
|
23 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
24 |
-
|
25 |
-
# 将图像大小缩小到一个较小的尺寸,降低计算负担
|
26 |
-
image = cv2.resize(image, (640, 480))
|
27 |
-
|
28 |
-
# 使用 MediaPipe Hands 处理图像
|
29 |
-
results = hands.process(image_rgb)
|
30 |
-
|
31 |
-
# 如果检测到手,绘制手部关键点
|
32 |
-
if results.multi_hand_landmarks:
|
33 |
-
for hand_landmarks in results.multi_hand_landmarks:
|
34 |
-
mp_drawing.draw_landmarks(
|
35 |
-
image, hand_landmarks, mp_hands.HAND_CONNECTIONS
|
36 |
-
)
|
37 |
-
|
38 |
-
# 返回带注释的图像
|
39 |
-
return image
|
40 |
-
|
41 |
# Gradio 界面
|
42 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
43 |
.my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
|
@@ -46,31 +15,22 @@ with gr.Blocks(css=css) as demo:
|
|
46 |
gr.HTML(
|
47 |
"""
|
48 |
<h1 style='text-align: center'>
|
49 |
-
|
50 |
</h1>
|
51 |
"""
|
52 |
)
|
53 |
gr.HTML(
|
54 |
"""
|
55 |
<h3 style='text-align: center'>
|
56 |
-
|
57 |
</h3>
|
58 |
"""
|
59 |
)
|
60 |
with gr.Column(elem_classes=["my-column"]):
|
61 |
with gr.Group(elem_classes=["my-group"]):
|
62 |
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
63 |
-
conf_threshold = gr.Slider(
|
64 |
-
label="Confidence Threshold",
|
65 |
-
minimum=0.0,
|
66 |
-
maximum=1.0,
|
67 |
-
step=0.05,
|
68 |
-
value=0.5,
|
69 |
-
)
|
70 |
|
71 |
-
image.stream(
|
72 |
-
fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
|
73 |
-
)
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from gradio_webrtc import WebRTC
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# WebRTC 配置
|
5 |
rtc_configuration = {
|
|
|
7 |
"iceTransportPolicy": "relay"
|
8 |
}
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Gradio 界面
|
11 |
css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
|
12 |
.my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
|
|
|
15 |
gr.HTML(
|
16 |
"""
|
17 |
<h1 style='text-align: center'>
|
18 |
+
WebRTC Stream Test
|
19 |
</h1>
|
20 |
"""
|
21 |
)
|
22 |
gr.HTML(
|
23 |
"""
|
24 |
<h3 style='text-align: center'>
|
25 |
+
WebRTC without MediaPipe
|
26 |
</h3>
|
27 |
"""
|
28 |
)
|
29 |
with gr.Column(elem_classes=["my-column"]):
|
30 |
with gr.Group(elem_classes=["my-group"]):
|
31 |
image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
image.stream(fn=lambda x: x, inputs=[image], outputs=[image], time_limit=10)
|
|
|
|
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
demo.launch()
|