liuhui0401 commited on
Commit
af5db15
·
verified ·
1 Parent(s): addb2d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -34
app.py CHANGED
@@ -1,55 +1,72 @@
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
  )
@@ -61,12 +78,11 @@ with gr.Blocks(css=css) as demo:
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()
 
1
  import gradio as gr
2
  import cv2
 
3
  from gradio_webrtc import WebRTC
4
+ import mediapipe as mp
5
+ import time
 
6
 
7
+ # 初始化 MediaPipe Hands
8
+ mp_hands = mp.solutions.hands
9
+ mp_drawing = mp.solutions.drawing_utils
10
+ hands = mp_hands.Hands(min_detection_confidence=0.3, min_tracking_confidence=0.3) # 降低置信度提升速度
11
 
12
+ # WebRTC 配置
13
+ rtc_configuration = {
14
+ "iceServers": [{"urls": "stun:stun.l.google.com:19302"}],
15
+ "iceTransportPolicy": "relay"
16
+ }
17
 
18
+ # 控制每秒帧处理频率的时间
19
+ last_process_time = time.time()
20
 
21
+ # 手势检测函数
22
+ def detection(image, conf_threshold=0.5):
23
+ """
24
+ 使用 MediaPipe Hands 进行手势检测。
25
+ """
26
+ global last_process_time
27
+ current_time = time.time()
28
+
29
+ # 只每隔一定时间(比如0.1秒)才进行一次处理,减少计算负担
30
+ if current_time - last_process_time < 0.1:
31
+ return image # 如果时间间隔太短,则直接返回原图像
32
 
33
+ last_process_time = current_time
34
 
35
+ # 将图像从 BGR 转换为 RGB(MediaPipe 需要 RGB 格式)
36
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
 
 
 
37
 
38
+ # 将图像大小缩小到一个较小的尺寸,降低计算负担
39
+ image = cv2.resize(image, (640, 480))
40
 
41
+ # 使用 MediaPipe Hands 处理图像
42
+ results = hands.process(image_rgb)
 
 
43
 
44
+ # 如果检测到手,绘制手部关键点
45
+ if results.multi_hand_landmarks:
46
+ for hand_landmarks in results.multi_hand_landmarks:
47
+ mp_drawing.draw_landmarks(
48
+ image, hand_landmarks, mp_hands.HAND_CONNECTIONS
49
+ )
50
 
51
+ # 返回带注释的图像
52
+ return image
53
 
54
+ # Gradio 界面
55
+ css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
56
+ .my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
57
 
58
  with gr.Blocks(css=css) as demo:
59
  gr.HTML(
60
  """
61
+ <h1 style='text-align: center'>
62
+ Hand Gesture Detection with MediaPipe (Powered by WebRTC ⚡️)
63
+ </h1>
64
+ """
65
  )
66
  gr.HTML(
67
  """
68
  <h3 style='text-align: center'>
69
+ <a href='https://mediapipe.dev/'>MediaPipe Hands</a>
70
  </h3>
71
  """
72
  )
 
78
  minimum=0.0,
79
  maximum=1.0,
80
  step=0.05,
81
+ value=0.5,
82
  )
83
 
84
+ # 使用简化的stream函数,不使用queue参数
85
+ image.stream(fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10)
 
86
 
87
  if __name__ == "__main__":
88
  demo.launch()