Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import cv2
|
| 2 |
import mediapipe as mp
|
| 3 |
-
from mediapipe.tasks.python import vision
|
| 4 |
-
from mediapipe.tasks.python import BaseOptions
|
| 5 |
import numpy as np
|
| 6 |
import torch
|
| 7 |
import torch.nn as nn
|
|
@@ -27,73 +25,101 @@ CONF_THRESHOLD = 0.6
|
|
| 27 |
class GestureNet(nn.Module):
|
| 28 |
def __init__(self, input_size=126, num_classes=len(GESTURE_LABELS)):
|
| 29 |
super().__init__()
|
|
|
|
| 30 |
self.fc1 = nn.Linear(input_size, 256)
|
| 31 |
self.fc2 = nn.Linear(256, 128)
|
| 32 |
self.fc3 = nn.Linear(128, num_classes)
|
|
|
|
| 33 |
self.relu = nn.ReLU()
|
| 34 |
self.dropout = nn.Dropout(0.3)
|
| 35 |
|
| 36 |
def forward(self, x):
|
| 37 |
x = self.relu(self.fc1(x))
|
| 38 |
x = self.dropout(x)
|
|
|
|
| 39 |
x = self.relu(self.fc2(x))
|
| 40 |
x = self.dropout(x)
|
|
|
|
| 41 |
return self.fc3(x)
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
model = GestureNet()
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
model.eval()
|
| 46 |
|
| 47 |
# ----------------------------
|
| 48 |
-
# MediaPipe
|
| 49 |
# ----------------------------
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
coords = []
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
hand_coords -= hand_coords[0]
|
|
|
|
| 71 |
max_val = np.max(np.linalg.norm(hand_coords, axis=1))
|
|
|
|
| 72 |
if max_val > 0:
|
| 73 |
hand_coords /= max_val
|
|
|
|
| 74 |
coords.extend(hand_coords.flatten())
|
| 75 |
|
| 76 |
-
#
|
| 77 |
if len(coords) < 126:
|
| 78 |
coords.extend([0.0] * (126 - len(coords)))
|
|
|
|
| 79 |
elif len(coords) > 126:
|
| 80 |
coords = coords[:126]
|
| 81 |
-
return coords
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
return "Unknown"
|
| 98 |
|
| 99 |
# ----------------------------
|
|
@@ -107,4 +133,7 @@ app = gr.Interface(
|
|
| 107 |
description="Upload an image of a hand gesture"
|
| 108 |
)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import mediapipe as mp
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
import torch.nn as nn
|
|
|
|
| 25 |
class GestureNet(nn.Module):
|
| 26 |
def __init__(self, input_size=126, num_classes=len(GESTURE_LABELS)):
|
| 27 |
super().__init__()
|
| 28 |
+
|
| 29 |
self.fc1 = nn.Linear(input_size, 256)
|
| 30 |
self.fc2 = nn.Linear(256, 128)
|
| 31 |
self.fc3 = nn.Linear(128, num_classes)
|
| 32 |
+
|
| 33 |
self.relu = nn.ReLU()
|
| 34 |
self.dropout = nn.Dropout(0.3)
|
| 35 |
|
| 36 |
def forward(self, x):
|
| 37 |
x = self.relu(self.fc1(x))
|
| 38 |
x = self.dropout(x)
|
| 39 |
+
|
| 40 |
x = self.relu(self.fc2(x))
|
| 41 |
x = self.dropout(x)
|
| 42 |
+
|
| 43 |
return self.fc3(x)
|
| 44 |
|
| 45 |
+
# ----------------------------
|
| 46 |
+
# Load Model
|
| 47 |
+
# ----------------------------
|
| 48 |
model = GestureNet()
|
| 49 |
+
|
| 50 |
+
model.load_state_dict(
|
| 51 |
+
torch.load("gesture_model1.pth", map_location=torch.device("cpu"))
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
model.eval()
|
| 55 |
|
| 56 |
# ----------------------------
|
| 57 |
+
# MediaPipe
|
| 58 |
# ----------------------------
|
| 59 |
+
mp_hands = mp.solutions.hands
|
| 60 |
+
|
| 61 |
+
hands = mp_hands.Hands(
|
| 62 |
+
static_image_mode=True,
|
| 63 |
+
max_num_hands=2,
|
| 64 |
+
min_detection_confidence=0.5
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
# ----------------------------
|
| 68 |
+
# Prediction Function
|
| 69 |
+
# ----------------------------
|
| 70 |
+
def predict(image):
|
| 71 |
+
|
| 72 |
+
if image is None:
|
| 73 |
+
return "No image uploaded"
|
| 74 |
+
|
| 75 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 76 |
+
|
| 77 |
+
results = hands.process(image_rgb)
|
| 78 |
+
|
| 79 |
coords = []
|
| 80 |
|
| 81 |
+
if results.multi_hand_landmarks:
|
| 82 |
+
|
| 83 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
| 84 |
+
|
| 85 |
+
hand_coords = np.array([
|
| 86 |
+
[lm.x, lm.y, lm.z]
|
| 87 |
+
for lm in hand_landmarks.landmark
|
| 88 |
+
])
|
| 89 |
+
|
| 90 |
+
# Normalize
|
| 91 |
hand_coords -= hand_coords[0]
|
| 92 |
+
|
| 93 |
max_val = np.max(np.linalg.norm(hand_coords, axis=1))
|
| 94 |
+
|
| 95 |
if max_val > 0:
|
| 96 |
hand_coords /= max_val
|
| 97 |
+
|
| 98 |
coords.extend(hand_coords.flatten())
|
| 99 |
|
| 100 |
+
# Padding
|
| 101 |
if len(coords) < 126:
|
| 102 |
coords.extend([0.0] * (126 - len(coords)))
|
| 103 |
+
|
| 104 |
elif len(coords) > 126:
|
| 105 |
coords = coords[:126]
|
|
|
|
| 106 |
|
| 107 |
+
input_tensor = torch.tensor(
|
| 108 |
+
coords,
|
| 109 |
+
dtype=torch.float32
|
| 110 |
+
).unsqueeze(0)
|
| 111 |
+
|
| 112 |
+
with torch.no_grad():
|
| 113 |
+
outputs = model(input_tensor)
|
| 114 |
+
|
| 115 |
+
probs = torch.softmax(outputs, dim=1).cpu().numpy()[0]
|
| 116 |
+
|
| 117 |
+
pred_class = np.argmax(probs)
|
| 118 |
+
confidence = probs[pred_class]
|
| 119 |
+
|
| 120 |
+
if confidence >= CONF_THRESHOLD:
|
| 121 |
+
return f"{GESTURE_LABELS[pred_class]} ({confidence*100:.2f}%)"
|
| 122 |
+
|
| 123 |
return "Unknown"
|
| 124 |
|
| 125 |
# ----------------------------
|
|
|
|
| 133 |
description="Upload an image of a hand gesture"
|
| 134 |
)
|
| 135 |
|
| 136 |
+
# ----------------------------
|
| 137 |
+
# Launch
|
| 138 |
+
# ----------------------------
|
| 139 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|