File size: 1,380 Bytes
2c5b705
 
 
 
 
 
 
 
 
 
 
7c99a6d
 
 
 
 
 
 
 
 
47aa72a
7c99a6d
 
47aa72a
7c99a6d
 
 
 
 
2c5b705
 
 
 
 
7c99a6d
 
 
 
2c5b705
47aa72a
2c5b705
 
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
import gradio as gr
from ultralytics import YOLO
from PIL import Image
import numpy as np

# Load the YOLOv9 model
model = YOLO('best.pt')  # Make sure best.pt is in the same folder

def detect(image):
    results = model(image)
    annotated_frame = results[0].plot()  # Draw predictions on the image
    
    # Get detection results
    detections = []
    for r in results:
        boxes = r.boxes
        for box in boxes:
            x1, y1, x2, y2 = box.xyxy[0].tolist()  # Get box coordinates
            conf = float(box.conf[0])  # Get confidence
            cls = int(box.cls[0])  # Get class
            class_name = model.names[cls]  # Get class name
            detections.append({
                'class': cls,
                'class_name': class_name,
                'confidence': conf,
                'box': [x1, y1, x2, y2]
            })
    
    return Image.fromarray(annotated_frame), detections

# Launch the Gradio interface
gr.Interface(
    fn=detect,
    inputs=gr.Image(type="pil"),
    outputs=[
        gr.Image(type="pil", label="Detected Image"),
        gr.JSON(label="Detection Results")
    ],
    title="YOLOv9 (Ultralytics) Object Detection",
    description="Upload an image to run object detection using your custom YOLOv9 model. The results will show both the annotated image and the detection details including class names.",
).launch()