File size: 2,951 Bytes
45439d0
b0a31e1
79d95c0
1d11470
79d95c0
1d11470
45439d0
ea9cbec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79d95c0
45439d0
 
 
 
 
 
 
 
 
 
8c94676
 
 
 
45439d0
 
 
 
28751ad
45439d0
 
 
 
 
 
 
 
 
 
 
 
 
aafcd69
c371468
2c2e40f
45439d0
 
3359693
 
 
3b67c02
 
 
 
45439d0
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
from ultralytics import YOLO
import spaces

@spaces.GPU
def yolo_inference(images, model_id, conf_threshold, iou_threshold, max_detection):
    if images is None:
        # Create a blank image
        width, height = 640, 480
        blank_image = Image.new("RGB", (width, height), color="white")
        draw = ImageDraw.Draw(blank_image)
        message = "No image provided"
        font = ImageFont.load_default(size=40)
        bbox = draw.textbbox((0, 0), message, font=font)
        text_width = bbox[2] - bbox[0]
        text_height = bbox[3] - bbox[1]
        text_x = (width - text_width) / 2
        text_y = (height - text_height) / 2
        draw.text((text_x, text_y), message, fill="black", font=font)
        return blank_image
    
    model = YOLO(model_id)
    results = model.predict(
        source=images,
        conf=conf_threshold,
        iou=iou_threshold,
        imgsz=640,
        max_det=max_detection,
        show_labels=True,
        show_conf=True,
    )

    for r in results:
        image_array = r.plot()
        image = Image.fromarray(image_array[..., ::-1])
    return image

interface = gr.Interface(
    fn=yolo_inference,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Dropdown(
            choices=['yolo11n.pt', 'yolo11s.pt', 'yolo11m.pt', 'yolo11l.pt', 'yolo11x.pt', 
                     'yolo11n-seg.pt', 'yolo11s-seg.pt', 'yolo11m-seg.pt', 'yolo11l-seg.pt', 'yolo11x-seg.pt',
                     'yolo11n-pose.pt', 'yolo11s-pose.pt', 'yolo11m-pose.pt', 'yolo11l-pose.pt', 'yolo11x-pose.pt',
                     'yolo11n-obb.pt', 'yolo11s-obb.pt', 'yolo11m-obb.pt', 'yolo11l-obb.pt', 'yolo11x-obb.pt',
                     'yolo11n-cls.pt', 'yolo11s-cls.pt', 'yolo11m-cls.pt', 'yolo11l-cls.pt', 'yolo11x-cls.pt'],
            label="Model Name",
            value="yolo11n.pt",
        ),
        gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence Threshold"),
        gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU Threshold"),
        gr.Slider(minimum=1, maximum=300, step=1, value=300, label="Max Detection"),
    ],
    outputs=gr.Image(type="pil", label="Annotated Image"),
    cache_examples=True,
    title="Yolo11: Object Detection, Instance Segmentation, Pose/Keypoints, Oriented Detection, Classification",
    description="Upload image(s) for inference using the latest Ultralytics YOLO11 models.",
    examples=[
        ["zidane.jpg", "yolo11s.pt", 0.25, 0.45, 300],
        ["bus.jpg", "yolo11m.pt", 0.25, 0.45, 300],
        ["yolo_vision.jpg", "yolo11x.pt", 0.25, 0.45, 300],
        ["Tricycle.jpg", "yolo11x-cls.pt", 0.25, 0.45, 300],
        ["tcganadolu.jpg", "yolo11m-obb.pt", 0.25, 0.45, 300],
        ["San Diego Airport.jpg", "yolo11x-seg.pt", 0.25, 0.45, 300],
        ["Theodore_Roosevelt.png", "yolo11l-pose.pt", 0.25, 0.45, 300],
    ],
)
interface.launch()