mbar0075 commited on
Commit
007a1c8
·
1 Parent(s): d1131db

Initial Commit

Browse files
Files changed (4) hide show
  1. README.md +5 -5
  2. app.py +250 -0
  3. requirements.txt +9 -0
  4. yolov9e-seg.pt +3 -0
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: Segmentation Playground
3
- emoji: 📊
4
- colorFrom: red
5
- colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 5.25.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
1
  ---
2
+ title: Segmentation-Playground
3
+ emoji: 🖼️
4
+ colorFrom: green
5
+ colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.39.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple
2
+
3
+ import gradio as gr
4
+ import numpy as np
5
+ import supervision as sv
6
+ from inference import get_model
7
+ import warnings
8
+ from ultralytics import YOLO
9
+
10
+ warnings.filterwarnings("ignore")
11
+
12
+ MARKDOWN = """
13
+ <h1 style='text-align: left'>Segmentation-Playground 🖼️</h1>
14
+ Welcome to Segmentation-Playground! This demo showcases the segmentation capabilities of various YOLO models pre-trained on the COCO Dataset. 🚀🔍👀
15
+
16
+ A simple project just for fun for on the go instance segmentation. 🎉
17
+
18
+ Inspired from YOLO-ARENA by SkalskiP. 🙏
19
+
20
+ - **YOLOv8**
21
+ <div style="display: flex; align-items: center;">
22
+ <a href="https://docs.ultralytics.com/models/yolov8/" style="margin-right: 10px;">
23
+ <img src="https://badges.aleen42.com/src/github.svg">
24
+ </a>
25
+ <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;">
26
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
27
+ </a>
28
+ </div>
29
+ - **YOLOv9**
30
+ <div style="display: flex; align-items: center;">
31
+ <a href="https://github.com/WongKinYiu/yolov9" style="margin-right: 10px;">
32
+ <img src="https://badges.aleen42.com/src/github.svg">
33
+ </a>
34
+ <a href="https://arxiv.org/abs/2402.13616" style="margin-right: 10px;">
35
+ <img src="https://img.shields.io/badge/arXiv-2402.13616-b31b1b.svg">
36
+ </a>
37
+ <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;">
38
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
39
+ </a>
40
+ </div>
41
+ - **YOLO11**
42
+ <div style="display: flex; align-items: center;">
43
+ <a href="https://docs.ultralytics.com/models/yolo11/" style="margin-right: 10px;">
44
+ <img src="https://badges.aleen42.com/src/github.svg">
45
+ </a>
46
+ <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;">
47
+ <img src="https://colab.research.google.com/assets/colab-badge.svg">
48
+ </a>
49
+ </div>
50
+
51
+ Powered by Roboflow [Inference](https://github.com/roboflow/inference),
52
+ [Supervision](https://github.com/roboflow/supervision) and [Ultralytics](https://github.com/ultralytics/ultralytics).🔥
53
+ """
54
+
55
+ IMAGE_EXAMPLES = [
56
+ ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, 0.3, 0.3, 0.5],
57
+ ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, 0.3, 0.3, 0.5],
58
+ ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 0.3, 0.3, 0.3, 0.5],
59
+ ]
60
+
61
+ YOLO_V8S_MODEL = YOLO("yolov8m-seg.pt")
62
+ YOLO_V9S_MODEL = YOLO("https://huggingface.co/spaces/mbar0075/Segmentation-Playground/resolve/main/yolov9e-seg.pt")
63
+ YOLO_11S_MODEL = YOLO("yolo11m-seg.pt")
64
+
65
+ LABEL_ANNOTATORS = sv.LabelAnnotator()
66
+ MASK_ANNOTATORS = sv.MaskAnnotator()
67
+ BOUNDING_BOX_ANNOTATORS = sv.BoxAnnotator()
68
+
69
+
70
+ def detect_and_annotate(
71
+ model,
72
+ input_image: np.ndarray,
73
+ confidence_threshold: float,
74
+ iou_threshold: float,
75
+ class_id_mapping: dict = None
76
+ ) -> np.ndarray:
77
+ result = model(
78
+ input_image,
79
+ conf=confidence_threshold,
80
+ iou=iou_threshold
81
+ )[0]
82
+ detections = sv.Detections.from_ultralytics(result)
83
+
84
+ if class_id_mapping:
85
+ detections.class_id = np.array([
86
+ class_id_mapping[class_id]
87
+ for class_id
88
+ in detections.class_id
89
+ ])
90
+
91
+ labels = [
92
+ f"{class_name} ({confidence:.2f})"
93
+ for class_name, confidence
94
+ in zip(detections['class_name'], detections.confidence)
95
+ ]
96
+
97
+ annotated_image = input_image.copy()
98
+ annotated_image = MASK_ANNOTATORS.annotate(
99
+ scene=annotated_image, detections=detections)
100
+ annotated_image = BOUNDING_BOX_ANNOTATORS.annotate(
101
+ scene=annotated_image, detections=detections)
102
+ annotated_image = LABEL_ANNOTATORS.annotate(
103
+ scene=annotated_image, detections=detections, labels=labels)
104
+ return annotated_image
105
+
106
+
107
+ def process_image(
108
+ input_image: np.ndarray,
109
+ yolo_v8_confidence_threshold: float,
110
+ yolo_v9_confidence_threshold: float,
111
+ yolo_v10_confidence_threshold: float,
112
+ iou_threshold: float
113
+ ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
114
+ # Validate iou_threshold before using it
115
+ if iou_threshold is None or not isinstance(iou_threshold, float):
116
+ iou_threshold = 0.3 # Default value, adjust as necessary
117
+
118
+ yolo_v8n_annotated_image = detect_and_annotate(
119
+ YOLO_V8S_MODEL, input_image, yolo_v8_confidence_threshold, iou_threshold)
120
+ yolo_v8s_annotated_image = detect_and_annotate(
121
+ YOLO_V9S_MODEL, input_image, yolo_v9_confidence_threshold, iou_threshold)
122
+ yolo_8m_annotated_image = detect_and_annotate(
123
+ YOLO_11S_MODEL, input_image, yolo_v10_confidence_threshold, iou_threshold)
124
+
125
+ return (
126
+ yolo_v8n_annotated_image,
127
+ yolo_v8s_annotated_image,
128
+ yolo_8m_annotated_image
129
+ )
130
+
131
+
132
+ yolo_v8N_confidence_threshold_component = gr.Slider(
133
+ minimum=0,
134
+ maximum=1.0,
135
+ value=0.3,
136
+ step=0.01,
137
+ label="YOLOv8m Confidence Threshold",
138
+ info=(
139
+ "The confidence threshold for the YOLO model. Lower the threshold to "
140
+ "reduce false negatives, enhancing the model's sensitivity to detect "
141
+ "sought-after objects. Conversely, increase the threshold to minimize false "
142
+ "positives, preventing the model from identifying objects it shouldn't."
143
+ ))
144
+
145
+ yolo_v8S_confidence_threshold_component = gr.Slider(
146
+ minimum=0,
147
+ maximum=1.0,
148
+ value=0.3,
149
+ step=0.01,
150
+ label="YOLOv9e Confidence Threshold",
151
+ info=(
152
+ "The confidence threshold for the YOLO model. Lower the threshold to "
153
+ "reduce false negatives, enhancing the model's sensitivity to detect "
154
+ "sought-after objects. Conversely, increase the threshold to minimize false "
155
+ "positives, preventing the model from identifying objects it shouldn't."
156
+ ))
157
+
158
+ yolo_v8M_confidence_threshold_component = gr.Slider(
159
+ minimum=0,
160
+ maximum=1.0,
161
+ value=0.3,
162
+ step=0.01,
163
+ label="YOLO11m Confidence Threshold",
164
+ info=(
165
+ "The confidence threshold for the YOLO model. Lower the threshold to "
166
+ "reduce false negatives, enhancing the model's sensitivity to detect "
167
+ "sought-after objects. Conversely, increase the threshold to minimize false "
168
+ "positives, preventing the model from identifying objects it shouldn't."
169
+ ))
170
+
171
+ iou_threshold_component = gr.Slider(
172
+ minimum=0,
173
+ maximum=1.0,
174
+ value=0.5,
175
+ step=0.01,
176
+ label="IoU Threshold",
177
+ info=(
178
+ "The Intersection over Union (IoU) threshold for non-maximum suppression. "
179
+ "Decrease the value to lessen the occurrence of overlapping bounding boxes, "
180
+ "making the detection process stricter. On the other hand, increase the value "
181
+ "to allow more overlapping bounding boxes, accommodating a broader range of "
182
+ "detections."
183
+ ))
184
+
185
+
186
+ with gr.Blocks() as demo:
187
+ gr.Markdown(MARKDOWN)
188
+ with gr.Accordion("Configuration", open=False):
189
+ with gr.Row():
190
+ yolo_v8N_confidence_threshold_component.render()
191
+ yolo_v8S_confidence_threshold_component.render()
192
+ yolo_v8M_confidence_threshold_component.render()
193
+ iou_threshold_component.render()
194
+ with gr.Row():
195
+ input_image_component = gr.Image(
196
+ type='pil',
197
+ label='Input'
198
+ )
199
+ yolo_v8n_output_image_component = gr.Image(
200
+ type='pil',
201
+ label='YOLOv8m'
202
+ )
203
+ with gr.Row():
204
+ yolo_v8s_output_image_component = gr.Image(
205
+ type='pil',
206
+ label='YOLOv9e'
207
+ )
208
+ yolo_v8m_output_image_component = gr.Image(
209
+ type='pil',
210
+ label='YOLO11m'
211
+ )
212
+ submit_button_component = gr.Button(
213
+ value='Submit',
214
+ scale=1,
215
+ variant='primary'
216
+ )
217
+ gr.Examples(
218
+ fn=process_image,
219
+ examples=IMAGE_EXAMPLES,
220
+ inputs=[
221
+ input_image_component,
222
+ yolo_v8N_confidence_threshold_component,
223
+ yolo_v8S_confidence_threshold_component,
224
+ yolo_v8M_confidence_threshold_component,
225
+ iou_threshold_component
226
+ ],
227
+ outputs=[
228
+ yolo_v8n_output_image_component,
229
+ yolo_v8s_output_image_component,
230
+ yolo_v8m_output_image_component
231
+ ]
232
+ )
233
+
234
+ submit_button_component.click(
235
+ fn=process_image,
236
+ inputs=[
237
+ input_image_component,
238
+ yolo_v8N_confidence_threshold_component,
239
+ yolo_v8S_confidence_threshold_component,
240
+ yolo_v8M_confidence_threshold_component,
241
+ iou_threshold_component
242
+ ],
243
+ outputs=[
244
+ yolo_v8n_output_image_component,
245
+ yolo_v8s_output_image_component,
246
+ yolo_v8m_output_image_component
247
+ ]
248
+ )
249
+
250
+ 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==5.23.2
4
+ ultralytics==8.3.13
5
+ pydantic==2.10.6
6
+ inference
7
+ supervision
8
+ dill
9
+ timm
yolov9e-seg.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ac1d634a0cb9997c9edcdd1fae814d291c4fc4efd95a203a12f9ec36338d9e1
3
+ size 122210649