liuhui0401 commited on
Commit
343586f
·
verified ·
1 Parent(s): bc62d72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -22
app.py CHANGED
@@ -1,36 +1,17 @@
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
  def detection(image, conf_threshold=0.5):
22
  """
23
  使用 MediaPipe Hands 进行手势检测。
24
  """
25
- global last_process_time
26
- current_time = time.time()
27
-
28
- # 只每隔一定时间(比如0.1秒)才进行一次处理,减少计算负载
29
- if current_time - last_process_time < 0.1:
30
- return image # 如果时间间隔太短,则直接返回原图像
31
-
32
- last_process_time = current_time
33
-
34
  # 将图像从 BGR 转换为 RGB(MediaPipe 需要 RGB 格式)
35
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
36
 
@@ -58,7 +39,7 @@ with gr.Blocks(css=css) as demo:
58
  gr.HTML(
59
  """
60
  <h1 style='text-align: center'>
61
- Hand Gesture Detection with MediaPipe (Powered by WebRTC ⚡️)
62
  </h1>
63
  """
64
  )
@@ -71,7 +52,7 @@ with gr.Blocks(css=css) as demo:
71
  )
72
  with gr.Column(elem_classes=["my-column"]):
73
  with gr.Group(elem_classes=["my-group"]):
74
- image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
75
  conf_threshold = gr.Slider(
76
  label="Confidence Threshold",
77
  minimum=0.0,
 
1
  import gradio as gr
2
  import cv2
 
3
  import mediapipe as mp
 
4
 
5
  # 初始化 MediaPipe Hands
6
  mp_hands = mp.solutions.hands
7
  mp_drawing = mp.solutions.drawing_utils
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
 
 
39
  gr.HTML(
40
  """
41
  <h1 style='text-align: center'>
42
+ Hand Gesture Detection with MediaPipe
43
  </h1>
44
  """
45
  )
 
52
  )
53
  with gr.Column(elem_classes=["my-column"]):
54
  with gr.Group(elem_classes=["my-group"]):
55
+ image = gr.Image(label="Upload Image") # 使用图像上传代替 WebRTC 流
56
  conf_threshold = gr.Slider(
57
  label="Confidence Threshold",
58
  minimum=0.0,