Upload 6 files
Browse filesRelevant model files
- .gitattributes +1 -0
- classes.npy +3 -0
- hand_landmarker.task +3 -0
- nsl_model_v0-5.h5 +3 -0
- nsl_model_v0-9.h5 +3 -0
- nsl_model_v1.h5 +3 -0
- run-live-alpha.py +132 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
hand_landmarker.task filter=lfs diff=lfs merge=lfs -text
|
classes.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cf20d80fd50ea14a57bc51e0b1084f37ccd6afc379ed8ad31a946e4c920b1bdc
|
| 3 |
+
size 377
|
hand_landmarker.task
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fbc2a30080c3c557093b5ddfc334698132eb341044ccee322ccf8bcf3607cde1
|
| 3 |
+
size 7819105
|
nsl_model_v0-5.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9736a2d0ffdccf2d96a0745b87f46e80d5d143d0a0cb68b7be2ddfa277d1fcdd
|
| 3 |
+
size 241688
|
nsl_model_v0-9.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9c1a58332f83c112a8ba77e4c64bbc5640f2e80cb0bfbfb6ce855432f5be63b6
|
| 3 |
+
size 250476
|
nsl_model_v1.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9ddd7872f2affe87a481f40645eeaa5ad36575f89e0f5ef95f03326b023e00bf
|
| 3 |
+
size 250476
|
run-live-alpha.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import mediapipe as mp
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- Config ---
|
| 9 |
+
MODEL_PATH = 'hand_landmarker.task'
|
| 10 |
+
THRESHOLD = 0.7 # confidence threshold (70%)
|
| 11 |
+
|
| 12 |
+
BaseOptions = mp.tasks.BaseOptions
|
| 13 |
+
HandLandmarker = mp.tasks.vision.HandLandmarker
|
| 14 |
+
HandLandmarkerOptions = mp.tasks.vision.HandLandmarkerOptions
|
| 15 |
+
VisionRunningMode = mp.tasks.vision.RunningMode
|
| 16 |
+
|
| 17 |
+
# Loading model
|
| 18 |
+
if not os.path.exists('nsl_model_v1.h5'):
|
| 19 |
+
print("Run training script first!")
|
| 20 |
+
exit()
|
| 21 |
+
|
| 22 |
+
model = tf.keras.models.load_model('nsl_model_v1.h5')
|
| 23 |
+
classes = np.load('classes.npy', allow_pickle=True)
|
| 24 |
+
print(f"Loaded classes: {classes}")
|
| 25 |
+
|
| 26 |
+
# Helper to draw
|
| 27 |
+
def draw_landmarks(image, landmarks):
|
| 28 |
+
h, w, _ = image.shape
|
| 29 |
+
# Draw connections (standard MP)
|
| 30 |
+
HAND_CONNECTIONS = [
|
| 31 |
+
(0, 1), (1, 2), (2, 3), (3, 4), # thumb
|
| 32 |
+
(0, 5), (5, 6), (6, 7), (7, 8), # index
|
| 33 |
+
(5, 9), (9, 10), (10, 11), (11, 12), # middle
|
| 34 |
+
(9, 13), (13, 14), (14, 15), (15, 16), # ring
|
| 35 |
+
(13, 17), (17, 18), (18, 19), (19, 20), # pinky
|
| 36 |
+
(0, 17) # wrist to pinky base
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
for start_idx, end_idx in HAND_CONNECTIONS:
|
| 40 |
+
start = landmarks[start_idx]
|
| 41 |
+
end = landmarks[end_idx]
|
| 42 |
+
cv2.line(image, (int(start.x * w), int(start.y * h)), (int(end.x * w), int(end.y * h)), (200, 200, 200), 2)
|
| 43 |
+
# Draw points
|
| 44 |
+
for lm in landmarks:
|
| 45 |
+
cv2.circle(image, (int(lm.x * w), int(lm.y * h)), 4, (0, 0, 255), -1)
|
| 46 |
+
|
| 47 |
+
# Initialize Landmarker
|
| 48 |
+
options = HandLandmarkerOptions(
|
| 49 |
+
base_options=BaseOptions(model_asset_path=MODEL_PATH),
|
| 50 |
+
running_mode=VisionRunningMode.VIDEO,
|
| 51 |
+
num_hands=1,
|
| 52 |
+
min_hand_detection_confidence=0.5
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with HandLandmarker.create_from_options(options) as landmarker:
|
| 56 |
+
cap = cv2.VideoCapture(0)
|
| 57 |
+
start_time = time.time()
|
| 58 |
+
|
| 59 |
+
while True:
|
| 60 |
+
ret, frame = cap.read()
|
| 61 |
+
if not ret: break
|
| 62 |
+
|
| 63 |
+
frame = cv2.flip(frame, 1)
|
| 64 |
+
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 65 |
+
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=rgb_frame)
|
| 66 |
+
|
| 67 |
+
timestamp = int((time.time() - start_time) * 1000)
|
| 68 |
+
detection_result = landmarker.detect_for_video(mp_image, timestamp)
|
| 69 |
+
|
| 70 |
+
# Output info
|
| 71 |
+
display_text = "Waiting..."
|
| 72 |
+
color = (200, 200, 200)
|
| 73 |
+
|
| 74 |
+
if detection_result.hand_landmarks:
|
| 75 |
+
hand_landmarks = detection_result.hand_landmarks[0]
|
| 76 |
+
|
| 77 |
+
# --- Draw ---
|
| 78 |
+
draw_landmarks(frame, hand_landmarks)
|
| 79 |
+
|
| 80 |
+
# --- Preprocess (MUST MATCH COLLECTION LOGIC) ---
|
| 81 |
+
wrist = hand_landmarks[0]
|
| 82 |
+
middle_mcp = hand_landmarks[9]
|
| 83 |
+
|
| 84 |
+
# Calculate scale
|
| 85 |
+
scale = np.sqrt(
|
| 86 |
+
(middle_mcp.x - wrist.x)**2 +
|
| 87 |
+
(middle_mcp.y - wrist.y)**2 +
|
| 88 |
+
(middle_mcp.z - wrist.z)**2
|
| 89 |
+
)
|
| 90 |
+
if scale == 0: scale = 1.0
|
| 91 |
+
|
| 92 |
+
# Normalization*
|
| 93 |
+
coords = []
|
| 94 |
+
for lm in hand_landmarks:
|
| 95 |
+
rel_x = (lm.x - wrist.x) / scale
|
| 96 |
+
rel_y = (lm.y - wrist.y) / scale
|
| 97 |
+
rel_z = (lm.z - wrist.z) / scale
|
| 98 |
+
coords.extend([rel_x, rel_y, rel_z])
|
| 99 |
+
|
| 100 |
+
# --- Predict ---
|
| 101 |
+
input_data = np.array([coords])
|
| 102 |
+
prediction = model.predict(input_data, verbose=0)
|
| 103 |
+
class_id = np.argmax(prediction)
|
| 104 |
+
confidence = np.max(prediction)
|
| 105 |
+
|
| 106 |
+
predicted_char = classes[class_id]
|
| 107 |
+
|
| 108 |
+
# --- Display logic ---
|
| 109 |
+
if confidence > THRESHOLD:
|
| 110 |
+
display_text = f"Sign: {predicted_char}"
|
| 111 |
+
color = (0, 255, 0)
|
| 112 |
+
|
| 113 |
+
# Dynamic visual bar
|
| 114 |
+
bar_width = int(confidence * 200)
|
| 115 |
+
cv2.rectangle(frame, (50, 90), (50 + bar_width, 110), color, -1)
|
| 116 |
+
cv2.rectangle(frame, (50, 90), (250, 110), (255, 255, 255), 2)
|
| 117 |
+
else:
|
| 118 |
+
display_text = f"Unsure ({predicted_char}?)"
|
| 119 |
+
color = (0, 165, 255) # orange
|
| 120 |
+
|
| 121 |
+
cv2.putText(frame, f"{confidence:.2f}", (260, 108), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1)
|
| 122 |
+
|
| 123 |
+
# on screen result
|
| 124 |
+
cv2.rectangle(frame, (0, 0), (640, 60), (0, 0, 0), -1)
|
| 125 |
+
cv2.putText(frame, display_text, (20, 45), cv2.FONT_HERSHEY_SIMPLEX, 1.5, color, 3)
|
| 126 |
+
|
| 127 |
+
cv2.imshow('NSL Live Test', frame)
|
| 128 |
+
if cv2.waitKey(1) & 0xFF == 27:
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
cap.release()
|
| 132 |
+
cv2.destroyAllWindows()
|