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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os # added for cache_examples
|
2 |
+
from pathlib import Path
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import superverse as sv
|
7 |
+
from gradio import ColorPicker
|
8 |
+
from PIL import Image
|
9 |
+
from torch import cuda, device
|
10 |
+
from ultralytics import YOLO
|
11 |
+
|
12 |
+
# Use GPU if available
|
13 |
+
if cuda.is_available():
|
14 |
+
device = device("cuda")
|
15 |
+
else:
|
16 |
+
device = device("cpu")
|
17 |
+
|
18 |
+
|
19 |
+
TITLE = """<h1 align="center">Superverse Annotator Playground 🚀</h1>"""
|
20 |
+
SUBTITLE = """<h2 align="center">Experiment with Superverse Annotators</h2>"""
|
21 |
+
BANNER = """
|
22 |
+
|
23 |
+
""" # noqa: E501 title/docs
|
24 |
+
DESC = """
|
25 |
+
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
|
26 |
+
<a href="https://huggingface.co/spaces/Khulnasoft/Annotators?duplicate=true">
|
27 |
+
<img src="https://bit.ly/3gLdBN6" alt="Duplicate Space" style="margin-right: 10px;">
|
28 |
+
</a>
|
29 |
+
<a href="https://github.com/khulnasoft/superverse">
|
30 |
+
<img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/khulnasoft/superverse"
|
31 |
+
style="margin-right: 10px;">
|
32 |
+
</a>
|
33 |
+
<a href="https://colab.research.google.com/github/khulnasoft/superverse/blob/main/demo.ipynb">
|
34 |
+
<img alt="Open In Colab" src="https://colab.research.google.com/assets/colab-badge.svg"
|
35 |
+
style="margin-right: 10px;">
|
36 |
+
</a>
|
37 |
+
</div>
|
38 |
+
""" # noqa: E501 title/docs
|
39 |
+
|
40 |
+
last_detections = sv.Detections.empty()
|
41 |
+
last_labels: list[str] = []
|
42 |
+
|
43 |
+
|
44 |
+
def load_model(img, model: str | Path = "yolov8s-seg.pt"):
|
45 |
+
# Load model, get results and return detections/labels
|
46 |
+
model = YOLO(model=model)
|
47 |
+
result = model(img, verbose=False, imgsz=1280)[0]
|
48 |
+
detections = sv.Detections.from_ultralytics(result)
|
49 |
+
labels = [
|
50 |
+
f"{model.model.names[class_id]} {confidence:.2f}"
|
51 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
52 |
+
]
|
53 |
+
|
54 |
+
return detections, labels
|
55 |
+
|
56 |
+
|
57 |
+
def calculate_crop_dim(a, b):
|
58 |
+
# Calculates the crop dimensions of the image resultant
|
59 |
+
if a > b:
|
60 |
+
width = a
|
61 |
+
height = a
|
62 |
+
else:
|
63 |
+
width = b
|
64 |
+
height = b
|
65 |
+
|
66 |
+
return width, height
|
67 |
+
|
68 |
+
|
69 |
+
def annotators(
|
70 |
+
img,
|
71 |
+
last_detections,
|
72 |
+
annotators_list,
|
73 |
+
last_labels,
|
74 |
+
colorbb,
|
75 |
+
colormask,
|
76 |
+
colorellipse,
|
77 |
+
colorbc,
|
78 |
+
colorcir,
|
79 |
+
colorlabel,
|
80 |
+
colorhalo,
|
81 |
+
colortri,
|
82 |
+
colordot,
|
83 |
+
) -> np.ndarray:
|
84 |
+
if last_detections == sv.Detections.empty():
|
85 |
+
gr.Warning("Detection is empty please add image and annotate first")
|
86 |
+
return np.zeros()
|
87 |
+
|
88 |
+
if "Blur" in annotators_list:
|
89 |
+
# Apply Blur
|
90 |
+
blur_annotator = sv.BlurAnnotator()
|
91 |
+
img = blur_annotator.annotate(img, detections=last_detections)
|
92 |
+
|
93 |
+
if "BoundingBox" in annotators_list:
|
94 |
+
# Draw Boundingbox
|
95 |
+
box_annotator = sv.BoundingBoxAnnotator(sv.Color.from_hex(str(colorbb)))
|
96 |
+
img = box_annotator.annotate(img, detections=last_detections)
|
97 |
+
|
98 |
+
if "Mask" in annotators_list:
|
99 |
+
# Draw Mask
|
100 |
+
mask_annotator = sv.MaskAnnotator(sv.Color.from_hex(str(colormask)))
|
101 |
+
img = mask_annotator.annotate(img, detections=last_detections)
|
102 |
+
|
103 |
+
if "Ellipse" in annotators_list:
|
104 |
+
# Draw Ellipse
|
105 |
+
ellipse_annotator = sv.EllipseAnnotator(sv.Color.from_hex(str(colorellipse)))
|
106 |
+
img = ellipse_annotator.annotate(img, detections=last_detections)
|
107 |
+
|
108 |
+
if "BoxCorner" in annotators_list:
|
109 |
+
# Draw Box corner
|
110 |
+
corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
|
111 |
+
img = corner_annotator.annotate(img, detections=last_detections)
|
112 |
+
|
113 |
+
if "Circle" in annotators_list:
|
114 |
+
# Draw Circle
|
115 |
+
circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
|
116 |
+
img = circle_annotator.annotate(img, detections=last_detections)
|
117 |
+
|
118 |
+
if "Label" in annotators_list:
|
119 |
+
# Draw Label
|
120 |
+
label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
|
121 |
+
label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
|
122 |
+
img = label_annotator.annotate(
|
123 |
+
img, detections=last_detections, labels=last_labels
|
124 |
+
)
|
125 |
+
|
126 |
+
if "Pixelate" in annotators_list:
|
127 |
+
# Apply PixelateAnnotator
|
128 |
+
pixelate_annotator = sv.PixelateAnnotator()
|
129 |
+
img = pixelate_annotator.annotate(img, detections=last_detections)
|
130 |
+
|
131 |
+
if "Halo" in annotators_list:
|
132 |
+
# Draw HaloAnnotator
|
133 |
+
halo_annotator = sv.HaloAnnotator(sv.Color.from_hex(str(colorhalo)))
|
134 |
+
img = halo_annotator.annotate(img, detections=last_detections)
|
135 |
+
|
136 |
+
if "HeatMap" in annotators_list:
|
137 |
+
# Draw HeatMapAnnotator
|
138 |
+
heatmap_annotator = sv.HeatMapAnnotator()
|
139 |
+
img = heatmap_annotator.annotate(img, detections=last_detections)
|
140 |
+
|
141 |
+
if "Dot" in annotators_list:
|
142 |
+
# Dot DotAnnotator
|
143 |
+
dot_annotator = sv.DotAnnotator(sv.Color.from_hex(str(colordot)))
|
144 |
+
img = dot_annotator.annotate(img, detections=last_detections)
|
145 |
+
|
146 |
+
if "Triangle" in annotators_list:
|
147 |
+
# Draw TriangleAnnotator
|
148 |
+
tri_annotator = sv.TriangleAnnotator(sv.Color.from_hex(str(colortri)))
|
149 |
+
img = tri_annotator.annotate(img, detections=last_detections)
|
150 |
+
|
151 |
+
# crop image for the largest possible square
|
152 |
+
res_img = Image.fromarray(img)
|
153 |
+
# print(type(res_img))
|
154 |
+
x = 0
|
155 |
+
y = 0
|
156 |
+
|
157 |
+
# print("size of the pil im=", res_img.size)
|
158 |
+
(v1, v2) = res_img.size
|
159 |
+
width, height = calculate_crop_dim(v1, v2)
|
160 |
+
# print(width, height)
|
161 |
+
my_img = np.array(res_img)
|
162 |
+
|
163 |
+
crop_img = my_img[y : y + height, x : x + width]
|
164 |
+
# print(type(crop_img))
|
165 |
+
|
166 |
+
return crop_img[..., ::-1].copy() # BGR to RGB using numpy
|
167 |
+
|
168 |
+
|
169 |
+
def annotator(
|
170 |
+
img,
|
171 |
+
model,
|
172 |
+
annotators_list,
|
173 |
+
colorbb,
|
174 |
+
colormask,
|
175 |
+
colorellipse,
|
176 |
+
colorbc,
|
177 |
+
colorcir,
|
178 |
+
colorlabel,
|
179 |
+
colorhalo,
|
180 |
+
colortri,
|
181 |
+
colordot,
|
182 |
+
progress=gr.Progress(track_tqdm=True),
|
183 |
+
) -> np.ndarray:
|
184 |
+
"""
|
185 |
+
Function that changes the color of annotators
|
186 |
+
Args:
|
187 |
+
annotators: Icon whose color needs to be changed.
|
188 |
+
color: Chosen color with which to edit the input icon in Hex.
|
189 |
+
img: Input image is numpy matrix in BGR.
|
190 |
+
Returns:
|
191 |
+
annotators: annotated image
|
192 |
+
"""
|
193 |
+
|
194 |
+
img = img[..., ::-1].copy() # BGR to RGB using numpy
|
195 |
+
|
196 |
+
detections, labels = load_model(img, model)
|
197 |
+
last_detections = detections
|
198 |
+
last_labels = labels
|
199 |
+
|
200 |
+
return annotators(
|
201 |
+
img,
|
202 |
+
last_detections,
|
203 |
+
annotators_list,
|
204 |
+
last_labels,
|
205 |
+
colorbb,
|
206 |
+
colormask,
|
207 |
+
colorellipse,
|
208 |
+
colorbc,
|
209 |
+
colorcir,
|
210 |
+
colorlabel,
|
211 |
+
colorhalo,
|
212 |
+
colortri,
|
213 |
+
colordot,
|
214 |
+
)
|
215 |
+
|
216 |
+
|
217 |
+
purple_theme = theme = gr.themes.Soft(primary_hue=gr.themes.colors.purple).set(
|
218 |
+
button_primary_background_fill="*primary_600",
|
219 |
+
button_primary_background_fill_hover="*primary_700",
|
220 |
+
checkbox_label_background_fill_selected="*primary_600",
|
221 |
+
checkbox_background_color_selected="*primary_400",
|
222 |
+
)
|
223 |
+
|
224 |
+
with gr.Blocks(theme=purple_theme) as app:
|
225 |
+
gr.HTML(TITLE)
|
226 |
+
gr.HTML(SUBTITLE)
|
227 |
+
gr.HTML(BANNER)
|
228 |
+
gr.HTML(DESC)
|
229 |
+
|
230 |
+
models = gr.Dropdown(
|
231 |
+
[
|
232 |
+
"yolov8n-seg.pt",
|
233 |
+
"yolov8s-seg.pt",
|
234 |
+
"yolov8m-seg.pt",
|
235 |
+
"yolov8l-seg.pt",
|
236 |
+
"yolov8x-seg.pt",
|
237 |
+
],
|
238 |
+
type="value",
|
239 |
+
value="yolov8s-seg.pt",
|
240 |
+
label="Select Model:",
|
241 |
+
)
|
242 |
+
|
243 |
+
annotators_list = gr.CheckboxGroup(
|
244 |
+
choices=[
|
245 |
+
"BoundingBox",
|
246 |
+
"Mask",
|
247 |
+
"Halo",
|
248 |
+
"Ellipse",
|
249 |
+
"BoxCorner",
|
250 |
+
"Circle",
|
251 |
+
"Label",
|
252 |
+
"Blur",
|
253 |
+
"Pixelate",
|
254 |
+
"HeatMap",
|
255 |
+
"Dot",
|
256 |
+
"Triangle",
|
257 |
+
],
|
258 |
+
value=["BoundingBox", "Mask"],
|
259 |
+
label="Select Annotators:",
|
260 |
+
)
|
261 |
+
|
262 |
+
gr.Markdown("## Color Picker 🎨")
|
263 |
+
with gr.Row(variant="panel"):
|
264 |
+
with gr.Column():
|
265 |
+
colorbb = gr.ColorPicker(value="#A351FB", label="BoundingBox")
|
266 |
+
colormask = gr.ColorPicker(value="#A351FB", label="Mask")
|
267 |
+
colorellipse = gr.ColorPicker(value="#A351FB", label="Ellipse")
|
268 |
+
with gr.Column():
|
269 |
+
colorbc = gr.ColorPicker(value="#A351FB", label="BoxCorner")
|
270 |
+
colorcir = gr.ColorPicker(value="#A351FB", label="Circle")
|
271 |
+
colorlabel = gr.ColorPicker(value="#A351FB", label="Label")
|
272 |
+
with gr.Column():
|
273 |
+
colorhalo = gr.ColorPicker(value="#A351FB", label="Halo")
|
274 |
+
colordot = gr.ColorPicker(value="#A351FB", label="Dot")
|
275 |
+
colortri = gr.ColorPicker(value="#A351FB", label="Triangle")
|
276 |
+
|
277 |
+
with gr.Row():
|
278 |
+
with gr.Column():
|
279 |
+
with gr.Tab("Input image"):
|
280 |
+
image_input = gr.Image(type="numpy", show_label=False)
|
281 |
+
with gr.Column():
|
282 |
+
with gr.Tab("Result image"):
|
283 |
+
image_output = gr.Image(type="numpy", show_label=False)
|
284 |
+
image_button = gr.Button(value="Annotate it!", variant="primary")
|
285 |
+
|
286 |
+
image_button.click(
|
287 |
+
annotator,
|
288 |
+
inputs=[
|
289 |
+
image_input,
|
290 |
+
models,
|
291 |
+
annotators_list,
|
292 |
+
colorbb,
|
293 |
+
colormask,
|
294 |
+
colorellipse,
|
295 |
+
colorbc,
|
296 |
+
colorcir,
|
297 |
+
colorlabel,
|
298 |
+
colorhalo,
|
299 |
+
colortri,
|
300 |
+
colordot,
|
301 |
+
],
|
302 |
+
outputs=image_output,
|
303 |
+
)
|
304 |
+
|
305 |
+
gr.Markdown("## Image Examples 🖼️")
|
306 |
+
gr.Examples(
|
307 |
+
examples=[
|
308 |
+
os.path.join(os.path.abspath(""), "./assets/city.jpg"),
|
309 |
+
os.path.join(os.path.abspath(""), "./assets/household.jpg"),
|
310 |
+
os.path.join(os.path.abspath(""), "./assets/industry.jpg"),
|
311 |
+
os.path.join(os.path.abspath(""), "./assets/retail.jpg"),
|
312 |
+
os.path.join(os.path.abspath(""), "./assets/aerodefence.jpg"),
|
313 |
+
],
|
314 |
+
inputs=image_input,
|
315 |
+
outputs=image_output,
|
316 |
+
fn=annotator,
|
317 |
+
cache_examples=False,
|
318 |
+
)
|
319 |
+
|
320 |
+
annotators_list.change(
|
321 |
+
fn=annotator,
|
322 |
+
inputs=[
|
323 |
+
image_input,
|
324 |
+
models,
|
325 |
+
annotators_list,
|
326 |
+
colorbb,
|
327 |
+
colormask,
|
328 |
+
colorellipse,
|
329 |
+
colorbc,
|
330 |
+
colorcir,
|
331 |
+
colorlabel,
|
332 |
+
colorhalo,
|
333 |
+
colortri,
|
334 |
+
colordot,
|
335 |
+
],
|
336 |
+
outputs=image_output,
|
337 |
+
)
|
338 |
+
|
339 |
+
def change_color(color: ColorPicker):
|
340 |
+
color.change(
|
341 |
+
fn=annotator,
|
342 |
+
inputs=[
|
343 |
+
image_input,
|
344 |
+
models,
|
345 |
+
annotators_list,
|
346 |
+
colorbb,
|
347 |
+
colormask,
|
348 |
+
colorellipse,
|
349 |
+
colorbc,
|
350 |
+
colorcir,
|
351 |
+
colorlabel,
|
352 |
+
colorhalo,
|
353 |
+
colortri,
|
354 |
+
colordot,
|
355 |
+
],
|
356 |
+
outputs=image_output,
|
357 |
+
)
|
358 |
+
|
359 |
+
colors = [
|
360 |
+
colorbb,
|
361 |
+
colormask,
|
362 |
+
colorellipse,
|
363 |
+
colorbc,
|
364 |
+
colorcir,
|
365 |
+
colorlabel,
|
366 |
+
colorhalo,
|
367 |
+
colortri,
|
368 |
+
colordot,
|
369 |
+
]
|
370 |
+
|
371 |
+
for color in colors:
|
372 |
+
change_color(color)
|
373 |
+
|
374 |
+
|
375 |
+
if __name__ == "__main__":
|
376 |
+
print("Starting app...")
|
377 |
+
print("Dark theme is available at: http://localhost:7860/?__theme=dark")
|
378 |
+
# app.launch(debug=False, server_name="0.0.0.0") # for local network
|
379 |
+
app.launch(debug=False)
|