Spaces:
Sleeping
Sleeping
import gradio as gr | |
from ultralytics import YOLO | |
from PIL import Image, ImageDraw | |
import tempfile | |
import os | |
# Load your trained model | |
model = YOLO("best.pt") # Replace with your trained model .pt path | |
# Detection function with confidence filter | |
def detect_disease(image): | |
# Save input to a temporary file | |
temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png") | |
image.save(temp.name) | |
# Run YOLOv8 inference | |
results = model(temp.name)[0] # Get first result from list | |
# Filter predictions with confidence >= 0.5 | |
filtered_boxes = [] | |
for box in results.boxes.data: | |
x1, y1, x2, y2, score, cls_id = box.tolist() | |
if score >= 0.5: | |
filtered_boxes.append((x1, y1, x2, y2, score, int(cls_id))) | |
# Draw boxes on image | |
draw = ImageDraw.Draw(image) | |
class_names = model.names | |
for x1, y1, x2, y2, score, cls_id in filtered_boxes: | |
draw.rectangle([x1, y1, x2, y2], outline="red", width=3) | |
draw.text((x1, y1 - 10), f"{class_names[cls_id]}: {score:.2f}", fill="red") | |
return image | |
# Gradio Interface | |
interface = gr.Interface( | |
fn=detect_disease, | |
inputs=gr.Image(type="pil"), | |
outputs=gr.Image(type="pil"), | |
title="🌿 Plant Disease Detector (YOLOv8)", | |
description="Upload a leaf image. This model will detect plant diseases using YOLOv8. Only results with confidence ≥ 50% are shown." | |
) | |
# Launch app | |
interface.launch() | |