Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ import traceback
|
|
| 15 |
# -----------------------------
|
| 16 |
HAND_MODEL_PATH = "hand_landmarker.task"
|
| 17 |
HAND_MODEL_URL = "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task"
|
| 18 |
-
YOLO_MODEL_PATH = "yolov11n_finetuned_ASL.pt"
|
| 19 |
|
| 20 |
# -----------------------------
|
| 21 |
# 2. Download MediaPipe model if missing
|
|
@@ -30,22 +30,20 @@ if not os.path.exists(HAND_MODEL_PATH):
|
|
| 30 |
# -----------------------------
|
| 31 |
# 3. Load models
|
| 32 |
# -----------------------------
|
| 33 |
-
# YOLO ASL classifier
|
| 34 |
yolo_model = YOLO(YOLO_MODEL_PATH)
|
| 35 |
yolo_model.eval()
|
| 36 |
|
| 37 |
-
# MediaPipe hand landmark detector
|
| 38 |
base_options = python.BaseOptions(model_asset_path=HAND_MODEL_PATH)
|
| 39 |
hand_options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
|
| 40 |
detector = vision.HandLandmarker.create_from_options(hand_options)
|
| 41 |
|
| 42 |
# -----------------------------
|
| 43 |
-
# 4. Inference function
|
| 44 |
# -----------------------------
|
| 45 |
def predict_asl(image):
|
| 46 |
try:
|
| 47 |
if image is None:
|
| 48 |
-
raise ValueError("No image
|
| 49 |
|
| 50 |
img = image.copy()
|
| 51 |
h, w, _ = img.shape
|
|
@@ -60,7 +58,7 @@ def predict_asl(image):
|
|
| 60 |
x, y = int(landmark.x * w), int(landmark.y * h)
|
| 61 |
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
|
| 62 |
|
| 63 |
-
# --- YOLO prediction ---
|
| 64 |
results = yolo_model.predict(img, imgsz=300, verbose=False)[0]
|
| 65 |
pred_idx = results.probs.top1
|
| 66 |
pred_label = results.names[pred_idx]
|
|
@@ -83,7 +81,6 @@ def predict_asl(image):
|
|
| 83 |
except Exception as e:
|
| 84 |
print("❌ Error in predict_asl:", e)
|
| 85 |
traceback.print_exc()
|
| 86 |
-
# Return original image and error placeholders
|
| 87 |
return image, "Error", 0.0
|
| 88 |
|
| 89 |
# -----------------------------
|
|
|
|
| 15 |
# -----------------------------
|
| 16 |
HAND_MODEL_PATH = "hand_landmarker.task"
|
| 17 |
HAND_MODEL_URL = "https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task"
|
| 18 |
+
YOLO_MODEL_PATH = "yolov11n_finetuned_ASL.pt" # Push this small model to HF repo
|
| 19 |
|
| 20 |
# -----------------------------
|
| 21 |
# 2. Download MediaPipe model if missing
|
|
|
|
| 30 |
# -----------------------------
|
| 31 |
# 3. Load models
|
| 32 |
# -----------------------------
|
|
|
|
| 33 |
yolo_model = YOLO(YOLO_MODEL_PATH)
|
| 34 |
yolo_model.eval()
|
| 35 |
|
|
|
|
| 36 |
base_options = python.BaseOptions(model_asset_path=HAND_MODEL_PATH)
|
| 37 |
hand_options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
|
| 38 |
detector = vision.HandLandmarker.create_from_options(hand_options)
|
| 39 |
|
| 40 |
# -----------------------------
|
| 41 |
+
# 4. Inference function
|
| 42 |
# -----------------------------
|
| 43 |
def predict_asl(image):
|
| 44 |
try:
|
| 45 |
if image is None:
|
| 46 |
+
raise ValueError("No image uploaded")
|
| 47 |
|
| 48 |
img = image.copy()
|
| 49 |
h, w, _ = img.shape
|
|
|
|
| 58 |
x, y = int(landmark.x * w), int(landmark.y * h)
|
| 59 |
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
|
| 60 |
|
| 61 |
+
# --- YOLO prediction directly on NumPy array ---
|
| 62 |
results = yolo_model.predict(img, imgsz=300, verbose=False)[0]
|
| 63 |
pred_idx = results.probs.top1
|
| 64 |
pred_label = results.names[pred_idx]
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
print("❌ Error in predict_asl:", e)
|
| 83 |
traceback.print_exc()
|
|
|
|
| 84 |
return image, "Error", 0.0
|
| 85 |
|
| 86 |
# -----------------------------
|