mbar0075 commited on
Commit
1cab7ec
Β·
1 Parent(s): 3ab69ef

Initial Commit

Browse files
Files changed (3) hide show
  1. README.md +6 -5
  2. app.py +283 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,13 +1,14 @@
1
  ---
2
- title: YOLO Playground
3
- emoji: 🐨
4
- colorFrom: green
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.25.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: YOLO-Playground
3
+ emoji: πŸ“ˆ
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.39.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
+ short_description: Identify objects in images using multiple YOLO models
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple
2
+
3
+ import gradio as gr
4
+ import numpy as np
5
+ import supervision as sv
6
+ from ultralytics import YOLO
7
+
8
+ MARKDOWN = """
9
+ <h1 style='text-align: left'>YOLO-Playground πŸ“ˆ</h1>
10
+ Welcome to YOLO-Playground! This demo showcases the detection capabilities of various YOLO models pre-trained on the COCO Dataset. πŸš€πŸ”πŸ‘€
11
+
12
+ A simple project just for fun for on the go object detection. πŸŽ‰
13
+
14
+ Inspired from YOLO-ARENA by SkalskiP. πŸ™
15
+
16
+ - **YOLOv8**
17
+ <div style="display: flex; align-items: center;">
18
+ <a href="https://docs.ultralytics.com/models/yolov8/" style="margin-right: 10px;">
19
+ <img src="https://badges.aleen42.com/src/github.svg">
20
+ </a>
21
+ <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;">
22
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
23
+ </a>
24
+ </div>
25
+ - **YOLOv9**
26
+ <div style="display: flex; align-items: center;">
27
+ <a href="https://github.com/WongKinYiu/yolov9" style="margin-right: 10px;">
28
+ <img src="https://badges.aleen42.com/src/github.svg">
29
+ </a>
30
+ <a href="https://arxiv.org/abs/2402.13616" style="margin-right: 10px;">
31
+ <img src="https://img.shields.io/badge/arXiv-2402.13616-b31b1b.svg">
32
+ </a>
33
+ <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;">
34
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
35
+ </a>
36
+ </div>
37
+ - **YOLOv10**
38
+ <div style="display: flex; align-items: center;">
39
+ <a href="https://github.com/THU-MIG/yolov10" style="margin-right: 10px;">
40
+ <img src="https://badges.aleen42.com/src/github.svg">
41
+ </a>
42
+ <a href="https://arxiv.org/abs/2405.14458" style="margin-right: 10px;">
43
+ <img src="https://img.shields.io/badge/arXiv-2405.14458-b31b1b.svg">
44
+ </a>
45
+ <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;">
46
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
47
+ </a>
48
+ </div>
49
+ - **YOLO11**
50
+ <div style="display: flex; align-items: center;">
51
+ <a href="https://docs.ultralytics.com/models/yolo11/" style="margin-right: 10px;">
52
+ <img src="https://badges.aleen42.com/src/github.svg">
53
+ </a>
54
+ <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;">
55
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
56
+ </a>
57
+ </div>
58
+
59
+ Powered by Roboflow [Inference](https://github.com/roboflow/inference),
60
+ [Supervision](https://github.com/roboflow/supervision) and [Ultralytics](https://github.com/ultralytics/ultralytics).πŸ”₯
61
+ """
62
+
63
+ IMAGE_EXAMPLES = [
64
+ ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, 0.3, 0.3, 0.3, 0.5],
65
+ ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, 0.3, 0.3, 0.3, 0.5],
66
+ ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.3, 0.3, 0.3, 0.3, 0.5],
67
+ ]
68
+
69
+ YOLO_V8S_MODEL = YOLO("yolov8s.pt")
70
+ YOLO_V9S_MODEL = YOLO("yolov9s.pt")
71
+ YOLO_V10S_MODEL = YOLO("yolov10s.pt")
72
+ YOLO_11S_MODEL = YOLO("yolo11s.pt")
73
+
74
+ LABEL_ANNOTATORS = sv.LabelAnnotator()
75
+ BOUNDING_BOX_ANNOTATORS = sv.BoxAnnotator()
76
+
77
+
78
+ def detect_and_annotate(
79
+ model,
80
+ input_image: np.ndarray,
81
+ confidence_threshold: float,
82
+ iou_threshold: float,
83
+ class_id_mapping: dict = None
84
+ ) -> np.ndarray:
85
+ result = model(
86
+ input_image,
87
+ conf=confidence_threshold,
88
+ iou=iou_threshold
89
+ )[0]
90
+ detections = sv.Detections.from_ultralytics(result)
91
+
92
+ if class_id_mapping:
93
+ detections.class_id = np.array([
94
+ class_id_mapping[class_id]
95
+ for class_id
96
+ in detections.class_id
97
+ ])
98
+
99
+ labels = [
100
+ f"{class_name} ({confidence:.2f})"
101
+ for class_name, confidence
102
+ in zip(detections['class_name'], detections.confidence)
103
+ ]
104
+
105
+ annotated_image = input_image.copy()
106
+ annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(
107
+ scene=annotated_image, detections=detections)
108
+ annotated_image = LABEL_ANNOTATORS.annotate(
109
+ scene=annotated_image, detections=detections, labels=labels)
110
+ return annotated_image
111
+
112
+
113
+ def process_image(
114
+ input_image: np.ndarray,
115
+ yolo_v8_confidence_threshold: float,
116
+ yolo_v9_confidence_threshold: float,
117
+ yolo_v10_confidence_threshold: float,
118
+ yolov11_confidence_threshold: float,
119
+ iou_threshold: float
120
+ ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
121
+ # Validate iou_threshold before using it
122
+ if iou_threshold is None or not isinstance(iou_threshold, float):
123
+ iou_threshold = 0.3 # Default value, adjust as necessary
124
+
125
+ yolo_v8s_annotated_image = detect_and_annotate(
126
+ YOLO_V8S_MODEL, input_image, yolo_v8_confidence_threshold, iou_threshold)
127
+ yolo_v9s_annotated_image = detect_and_annotate(
128
+ YOLO_V9S_MODEL, input_image, yolo_v9_confidence_threshold, iou_threshold)
129
+ yolo_v10s_annotated_image = detect_and_annotate(
130
+ YOLO_V10S_MODEL, input_image, yolo_v10_confidence_threshold, iou_threshold)
131
+ yolo_11s_annnotated_image = detect_and_annotate(
132
+ YOLO_11S_MODEL, input_image, yolov11_confidence_threshold, iou_threshold)
133
+
134
+ return (
135
+ yolo_v8s_annotated_image,
136
+ yolo_v9s_annotated_image,
137
+ yolo_v10s_annotated_image,
138
+ yolo_11s_annnotated_image
139
+ )
140
+
141
+ yolo_v8s_confidence_threshold_component = gr.Slider(
142
+ minimum=0,
143
+ maximum=1.0,
144
+ value=0.3,
145
+ step=0.01,
146
+ label="YOLOv8s Confidence Threshold",
147
+ info=(
148
+ "The confidence threshold for the YOLO model. Lower the threshold to "
149
+ "reduce false negatives, enhancing the model's sensitivity to detect "
150
+ "sought-after objects. Conversely, increase the threshold to minimize false "
151
+ "positives, preventing the model from identifying objects it shouldn't."
152
+ ))
153
+
154
+
155
+ yolo_v9s_confidence_threshold_component = gr.Slider(
156
+ minimum=0,
157
+ maximum=1.0,
158
+ value=0.3,
159
+ step=0.01,
160
+ label="YOLOv9s Confidence Threshold",
161
+ info=(
162
+ "The confidence threshold for the YOLO model. Lower the threshold to "
163
+ "reduce false negatives, enhancing the model's sensitivity to detect "
164
+ "sought-after objects. Conversely, increase the threshold to minimize false "
165
+ "positives, preventing the model from identifying objects it shouldn't."
166
+ ))
167
+
168
+ yolo_v10s_confidence_threshold_component = gr.Slider(
169
+ minimum=0,
170
+ maximum=1.0,
171
+ value=0.3,
172
+ step=0.01,
173
+ label="YOLOv10s Confidence Threshold",
174
+ info=(
175
+ "The confidence threshold for the YOLO model. Lower the threshold to "
176
+ "reduce false negatives, enhancing the model's sensitivity to detect "
177
+ "sought-after objects. Conversely, increase the threshold to minimize false "
178
+ "positives, preventing the model from identifying objects it shouldn't."
179
+ ))
180
+
181
+ yolo_11s_confidence_threshold_component = gr.Slider(
182
+ minimum=0,
183
+ maximum=1.0,
184
+ value=0.3,
185
+ step=0.01,
186
+ label="YOLO11s Confidence Threshold",
187
+ info=(
188
+ "The confidence threshold for the YOLO model. Lower the threshold to "
189
+ "reduce false negatives, enhancing the model's sensitivity to detect "
190
+ "sought-after objects. Conversely, increase the threshold to minimize false "
191
+ "positives, preventing the model from identifying objects it shouldn't."
192
+ ))
193
+
194
+ iou_threshold_component = gr.Slider(
195
+ minimum=0,
196
+ maximum=1.0,
197
+ value=0.5,
198
+ step=0.01,
199
+ label="IoU Threshold",
200
+ info=(
201
+ "The Intersection over Union (IoU) threshold for non-maximum suppression. "
202
+ "Decrease the value to lessen the occurrence of overlapping bounding boxes, "
203
+ "making the detection process stricter. On the other hand, increase the value "
204
+ "to allow more overlapping bounding boxes, accommodating a broader range of "
205
+ "detections."
206
+ ))
207
+
208
+
209
+ with gr.Blocks() as demo:
210
+ gr.Markdown(MARKDOWN)
211
+ with gr.Accordion("Configuration", open=False):
212
+ with gr.Row():
213
+ yolo_v8s_confidence_threshold_component.render()
214
+ yolo_v9s_confidence_threshold_component.render()
215
+ yolo_v10s_confidence_threshold_component.render()
216
+ yolo_11s_confidence_threshold_component.render()
217
+ iou_threshold_component.render()
218
+ with gr.Row():
219
+ input_image_component = gr.Image(
220
+ type='pil',
221
+ label='Input'
222
+ )
223
+ with gr.Row():
224
+ yolo_v8s_output_image_component = gr.Image(
225
+ type='pil',
226
+ label='YOLOv8s'
227
+ )
228
+ yolo_v9s_output_image_component = gr.Image(
229
+ type='pil',
230
+ label='YOLOv9s'
231
+ )
232
+ with gr.Row():
233
+ yolo_v10s_output_image_component = gr.Image(
234
+ type='pil',
235
+ label='YOLOv10s'
236
+ )
237
+ yolo_11s_output_image_component = gr.Image(
238
+ type='pil',
239
+ label='YOLO11s'
240
+ )
241
+ submit_button_component = gr.Button(
242
+ value='Submit',
243
+ scale=1,
244
+ variant='primary'
245
+ )
246
+ gr.Examples(
247
+ fn=process_image,
248
+ examples=IMAGE_EXAMPLES,
249
+ inputs=[
250
+ input_image_component,
251
+ yolo_v8s_confidence_threshold_component,
252
+ yolo_v9s_confidence_threshold_component,
253
+ yolo_v10s_confidence_threshold_component,
254
+ yolo_11s_confidence_threshold_component,
255
+ iou_threshold_component
256
+ ],
257
+ outputs=[
258
+ yolo_v8s_output_image_component,
259
+ yolo_v9s_output_image_component,
260
+ yolo_v10s_output_image_component,
261
+ yolo_11s_output_image_component
262
+ ]
263
+ )
264
+
265
+ submit_button_component.click(
266
+ fn=process_image,
267
+ inputs=[
268
+ input_image_component,
269
+ yolo_v8s_confidence_threshold_component,
270
+ yolo_v9s_confidence_threshold_component,
271
+ yolo_v10s_confidence_threshold_component,
272
+ yolo_11s_confidence_threshold_component,
273
+ iou_threshold_component
274
+ ],
275
+ outputs=[
276
+ yolo_v8s_output_image_component,
277
+ yolo_v9s_output_image_component,
278
+ yolo_v10s_output_image_component,
279
+ yolo_11s_output_image_component
280
+ ]
281
+ )
282
+
283
+ demo.launch(debug=False, show_error=True, max_threads=1)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ setuptools<70.0.0
2
+ awscli==1.29.54
3
+ gradio
4
+ pydantic==2.10.6
5
+ inference
6
+ supervision
7
+ ultralytics
8
+ dill
9
+ timm