Mathematicaljuice commited on
Commit
e0e981c
·
verified ·
1 Parent(s): fafad68

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
20
+ CONF_THRESHOLD = 0.6
21
+
22
+ # ----------------------------
23
+ # Model
24
+ # ----------------------------
25
+ class GestureNet(nn.Module):
26
+ def __init__(self, input_size=126, num_classes=len(GESTURE_LABELS)):
27
+ super().__init__()
28
+ self.fc1 = nn.Linear(input_size, 256)
29
+ self.fc2 = nn.Linear(256, 128)
30
+ self.fc3 = nn.Linear(128, num_classes)
31
+ self.relu = nn.ReLU()
32
+ self.dropout = nn.Dropout(0.3)
33
+
34
+ def forward(self, x):
35
+ x = self.relu(self.fc1(x))
36
+ x = self.dropout(x)
37
+ x = self.relu(self.fc2(x))
38
+ x = self.dropout(x)
39
+ return self.fc3(x)
40
+
41
+ model = GestureNet()
42
+ model.load_state_dict(torch.load("gesture_model1.pth", map_location="cpu"))
43
+ model.eval()
44
+
45
+ # ----------------------------
46
+ # MediaPipe
47
+ # ----------------------------
48
+ mp_hands = mp.solutions.hands
49
+
50
+ def predict(image):
51
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
52
+
53
+ with mp_hands.Hands(static_image_mode=True, max_num_hands=2) as hands:
54
+ results = hands.process(image)
55
+
56
+ coords = []
57
+
58
+ if results.multi_hand_landmarks:
59
+ for hand_landmarks in results.multi_hand_landmarks:
60
+ hand_coords = np.array([[lm.x, lm.y, lm.z] for lm in hand_landmarks.landmark])
61
+
62
+ hand_coords -= hand_coords[0]
63
+ max_val = np.max(np.linalg.norm(hand_coords, axis=1))
64
+ if max_val > 0:
65
+ hand_coords /= max_val
66
+
67
+ coords.extend(hand_coords.flatten())
68
+
69
+ if len(coords) < 126:
70
+ coords.extend([0.0] * (126 - len(coords)))
71
+ elif len(coords) > 126:
72
+ coords = coords[:126]
73
+
74
+ if len(coords) == 126:
75
+ input_tensor = torch.tensor(coords, dtype=torch.float32).unsqueeze(0)
76
+
77
+ with torch.no_grad():
78
+ outputs = model(input_tensor)
79
+ probs = torch.softmax(outputs, dim=1).numpy()[0]
80
+
81
+ pred_class = np.argmax(probs)
82
+ confidence = probs[pred_class]
83
+
84
+ if confidence >= CONF_THRESHOLD:
85
+ return f"{GESTURE_LABELS[pred_class]} ({confidence*100:.2f}%)"
86
+
87
+ return "Unknown"
88
+
89
+ # ----------------------------
90
+ # Gradio UI
91
+ # ----------------------------
92
+ app = gr.Interface(
93
+ fn=predict,
94
+ inputs=gr.Image(type="numpy"),
95
+ outputs="text",
96
+ title="Hand Gesture Recognition",
97
+ description="Upload an image of a hand gesture"
98
+ )
99
+
100
+ app.launch()