Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import torch
|
| 5 |
from ultralytics import YOLO
|
| 6 |
import gradio as gr
|
| 7 |
import traceback
|
|
|
|
| 8 |
# -----------------------------
|
| 9 |
# 1. YOLO model path
|
| 10 |
# -----------------------------
|
| 11 |
-
YOLO_MODEL_PATH = "best.pt"
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
-
# 2.
|
| 15 |
# -----------------------------
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
# -----------------------------
|
| 20 |
-
# 3.
|
| 21 |
# -----------------------------
|
| 22 |
def predict_asl(image):
|
| 23 |
try:
|
|
@@ -25,16 +28,14 @@ def predict_asl(image):
|
|
| 25 |
raise ValueError("No image uploaded")
|
| 26 |
|
| 27 |
img = image.copy()
|
| 28 |
-
h, w, _ = img.shape
|
| 29 |
-
print(f"🔹 Uploaded image shape: {img.shape}, dtype: {img.dtype}")
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
results = yolo_model.predict(img, imgsz=
|
| 33 |
pred_idx = results.probs.top1
|
| 34 |
pred_label = results.names[pred_idx]
|
| 35 |
confidence = results.probs.data[pred_idx].item()
|
| 36 |
|
| 37 |
-
# Overlay prediction text
|
| 38 |
cv2.putText(
|
| 39 |
img,
|
| 40 |
f"{pred_label} ({confidence:.2f})",
|
|
@@ -46,7 +47,10 @@ def predict_asl(image):
|
|
| 46 |
cv2.LINE_AA
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
print("❌ Error in predict_asl:", e)
|
|
@@ -57,13 +61,13 @@ def predict_asl(image):
|
|
| 57 |
# 4. Gradio Interface
|
| 58 |
# -----------------------------
|
| 59 |
title = "🖐️ ASL Letter Classifier"
|
| 60 |
-
description = "Upload a hand sign image and
|
| 61 |
|
| 62 |
iface = gr.Interface(
|
| 63 |
fn=predict_asl,
|
| 64 |
-
inputs=gr.Image(type="numpy"),
|
| 65 |
outputs=[
|
| 66 |
-
gr.Image(type="numpy", label="
|
| 67 |
gr.Textbox(label="Predicted Letter"),
|
| 68 |
gr.Textbox(label="Confidence")
|
| 69 |
],
|
|
@@ -73,4 +77,4 @@ iface = gr.Interface(
|
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
iface.launch(
|
|
|
|
| 1 |
import os
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from ultralytics import YOLO
|
| 5 |
import gradio as gr
|
| 6 |
import traceback
|
| 7 |
+
|
| 8 |
# -----------------------------
|
| 9 |
# 1. YOLO model path
|
| 10 |
# -----------------------------
|
| 11 |
+
YOLO_MODEL_PATH = "best.pt"
|
| 12 |
+
yolo_model = YOLO(YOLO_MODEL_PATH)
|
| 13 |
+
yolo_model.eval()
|
| 14 |
|
| 15 |
# -----------------------------
|
| 16 |
+
# 2. Reference alphabet image
|
| 17 |
# -----------------------------
|
| 18 |
+
REFERENCE_IMAGE_PATH = "asl_alphabet.webp" # Web-friendly format
|
| 19 |
+
reference_img = cv2.imread(REFERENCE_IMAGE_PATH)
|
| 20 |
+
reference_img = cv2.cvtColor(reference_img, cv2.COLOR_BGR2RGB)
|
| 21 |
|
| 22 |
# -----------------------------
|
| 23 |
+
# 3. Prediction function
|
| 24 |
# -----------------------------
|
| 25 |
def predict_asl(image):
|
| 26 |
try:
|
|
|
|
| 28 |
raise ValueError("No image uploaded")
|
| 29 |
|
| 30 |
img = image.copy()
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# YOLO prediction
|
| 33 |
+
results = yolo_model.predict(img, imgsz=320, verbose=False)[0]
|
| 34 |
pred_idx = results.probs.top1
|
| 35 |
pred_label = results.names[pred_idx]
|
| 36 |
confidence = results.probs.data[pred_idx].item()
|
| 37 |
|
| 38 |
+
# Overlay prediction text
|
| 39 |
cv2.putText(
|
| 40 |
img,
|
| 41 |
f"{pred_label} ({confidence:.2f})",
|
|
|
|
| 47 |
cv2.LINE_AA
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# Combine uploaded image and reference alphabet side by side
|
| 51 |
+
combined_img = np.hstack([cv2.cvtColor(img, cv2.COLOR_BGR2RGB), reference_img])
|
| 52 |
+
|
| 53 |
+
return combined_img, pred_label, round(confidence, 2)
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print("❌ Error in predict_asl:", e)
|
|
|
|
| 61 |
# 4. Gradio Interface
|
| 62 |
# -----------------------------
|
| 63 |
title = "🖐️ ASL Letter Classifier"
|
| 64 |
+
description = "Upload a hand sign image and compare it with the full ASL alphabet."
|
| 65 |
|
| 66 |
iface = gr.Interface(
|
| 67 |
fn=predict_asl,
|
| 68 |
+
inputs=gr.Image(type="numpy", label="Upload your ASL Letter"),
|
| 69 |
outputs=[
|
| 70 |
+
gr.Image(type="numpy", label="Your Image with Prediction & Alphabet Reference"),
|
| 71 |
gr.Textbox(label="Predicted Letter"),
|
| 72 |
gr.Textbox(label="Confidence")
|
| 73 |
],
|
|
|
|
| 77 |
)
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
+
iface.launch(share=True)
|