File size: 1,431 Bytes
5b5367b
 
 
 
 
 
 
414b874
5b5367b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()