File size: 10,557 Bytes
4a66401
 
 
 
 
4e284d1
4a66401
 
4e284d1
 
4a66401
 
 
 
 
4e284d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a66401
 
 
4e284d1
 
 
4a66401
 
4e284d1
 
 
 
4a66401
4e284d1
 
4a66401
 
 
 
 
 
 
 
 
4e284d1
4a66401
4e284d1
 
4a66401
4e284d1
4a66401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e284d1
4a66401
 
 
 
 
 
 
4e284d1
 
 
 
 
 
 
4a66401
 
 
4e284d1
 
 
4a66401
 
4e284d1
 
 
 
 
 
 
 
 
 
 
 
4a66401
4e284d1
 
4a66401
 
 
 
4e284d1
4a66401
 
 
 
 
 
 
4e284d1
4a66401
 
 
 
4e284d1
4a66401
 
 
 
 
 
 
4e284d1
4a66401
 
 
 
4e284d1
4a66401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e284d1
 
 
 
4a66401
 
 
 
 
 
4e284d1
 
 
 
 
 
4a66401
4e284d1
4a66401
 
4e284d1
4a66401
4e284d1
4a66401
4e284d1
4a66401
4e284d1
4a66401
 
 
 
 
 
 
 
 
 
 
4e284d1
 
 
 
4a66401
 
 
 
4e284d1
 
 
4a66401
 
 
 
 
 
 
4e284d1
 
 
 
4a66401
 
 
 
4e284d1
 
 
4a66401
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from typing import Tuple

import gradio as gr
import numpy as np
import supervision as sv
from ultralytics import YOLO

MARKDOWN = """
<h1 style='text-align: left'>YOLO-Playground πŸ“ˆ</h1>
Welcome to YOLO-Playground! This demo showcases the detection capabilities of various YOLO models pre-trained on the COCO Dataset. πŸš€πŸ”πŸ‘€

A simple project just for fun for on the go object detection. πŸŽ‰

Inspired from YOLO-ARENA by SkalskiP. πŸ™

- **YOLOv8**
  <div style="display: flex; align-items: center;">
    <a href="https://docs.ultralytics.com/models/yolov8/" style="margin-right: 10px;">
      <img src="https://badges.aleen42.com/src/github.svg">
    </a>
    <a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov8-object-detection-on-custom-dataset.ipynb" style="margin-right: 10px;">
      <img src="https://colab.research.google.com/assets/colab-badge.svg">
    </a>
  </div>
- **YOLOv9**
  <div style="display: flex; align-items: center;">
    <a href="https://github.com/WongKinYiu/yolov9" style="margin-right: 10px;">
      <img src="https://badges.aleen42.com/src/github.svg">
    </a>
    <a href="https://arxiv.org/abs/2402.13616" style="margin-right: 10px;">
      <img src="https://img.shields.io/badge/arXiv-2402.13616-b31b1b.svg">
    </a>
    <a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov9-object-detection-on-custom-dataset.ipynb" style="margin-right: 10px;">
      <img src="https://colab.research.google.com/assets/colab-badge.svg">
    </a>
  </div>
- **YOLOv10**
  <div style="display: flex; align-items: center;">
    <a href="https://github.com/THU-MIG/yolov10" style="margin-right: 10px;">
      <img src="https://badges.aleen42.com/src/github.svg">
    </a>
    <a href="https://arxiv.org/abs/2405.14458" style="margin-right: 10px;">
      <img src="https://img.shields.io/badge/arXiv-2405.14458-b31b1b.svg">
    </a>
    <a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov10-object-detection-on-custom-dataset.ipynb" style="margin-right: 10px;">
      <img src="https://colab.research.google.com/assets/colab-badge.svg">
    </a>
  </div>
- **YOLO11**
<div style="display: flex; align-items: center;">
    <a href="https://docs.ultralytics.com/models/yolo11/" style="margin-right: 10px;">
      <img src="https://badges.aleen42.com/src/github.svg">
    </a>
    <a href="https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/train-yolov8-object-detection-on-custom-dataset.ipynb" style="margin-right: 10px;">
      <img src="https://colab.research.google.com/assets/colab-badge.svg">
    </a>
  </div>

Powered by Roboflow [Inference](https://github.com/roboflow/inference), 
[Supervision](https://github.com/roboflow/supervision) and [Ultralytics](https://github.com/ultralytics/ultralytics).πŸ”₯
"""

IMAGE_EXAMPLES = [
    ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, 0.3, 0.3, 0.3, 0.5],
    ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, 0.3, 0.3, 0.3, 0.5],
    ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.3, 0.3, 0.3, 0.3, 0.5],
]

YOLO_V8S_MODEL = YOLO("yolov8s.pt")
YOLO_V9S_MODEL = YOLO("yolov9s.pt")
YOLO_V10S_MODEL = YOLO("yolov10s.pt")
YOLO_11S_MODEL = YOLO("yolo11s.pt")

LABEL_ANNOTATORS = sv.LabelAnnotator()
BOUNDING_BOX_ANNOTATORS = sv.BoxAnnotator()


def detect_and_annotate(
    model,
    input_image: np.ndarray,
    confidence_threshold: float,
    iou_threshold: float,
    class_id_mapping: dict = None
) -> np.ndarray:
    result = model(
        input_image,
        conf=confidence_threshold,
        iou=iou_threshold
    )[0]
    detections = sv.Detections.from_ultralytics(result)

    if class_id_mapping:
        detections.class_id = np.array([
            class_id_mapping[class_id]
            for class_id
            in detections.class_id
        ])

    labels = [
        f"{class_name} ({confidence:.2f})"
        for class_name, confidence
        in zip(detections['class_name'], detections.confidence)
    ]

    annotated_image = input_image.copy()
    annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(
        scene=annotated_image, detections=detections)
    annotated_image = LABEL_ANNOTATORS.annotate(
        scene=annotated_image, detections=detections, labels=labels)
    return annotated_image


def process_image(
    input_image: np.ndarray,
    yolo_v8_confidence_threshold: float,
    yolo_v9_confidence_threshold: float,
    yolo_v10_confidence_threshold: float,
    yolov11_confidence_threshold: float,
    iou_threshold: float
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
    # Validate iou_threshold before using it
    if iou_threshold is None or not isinstance(iou_threshold, float):
        iou_threshold = 0.3  # Default value, adjust as necessary

    yolo_v8s_annotated_image = detect_and_annotate(
        YOLO_V8S_MODEL, input_image, yolo_v8_confidence_threshold, iou_threshold)
    yolo_v9s_annotated_image = detect_and_annotate(
        YOLO_V9S_MODEL, input_image, yolo_v9_confidence_threshold, iou_threshold)
    yolo_v10s_annotated_image = detect_and_annotate(
        YOLO_V10S_MODEL, input_image, yolo_v10_confidence_threshold, iou_threshold)
    yolo_11s_annnotated_image = detect_and_annotate(
        YOLO_11S_MODEL, input_image, yolov11_confidence_threshold, iou_threshold)

    return (
        yolo_v8s_annotated_image,
        yolo_v9s_annotated_image,
        yolo_v10s_annotated_image,
        yolo_11s_annnotated_image
    )

yolo_v8s_confidence_threshold_component = gr.Slider(
    minimum=0,
    maximum=1.0,
    value=0.3,
    step=0.01,
    label="YOLOv8s Confidence Threshold",
    info=(
        "The confidence threshold for the YOLO model. Lower the threshold to "
        "reduce false negatives, enhancing the model's sensitivity to detect "
        "sought-after objects. Conversely, increase the threshold to minimize false "
        "positives, preventing the model from identifying objects it shouldn't."
    ))


yolo_v9s_confidence_threshold_component = gr.Slider(
    minimum=0,
    maximum=1.0,
    value=0.3,
    step=0.01,
    label="YOLOv9s Confidence Threshold",
    info=(
        "The confidence threshold for the YOLO model. Lower the threshold to "
        "reduce false negatives, enhancing the model's sensitivity to detect "
        "sought-after objects. Conversely, increase the threshold to minimize false "
        "positives, preventing the model from identifying objects it shouldn't."
    ))

yolo_v10s_confidence_threshold_component = gr.Slider(
    minimum=0,
    maximum=1.0,
    value=0.3,
    step=0.01,
    label="YOLOv10s Confidence Threshold",
    info=(
        "The confidence threshold for the YOLO model. Lower the threshold to "
        "reduce false negatives, enhancing the model's sensitivity to detect "
        "sought-after objects. Conversely, increase the threshold to minimize false "
        "positives, preventing the model from identifying objects it shouldn't."
    ))

yolo_11s_confidence_threshold_component = gr.Slider(
    minimum=0,
    maximum=1.0,
    value=0.3,
    step=0.01,
    label="YOLO11s Confidence Threshold",
    info=(
        "The confidence threshold for the YOLO model. Lower the threshold to "
        "reduce false negatives, enhancing the model's sensitivity to detect "
        "sought-after objects. Conversely, increase the threshold to minimize false "
        "positives, preventing the model from identifying objects it shouldn't."
    ))

iou_threshold_component = gr.Slider(
    minimum=0,
    maximum=1.0,
    value=0.5,
    step=0.01,
    label="IoU Threshold",
    info=(
        "The Intersection over Union (IoU) threshold for non-maximum suppression. "
        "Decrease the value to lessen the occurrence of overlapping bounding boxes, "
        "making the detection process stricter. On the other hand, increase the value "
        "to allow more overlapping bounding boxes, accommodating a broader range of "
        "detections."
    ))


with gr.Blocks() as demo:
    gr.Markdown(MARKDOWN)
    with gr.Accordion("Configuration", open=False):
        with gr.Row():
            yolo_v8s_confidence_threshold_component.render()
            yolo_v9s_confidence_threshold_component.render()
            yolo_v10s_confidence_threshold_component.render()
            yolo_11s_confidence_threshold_component.render()
        iou_threshold_component.render()
    with gr.Row():
        input_image_component = gr.Image(
            type='pil',
            label='Input'
        )
    with gr.Row():
        yolo_v8s_output_image_component = gr.Image(
            type='pil',
            label='YOLOv8s'
        )
        yolo_v9s_output_image_component = gr.Image(
            type='pil',
            label='YOLOv9s'
        )
    with gr.Row():
        yolo_v10s_output_image_component = gr.Image(
            type='pil',
            label='YOLOv10s'
        )
        yolo_11s_output_image_component = gr.Image(
            type='pil',
            label='YOLO11s'
            )
    submit_button_component = gr.Button(
        value='Submit',
        scale=1,
        variant='primary'
    )
    gr.Examples(
        fn=process_image,
        examples=IMAGE_EXAMPLES,
        inputs=[
            input_image_component,
            yolo_v8s_confidence_threshold_component,
            yolo_v9s_confidence_threshold_component,
            yolo_v10s_confidence_threshold_component,
            yolo_11s_confidence_threshold_component,
            iou_threshold_component
        ],
        outputs=[
            yolo_v8s_output_image_component,
            yolo_v9s_output_image_component,
            yolo_v10s_output_image_component,
            yolo_11s_output_image_component
        ]
    )

    submit_button_component.click(
        fn=process_image,
        inputs=[
            input_image_component,
            yolo_v8s_confidence_threshold_component,
            yolo_v9s_confidence_threshold_component,
            yolo_v10s_confidence_threshold_component,
            yolo_11s_confidence_threshold_component,
            iou_threshold_component
        ],
        outputs=[
            yolo_v8s_output_image_component,
            yolo_v9s_output_image_component,
            yolo_v10s_output_image_component,
            yolo_11s_output_image_component
        ]
    )

demo.launch(debug=False, show_error=True, max_threads=1)