simar007 commited on
Commit
fc3a16a
·
verified ·
1 Parent(s): 2f76456

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -13
app.py CHANGED
@@ -14,24 +14,16 @@ def classify_gesture(landmarks):
14
 
15
  thumb_tip = landmarks[4]
16
  index_tip = landmarks[8]
17
- middle_tip = landmarks[12]
18
- ring_tip = landmarks[16]
19
- pinky_tip = landmarks[20]
20
- wrist = landmarks[0]
21
-
22
- def dist(a, b):
23
- return ((a.x - b.x)**2 + (a.y - b.y)**2)**0.5
24
 
25
  if thumb_tip.y < index_tip.y:
26
  return "A (Thumb Up)"
27
- if index_tip.y < wrist.y and middle_tip.y < wrist.y and ring_tip.y < wrist.y and pinky_tip.y < wrist.y:
28
- return "B (Flat Hand)"
29
  return "Unknown"
30
 
31
  def process_frame(frame):
32
  frame = cv2.flip(frame, 1)
33
  rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
34
  result = hands.process(rgb)
 
35
  gesture = "No hand detected"
36
 
37
  if result.multi_hand_landmarks:
@@ -42,14 +34,13 @@ def process_frame(frame):
42
  cv2.putText(frame, f"Sign: {gesture}", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
43
  return frame
44
 
45
- # Gradio 5.x interface: Use Video input for live webcam
46
  demo = gr.Interface(
47
  fn=process_frame,
48
- inputs=gr.Video(source="webcam"), # webcam live input
49
  outputs=gr.Image(type="numpy"),
50
- live=True,
51
  title="Sign Language Recognition",
52
- description="Live sign recognition using MediaPipe + OpenCV"
53
  )
54
 
55
  demo.launch()
 
14
 
15
  thumb_tip = landmarks[4]
16
  index_tip = landmarks[8]
 
 
 
 
 
 
 
17
 
18
  if thumb_tip.y < index_tip.y:
19
  return "A (Thumb Up)"
 
 
20
  return "Unknown"
21
 
22
  def process_frame(frame):
23
  frame = cv2.flip(frame, 1)
24
  rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
  result = hands.process(rgb)
26
+
27
  gesture = "No hand detected"
28
 
29
  if result.multi_hand_landmarks:
 
34
  cv2.putText(frame, f"Sign: {gesture}", (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
35
  return frame
36
 
37
+ # Gradio interface (compatible with old Gradio versions on Spaces)
38
  demo = gr.Interface(
39
  fn=process_frame,
40
+ inputs=gr.Image(type="numpy", tool="editor"), # webcam snapshot button works here
41
  outputs=gr.Image(type="numpy"),
 
42
  title="Sign Language Recognition",
43
+ description="Take a webcam snapshot or upload an image to detect hand signs"
44
  )
45
 
46
  demo.launch()