Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,16 @@ import gradio as gr
|
|
| 6 |
import traceback
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
-
# 1. YOLO model
|
| 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"
|
| 19 |
reference_img = cv2.imread(REFERENCE_IMAGE_PATH)
|
| 20 |
reference_img = cv2.cvtColor(reference_img, cv2.COLOR_BGR2RGB)
|
| 21 |
|
|
@@ -29,7 +29,6 @@ def predict_asl(image):
|
|
| 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]
|
|
@@ -47,10 +46,7 @@ def predict_asl(image):
|
|
| 47 |
cv2.LINE_AA
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 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)
|
|
@@ -58,23 +54,29 @@ def predict_asl(image):
|
|
| 58 |
return image, "Error", 0.0
|
| 59 |
|
| 60 |
# -----------------------------
|
| 61 |
-
# 4. Gradio
|
| 62 |
# -----------------------------
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
gr.
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
|
|
|
|
| 6 |
import traceback
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
+
# 1. YOLO model
|
| 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 (WebP)
|
| 17 |
# -----------------------------
|
| 18 |
+
REFERENCE_IMAGE_PATH = "asl_alphabet.webp"
|
| 19 |
reference_img = cv2.imread(REFERENCE_IMAGE_PATH)
|
| 20 |
reference_img = cv2.cvtColor(reference_img, cv2.COLOR_BGR2RGB)
|
| 21 |
|
|
|
|
| 29 |
|
| 30 |
img = image.copy()
|
| 31 |
|
|
|
|
| 32 |
results = yolo_model.predict(img, imgsz=320, verbose=False)[0]
|
| 33 |
pred_idx = results.probs.top1
|
| 34 |
pred_label = results.names[pred_idx]
|
|
|
|
| 46 |
cv2.LINE_AA
|
| 47 |
)
|
| 48 |
|
| 49 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), pred_label, round(confidence, 2)
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
print("❌ Error in predict_asl:", e)
|
|
|
|
| 54 |
return image, "Error", 0.0
|
| 55 |
|
| 56 |
# -----------------------------
|
| 57 |
+
# 4. Gradio Layout
|
| 58 |
# -----------------------------
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("## 🖐️ ASL Letter Classifier")
|
| 61 |
+
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.")
|
| 62 |
|
| 63 |
+
with gr.Row():
|
| 64 |
+
# Left column: reference alphabet
|
| 65 |
+
with gr.Column(scale=1):
|
| 66 |
+
gr.Image(value=reference_img, type="numpy", label="ASL Alphabet Reference")
|
| 67 |
+
|
| 68 |
+
# Right column: upload & prediction
|
| 69 |
+
with gr.Column(scale=2):
|
| 70 |
+
input_image = gr.Image(type="numpy", label="Upload your ASL Letter")
|
| 71 |
+
output_image = gr.Image(type="numpy", label="Prediction")
|
| 72 |
+
pred_text = gr.Textbox(label="Predicted Letter")
|
| 73 |
+
confidence_text = gr.Textbox(label="Confidence")
|
| 74 |
+
|
| 75 |
+
input_image.change(
|
| 76 |
+
fn=predict_asl,
|
| 77 |
+
inputs=input_image,
|
| 78 |
+
outputs=[output_image, pred_text, confidence_text]
|
| 79 |
+
)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch(share=True)
|