File size: 2,563 Bytes
cb2cf6f
0a0b7f1
cb2cf6f
0a0b7f1
 
5c5fe28
91ad5b8
0a0b7f1
2b3c5dc
0a0b7f1
91ad5b8
 
4573ba5
0a0b7f1
 
2b3c5dc
0a0b7f1
8df0804
91ad5b8
 
0a0b7f1
 
91ad5b8
0a0b7f1
 
5c5fe28
 
d6c4c85
5c5fe28
 
0a0b7f1
b13aa56
f7673c1
 
cc204a1
f7673c1
0a0b7f1
91ad5b8
5c5fe28
 
 
 
 
 
 
 
 
 
0a0b7f1
2b3c5dc
5c5fe28
 
 
 
 
0a0b7f1
 
2b3c5dc
0a0b7f1
2b3c5dc
 
 
0a0b7f1
2b3c5dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb2cf6f
 
2b3c5dc
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
78
79
80
81
82
83
84
import os
import cv2
import numpy as np
from ultralytics import YOLO
import gradio as gr
import traceback

# -----------------------------
# 1. YOLO model
# -----------------------------
YOLO_MODEL_PATH = "best.pt"
yolo_model = YOLO(YOLO_MODEL_PATH)


# -----------------------------
# 2. Reference alphabet image (WebP)
# -----------------------------
REFERENCE_IMAGE_PATH = "asl_alphabet.jpg"
reference_img = cv2.imread(REFERENCE_IMAGE_PATH)
reference_img = cv2.cvtColor(reference_img, cv2.COLOR_BGR2RGB)

# -----------------------------
# 3. Prediction function
# -----------------------------
def predict_asl(image):
    try:
        if image is None:
            raise ValueError("No image uploaded")
        
        img = image.copy()

        results = yolo_model.predict(image, imgsz=300, verbose=False)
        pred_idx = results[0].probs.top1
        pred_label = results[0].names[pred_idx]
        confidence = results[0].probs.top1conf.item() 


        # Overlay prediction text
        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 Layout
# -----------------------------
with gr.Blocks() as demo:
    gr.Markdown("## 🖐️ ASL Letter Classifier")
    gr.Markdown("Upload a hand sign image and see the predicted letter and confidence. The full ASL alphabet is always shown on the left as a reference.")

    with gr.Row():
        # Left column: reference alphabet
        with gr.Column(scale=1):
            gr.Image(value=reference_img, type="numpy", label="ASL Alphabet Reference")
        
        # Right column: upload & prediction
        with gr.Column(scale=2):
            input_image = gr.Image(type="numpy", label="Upload your ASL Letter")
            output_image = gr.Image(type="numpy", label="Prediction")
            pred_text = gr.Textbox(label="Predicted Letter")
            confidence_text = gr.Textbox(label="Confidence")
            
            input_image.change(
                fn=predict_asl,
                inputs=input_image,
                outputs=[output_image, pred_text, confidence_text]
            )

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