Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,63 @@ mp_hands = mp.solutions.hands
|
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def classify_gesture(landmarks):
|
| 13 |
-
if landmarks:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return "Unknown"
|
| 19 |
|
|
|
|
| 20 |
def process_frame(frame):
|
| 21 |
frame = cv2.flip(frame, 1)
|
| 22 |
h, w, _ = frame.shape
|
|
@@ -42,9 +90,10 @@ def process_frame(frame):
|
|
| 42 |
|
| 43 |
return frame
|
| 44 |
|
|
|
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=process_frame,
|
| 47 |
-
inputs=gr.
|
| 48 |
outputs=gr.Image(),
|
| 49 |
live=True,
|
| 50 |
title="Sign Language Recognition",
|
|
|
|
| 8 |
mp_drawing = mp.solutions.drawing_utils
|
| 9 |
hands = mp_hands.Hands(max_num_hands=1, min_detection_confidence=0.7)
|
| 10 |
|
| 11 |
+
# Gesture classifier with multiple gestures
|
| 12 |
def classify_gesture(landmarks):
|
| 13 |
+
if not landmarks:
|
| 14 |
+
return "Unknown"
|
| 15 |
+
|
| 16 |
+
# Extract needed landmarks
|
| 17 |
+
thumb_tip = landmarks[4]
|
| 18 |
+
index_tip = landmarks[8]
|
| 19 |
+
middle_tip = landmarks[12]
|
| 20 |
+
ring_tip = landmarks[16]
|
| 21 |
+
pinky_tip = landmarks[20]
|
| 22 |
+
wrist = landmarks[0]
|
| 23 |
+
|
| 24 |
+
# Helper distance function
|
| 25 |
+
def dist(a, b):
|
| 26 |
+
return ((a.x - b.x)**2 + (a.y - b.y)**2) ** 0.5
|
| 27 |
+
|
| 28 |
+
# --- Gesture A (Thumb Up) ---
|
| 29 |
+
if thumb_tip.y < index_tip.y and thumb_tip.y < middle_tip.y:
|
| 30 |
+
return "A (Thumb Up)"
|
| 31 |
+
|
| 32 |
+
# --- Gesture B (Flat Hand) ---
|
| 33 |
+
fingers_extended = (
|
| 34 |
+
index_tip.y < wrist.y and
|
| 35 |
+
middle_tip.y < wrist.y and
|
| 36 |
+
ring_tip.y < wrist.y and
|
| 37 |
+
pinky_tip.y < wrist.y
|
| 38 |
+
)
|
| 39 |
+
thumb_side = thumb_tip.x < wrist.x or thumb_tip.x > wrist.x
|
| 40 |
+
if fingers_extended and thumb_side:
|
| 41 |
+
return "B (Flat Hand)"
|
| 42 |
+
|
| 43 |
+
# --- Gesture C (Curved Hand) ---
|
| 44 |
+
curve = dist(index_tip, thumb_tip)
|
| 45 |
+
palm = dist(wrist, index_tip)
|
| 46 |
+
if 0.15 < curve < 0.3 and palm > 0.25:
|
| 47 |
+
return "C (Curved Hand)"
|
| 48 |
+
|
| 49 |
+
# --- OK Gesture ---
|
| 50 |
+
if dist(thumb_tip, index_tip) < 0.05:
|
| 51 |
+
return "OK"
|
| 52 |
+
|
| 53 |
+
# --- Peace Gesture ---
|
| 54 |
+
if (index_tip.y < wrist.y and middle_tip.y < wrist.y and
|
| 55 |
+
ring_tip.y > wrist.y and pinky_tip.y > wrist.y):
|
| 56 |
+
return "V / Peace"
|
| 57 |
+
|
| 58 |
+
# --- Fist Gesture ---
|
| 59 |
+
if (index_tip.y > wrist.y and
|
| 60 |
+
middle_tip.y > wrist.y and
|
| 61 |
+
ring_tip.y > wrist.y and
|
| 62 |
+
pinky_tip.y > wrist.y):
|
| 63 |
+
return "Fist"
|
| 64 |
+
|
| 65 |
return "Unknown"
|
| 66 |
|
| 67 |
+
# Process webcam frame
|
| 68 |
def process_frame(frame):
|
| 69 |
frame = cv2.flip(frame, 1)
|
| 70 |
h, w, _ = frame.shape
|
|
|
|
| 90 |
|
| 91 |
return frame
|
| 92 |
|
| 93 |
+
# Gradio interface (Gradio 3.x compatible)
|
| 94 |
demo = gr.Interface(
|
| 95 |
fn=process_frame,
|
| 96 |
+
inputs=gr.Image(type="numpy", source="webcam", streaming=True),
|
| 97 |
outputs=gr.Image(),
|
| 98 |
live=True,
|
| 99 |
title="Sign Language Recognition",
|