File size: 2,186 Bytes
1883b87
8feafb8
 
7585ca6
 
5d7eb68
7585ca6
 
 
 
 
 
 
1883b87
 
7585ca6
 
 
 
 
 
1883b87
7585ca6
 
 
afc4808
60c712e
98aca02
 
afc4808
60c712e
98aca02
 
 
 
 
 
60c712e
98aca02
 
 
 
7585ca6
 
 
 
 
 
 
 
 
aa46ad4
1883b87
7585ca6
 
aa46ad4
afc4808
7585ca6
afc4808
 
7585ca6
 
 
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
# Copyright (C) 2025 Anthony Casagrande
# Ultralytics YOLO 🚀, AGPL-3.0 license

import gradio as gr
import PIL.Image as Image
import cv2

from ultralytics import ASSETS, YOLO

model = None


def predict_image(img, conf_threshold, iou_threshold, model_name):
    """Predicts objects in an image using a YOLOv11 model with adjustable confidence and IOU thresholds."""
    model = YOLO("train29s.pt")
    results = model.predict(
        source=img,
        conf=conf_threshold,
        iou=iou_threshold,
        show_labels=True,
        show_conf=True,
        imgsz=1280,
    )

    for r in results:
        im_array1 = r.plot(img=img.copy(), labels=False, line_width=4)
        im1 = Image.fromarray(cv2.cvtColor(im_array1, cv2.COLOR_BGR2RGB))

    for r in results:
        im_array2 = r.plot(img=img.copy(), line_width=4, font_size=10)
        im2 = Image.fromarray(cv2.cvtColor(im_array2, cv2.COLOR_BGR2RGB))
    
    # Calculate the dimensions for the output image
    total_height = im1.height + im2.height
    total_width = im1.width

    # Create a blank image with the combined height
    im = Image.new("RGB", (total_width, total_height))

    # Paste the images one above the other
    im.paste(im1, (0, 0))
    im.paste(im2, (0, im1.height))

    return im


iface = gr.Interface(
    fn=predict_image,
    inputs=[
        gr.Image(type="pil", label="Upload Image"),
        gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
        gr.Slider(minimum=0, maximum=1, value=0.80, label="IoU threshold"),
        # gr.Radio(choices=["yolo11n", "yolo11s", "yolo11n-seg", "yolo11s-seg", "yolo11n-pose", "yolo11s-pose"], label="Model Name", value="yolo11n"),
    ],
    outputs=gr.Image(type="pil", label="Result"),
    title="RC Race Vision YOLO11 Gradio Application 🚀",
    description="Upload images of RC Race Cars for inference. \nCurrently supports 1/8th Buggies, and 1/10th Scale Buggies, Stadium Trucks, and Short Course Trucks.\nNot intended to work with close-up shots, works better on a wide view of the cars on a track.",
    examples=[
        ["rc1.jpg", 0.25, 0.8],
        ["rc2.jpg", 0.25, 0.8],
    ],
)
iface.launch(share=True)