Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# --- Load YOLOv12 model ---
|
| 8 |
+
model_path = "best.pt"
|
| 9 |
+
if os.path.exists(model_path):
|
| 10 |
+
model = YOLO(model_path)
|
| 11 |
+
else:
|
| 12 |
+
model = None
|
| 13 |
+
|
| 14 |
+
# --- Inference function ---
|
| 15 |
+
def detect_ear(image: Image.Image, conf: float = 0.25):
|
| 16 |
+
if model is not None:
|
| 17 |
+
try:
|
| 18 |
+
# Run YOLOv12 inference
|
| 19 |
+
results = model(np.array(image), conf=conf, verbose=False)
|
| 20 |
+
annotated = results[0].plot()
|
| 21 |
+
return Image.fromarray(annotated)
|
| 22 |
+
except Exception as e:
|
| 23 |
+
print(f"Model inference failed: {e}")
|
| 24 |
+
|
| 25 |
+
# --- Dummy fallback ---
|
| 26 |
+
import PIL.ImageDraw as ImageDraw
|
| 27 |
+
im = image.copy()
|
| 28 |
+
draw = ImageDraw.Draw(im)
|
| 29 |
+
w, h = im.size
|
| 30 |
+
box = (int(w*0.25), int(h*0.25), int(w*0.75), int(h*0.75))
|
| 31 |
+
draw.rectangle(box, outline="#00ffff", width=5)
|
| 32 |
+
draw.text((box[0], box[1]-25), "Detected: Possible Condition (Dummy)", fill="#00ffff")
|
| 33 |
+
return im
|
| 34 |
+
|
| 35 |
+
# --- Gradio Interface ---
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=detect_ear,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Image(type="pil", label="Upload Otoscopic Image"),
|
| 40 |
+
gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Confidence Threshold")
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Image(type="pil", label="Detection Result"),
|
| 43 |
+
title="Automated Ear Disease Detection",
|
| 44 |
+
description="Detect ear conditions using YOLOv12",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# --- Launch App ---
|
| 48 |
+
iface.launch()
|