File size: 2,131 Bytes
b0ee9c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
import cv2
import numpy as np
import torch
from ultralytics import YOLO
import gradio as gr
import traceback
# -----------------------------
# 1. YOLO model path
# -----------------------------
YOLO_MODEL_PATH = "best.pt"  # Push this small model to HF repo

# -----------------------------
# 2. Load YOLO model
# -----------------------------
yolo_model = YOLO(YOLO_MODEL_PATH)
yolo_model.eval()

# -----------------------------
# 3. Inference function
# -----------------------------
def predict_asl(image):
    try:
        if image is None:
            raise ValueError("No image uploaded")
        
        img = image.copy()
        h, w, _ = img.shape
        print(f"🔹 Uploaded image shape: {img.shape}, dtype: {img.dtype}")

        # --- YOLO prediction directly on NumPy array ---
        results = yolo_model.predict(img, imgsz=300, verbose=False)[0]
        pred_idx = results.probs.top1
        pred_label = results.names[pred_idx]
        confidence = results.probs.data[pred_idx].item()

        # Overlay prediction text on original image
        cv2.putText(
            img,
            f"{pred_label} ({confidence:.2f})",
            (10, 30),
            cv2.FONT_HERSHEY_SIMPLEX,
            1,
            (0, 0, 255),
            2,
            cv2.LINE_AA
        )

        return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), pred_label, round(confidence, 2)

    except Exception as e:
        print("❌ Error in predict_asl:", e)
        traceback.print_exc()
        return image, "Error", 0.0

# -----------------------------
# 4. Gradio Interface
# -----------------------------
title = "🖐️ ASL Letter Classifier"
description = "Upload a hand sign image and see the predicted letter and confidence."

iface = gr.Interface(
    fn=predict_asl,
    inputs=gr.Image(type="numpy"),
    outputs=[
        gr.Image(type="numpy", label="Original Image with Prediction"),
        gr.Textbox(label="Predicted Letter"),
        gr.Textbox(label="Confidence")
    ],
    title=title,
    description=description,
    allow_flagging="never"
)

if __name__ == "__main__":
    iface.launch(share=True)