liuhui0401 commited on
Commit
606d19f
·
verified ·
1 Parent(s): ef7efe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -35
app.py CHANGED
@@ -1,55 +1,56 @@
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,7 +62,7 @@ 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(
@@ -69,4 +70,4 @@ with gr.Blocks(css=css) as demo:
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
 
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.5, min_tracking_confidence=0.5)
10
 
11
+ # WebRTC 配置
12
+ rtc_configuration = {
13
+ "iceServers": [{"urls": "stun:stun.l.google.com:19302"}],
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
+ # 使用 MediaPipe Hands 处理图像
26
+ results = hands.process(image_rgb)
27
 
28
+ # 如果检测到手,绘制手部关键点
29
+ if results.multi_hand_landmarks:
30
+ for hand_landmarks in results.multi_hand_landmarks:
31
+ mp_drawing.draw_landmarks(
32
+ image, hand_landmarks, mp_hands.HAND_CONNECTIONS
33
+ )
34
 
35
+ # 返回带注释的图像
36
+ return image
37
 
38
+ # Gradio 界面
39
  css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
40
+ .my-column {display: flex !important; justify-content: center !important; align-items: center !important;}"""
 
41
 
42
  with gr.Blocks(css=css) as demo:
43
  gr.HTML(
44
  """
45
+ <h1 style='text-align: center'>
46
+ Hand Gesture Detection with MediaPipe (Powered by WebRTC ⚡️)
47
+ </h1>
48
+ """
49
  )
50
  gr.HTML(
51
  """
52
  <h3 style='text-align: center'>
53
+ <a href='https://mediapipe.dev/'>MediaPipe Hands</a>
54
  </h3>
55
  """
56
  )
 
62
  minimum=0.0,
63
  maximum=1.0,
64
  step=0.05,
65
+ value=0.5,
66
  )
67
 
68
  image.stream(
 
70
  )
71
 
72
  if __name__ == "__main__":
73
+ demo.launch()