Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from ultralytics import YOLO | |
| from PIL import Image | |
| import numpy as np | |
| import os | |
| # --- Load YOLOv12 model --- | |
| model_path = "best.pt" | |
| if os.path.exists(model_path): | |
| model = YOLO(model_path) | |
| else: | |
| model = None | |
| # --- Inference function --- | |
| def detect_ear(image: Image.Image, conf: float = 0.25): | |
| if model is not None: | |
| try: | |
| # Run YOLOv12 inference | |
| results = model(np.array(image), conf=conf, verbose=False) | |
| annotated = results[0].plot() | |
| return Image.fromarray(annotated) | |
| except Exception as e: | |
| print(f"Model inference failed: {e}") | |
| # --- Dummy fallback --- | |
| import PIL.ImageDraw as ImageDraw | |
| im = image.copy() | |
| draw = ImageDraw.Draw(im) | |
| w, h = im.size | |
| box = (int(w*0.25), int(h*0.25), int(w*0.75), int(h*0.75)) | |
| draw.rectangle(box, outline="#00ffff", width=5) | |
| draw.text((box[0], box[1]-25), "Detected: Possible Condition (Dummy)", fill="#00ffff") | |
| return im | |
| # --- Gradio Interface --- | |
| iface = gr.Interface( | |
| fn=detect_ear, | |
| inputs=[ | |
| gr.Image(type="pil", label="Upload Otoscopic Image"), | |
| gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Confidence Threshold") | |
| ], | |
| outputs=gr.Image(type="pil", label="Detection Result"), | |
| title="Automated Ear Disease Detection", | |
| description="Detect ear conditions using YOLOv12", | |
| ) | |
| # --- Launch App --- | |
| iface.launch() | |