Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +4 -4
- app.py +97 -0
- gesture_model1.pth +3 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.14.0
|
| 8 |
-
python_version: '3.
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Sample
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.14.0
|
| 8 |
+
python_version: '3.11'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# ----------------------------
|
| 9 |
+
# Labels
|
| 10 |
+
# ----------------------------
|
| 11 |
+
GESTURE_LABELS = {
|
| 12 |
+
0: "A", 1: "B", 2: "L", 3: "U", 4: "V", 5: "W",
|
| 13 |
+
6: "Z", 7: "F", 8: "five", 9: "one", 10: "three",
|
| 14 |
+
11: "two", 12: "six", 13: "seven", 14: "eight",
|
| 15 |
+
15: "nine", 16: "ten", 17: "E", 18: "four",
|
| 16 |
+
19: "i", 20: "k", 21: "r", 22: "zero",
|
| 17 |
+
23: "m", 24: "s"
|
| 18 |
+
}
|
| 19 |
+
CONF_THRESHOLD = 0.6
|
| 20 |
+
|
| 21 |
+
# ----------------------------
|
| 22 |
+
# Model
|
| 23 |
+
# ----------------------------
|
| 24 |
+
class GestureNet(nn.Module):
|
| 25 |
+
def __init__(self, input_size=126, num_classes=len(GESTURE_LABELS)):
|
| 26 |
+
super().__init__()
|
| 27 |
+
self.fc1 = nn.Linear(input_size, 256)
|
| 28 |
+
self.fc2 = nn.Linear(256, 128)
|
| 29 |
+
self.fc3 = nn.Linear(128, num_classes)
|
| 30 |
+
self.relu = nn.ReLU()
|
| 31 |
+
self.dropout = nn.Dropout(0.3)
|
| 32 |
+
|
| 33 |
+
def forward(self, x):
|
| 34 |
+
x = self.relu(self.fc1(x))
|
| 35 |
+
x = self.dropout(x)
|
| 36 |
+
x = self.relu(self.fc2(x))
|
| 37 |
+
x = self.dropout(x)
|
| 38 |
+
return self.fc3(x)
|
| 39 |
+
|
| 40 |
+
model = GestureNet()
|
| 41 |
+
model.load_state_dict(torch.load("gesture_model1.pth", map_location="cpu"))
|
| 42 |
+
model.eval()
|
| 43 |
+
|
| 44 |
+
# ----------------------------
|
| 45 |
+
# MediaPipe
|
| 46 |
+
# ----------------------------
|
| 47 |
+
mp_hands = mp.solutions.hands
|
| 48 |
+
|
| 49 |
+
# ----------------------------
|
| 50 |
+
# Predict
|
| 51 |
+
# ----------------------------
|
| 52 |
+
def predict(image):
|
| 53 |
+
# Gradio sends RGB, convert to BGR for OpenCV then back to RGB for MediaPipe
|
| 54 |
+
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 55 |
+
image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
|
| 56 |
+
|
| 57 |
+
with mp_hands.Hands(static_image_mode=True, max_num_hands=2) as hands:
|
| 58 |
+
results = hands.process(image_rgb)
|
| 59 |
+
|
| 60 |
+
coords = []
|
| 61 |
+
if results.multi_hand_landmarks:
|
| 62 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
| 63 |
+
hand_coords = np.array([[lm.x, lm.y, lm.z] for lm in hand_landmarks.landmark])
|
| 64 |
+
hand_coords -= hand_coords[0]
|
| 65 |
+
max_val = np.max(np.linalg.norm(hand_coords, axis=1))
|
| 66 |
+
if max_val > 0:
|
| 67 |
+
hand_coords /= max_val
|
| 68 |
+
coords.extend(hand_coords.flatten())
|
| 69 |
+
|
| 70 |
+
if len(coords) < 126:
|
| 71 |
+
coords.extend([0.0] * (126 - len(coords)))
|
| 72 |
+
elif len(coords) > 126:
|
| 73 |
+
coords = coords[:126]
|
| 74 |
+
|
| 75 |
+
if len(coords) == 126:
|
| 76 |
+
input_tensor = torch.tensor(coords, dtype=torch.float32).unsqueeze(0)
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
outputs = model(input_tensor)
|
| 79 |
+
probs = torch.softmax(outputs, dim=1)
|
| 80 |
+
pred_class = torch.argmax(probs, dim=1).item()
|
| 81 |
+
confidence = probs[0][pred_class].item()
|
| 82 |
+
if confidence >= CONF_THRESHOLD:
|
| 83 |
+
return f"{GESTURE_LABELS[pred_class]} ({confidence*100:.2f}%)"
|
| 84 |
+
|
| 85 |
+
return "Unknown"
|
| 86 |
+
|
| 87 |
+
# ----------------------------
|
| 88 |
+
# Gradio UI
|
| 89 |
+
# ----------------------------
|
| 90 |
+
app = gr.Interface(
|
| 91 |
+
fn=predict,
|
| 92 |
+
inputs=gr.Image(type="numpy"),
|
| 93 |
+
outputs="text",
|
| 94 |
+
title="Hand Gesture Recognition",
|
| 95 |
+
description="Upload an image of a hand gesture to recognize it."
|
| 96 |
+
)
|
| 97 |
+
app.launch()
|
gesture_model1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:80cd2891b17a30bfdb94376a2d222f51b7ec1b234643af2e6f19643d49bc02ff
|
| 3 |
+
size 277609
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 2 |
+
torch==2.2.2+cpu
|
| 3 |
+
torchvision==0.17.2+cpu
|
| 4 |
+
mediapipe==0.10.13
|
| 5 |
+
opencv-python-headless==4.9.0.80
|
| 6 |
+
numpy<2
|