Spaces:
Runtime error
Runtime error
feat: ✨ new annotators added from supervision
Browse filesSigned-off-by: Onuralp SEZER <thunderbirdtr@fedoraproject.org>
- .gitignore +9 -0
- app.py +158 -76
.gitignore
CHANGED
|
@@ -158,3 +158,12 @@ cython_debug/
|
|
| 158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
#.idea/
|
| 161 |
+
|
| 162 |
+
# Mac
|
| 163 |
+
.DS_Store
|
| 164 |
+
.AppleDouble
|
| 165 |
+
.LSOverride
|
| 166 |
+
|
| 167 |
+
# YoloV8 files
|
| 168 |
+
yolov8s-seg.pt
|
| 169 |
+
yolov8s.pt
|
app.py
CHANGED
|
@@ -1,35 +1,57 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import supervision as sv
|
|
|
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
import os #added for cache_examples
|
| 5 |
-
from PIL import Image, ImageColor
|
| 6 |
-
import numpy as np
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
labels = [
|
| 14 |
-
f"{model.model.names[class_id]} {confidence:.2f}"
|
| 15 |
-
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 16 |
-
]
|
| 17 |
-
|
| 18 |
-
return detections, labels
|
| 19 |
-
|
| 20 |
-
def calculate_crop_dim(a,b):
|
| 21 |
-
#Calculates the crop dimensions of the image resultant
|
| 22 |
-
if a>b:
|
| 23 |
-
width= a
|
| 24 |
-
height = a
|
| 25 |
-
else:
|
| 26 |
-
width = b
|
| 27 |
-
height = b
|
| 28 |
-
|
| 29 |
-
return width, height
|
| 30 |
-
|
| 31 |
-
def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel):
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
Function that changes the color of annotators
|
| 35 |
Args:
|
|
@@ -40,14 +62,10 @@ def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,col
|
|
| 40 |
annotators: annotated image
|
| 41 |
"""
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
img = img[...,::-1].copy() # BGR to RGB using numpy
|
| 46 |
-
|
| 47 |
|
| 48 |
detections, labels = load_model(img)
|
| 49 |
|
| 50 |
-
|
| 51 |
if "Blur" in annotators:
|
| 52 |
# Apply Blur
|
| 53 |
blur_annotator = sv.BlurAnnotator()
|
|
@@ -73,8 +91,7 @@ def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,col
|
|
| 73 |
corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
|
| 74 |
img = corner_annotator.annotate(img, detections=detections)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
if "Circle" in annotators:
|
| 78 |
# Draw circle
|
| 79 |
circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
|
| 80 |
img = circle_annotator.annotate(img, detections=detections)
|
|
@@ -85,71 +102,133 @@ def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,col
|
|
| 85 |
label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
|
| 86 |
img = label_annotator.annotate(img, detections=detections, labels=labels)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
print("size of the pil im=", res_img.size)
|
| 99 |
-
(v1,v2) = res_img.size
|
| 100 |
width, height = calculate_crop_dim(v1, v2)
|
| 101 |
-
print(width, height)
|
| 102 |
my_img = np.array(res_img)
|
| 103 |
|
| 104 |
-
crop_img = my_img[y:y+height, x:x+width]
|
| 105 |
-
print(type(crop_img))
|
| 106 |
|
| 107 |
-
return crop_img[
|
| 108 |
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 117 |
gr.Markdown("""# Supervision Annotators""")
|
| 118 |
-
annotators = gr.CheckboxGroup(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
with gr.Column():
|
| 123 |
-
|
| 124 |
with gr.Column():
|
| 125 |
-
|
| 126 |
with gr.Column():
|
| 127 |
-
|
| 128 |
with gr.Column():
|
| 129 |
-
|
| 130 |
with gr.Column():
|
| 131 |
-
|
| 132 |
with gr.Column():
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
with gr.Row():
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
image_button = gr.Button(value="Annotate it!", variant="primary")
|
| 143 |
|
| 144 |
-
image_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
gr.Markdown("## Image Examples")
|
| 147 |
gr.Examples(
|
| 148 |
-
examples=[
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
inputs=image_input,
|
| 154 |
outputs=image_output,
|
| 155 |
fn=annotator,
|
|
@@ -157,4 +236,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.purple)
|
|
| 157 |
)
|
| 158 |
|
| 159 |
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os # added for cache_examples
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
import supervision as sv
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from torch import cuda, device
|
| 8 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Use GPU if available
|
| 11 |
+
if cuda.is_available():
|
| 12 |
+
device = device("cuda")
|
| 13 |
+
else:
|
| 14 |
+
device = device("cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
|
| 17 |
+
def load_model(img):
|
| 18 |
+
# Load model, get results and return detections/labels
|
| 19 |
+
model = YOLO("yolov8s-seg.pt")
|
| 20 |
+
result = model(img, verbose=False, imgsz=1280)[0]
|
| 21 |
+
detections = sv.Detections.from_ultralytics(result)
|
| 22 |
+
labels = [
|
| 23 |
+
f"{model.model.names[class_id]} {confidence:.2f}"
|
| 24 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
print(labels)
|
| 28 |
+
return detections, labels
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def calculate_crop_dim(a, b):
|
| 32 |
+
# Calculates the crop dimensions of the image resultant
|
| 33 |
+
if a > b:
|
| 34 |
+
width = a
|
| 35 |
+
height = a
|
| 36 |
+
else:
|
| 37 |
+
width = b
|
| 38 |
+
height = b
|
| 39 |
+
|
| 40 |
+
return width, height
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def annotator(
|
| 44 |
+
img,
|
| 45 |
+
annotators,
|
| 46 |
+
colorbb,
|
| 47 |
+
colormask,
|
| 48 |
+
colorellipse,
|
| 49 |
+
colorbc,
|
| 50 |
+
colorcir,
|
| 51 |
+
colorlabel,
|
| 52 |
+
colorhalo,
|
| 53 |
+
colortri
|
| 54 |
+
):
|
| 55 |
"""
|
| 56 |
Function that changes the color of annotators
|
| 57 |
Args:
|
|
|
|
| 62 |
annotators: annotated image
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
img = img[..., ::-1].copy() # BGR to RGB using numpy
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
detections, labels = load_model(img)
|
| 68 |
|
|
|
|
| 69 |
if "Blur" in annotators:
|
| 70 |
# Apply Blur
|
| 71 |
blur_annotator = sv.BlurAnnotator()
|
|
|
|
| 91 |
corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
|
| 92 |
img = corner_annotator.annotate(img, detections=detections)
|
| 93 |
|
| 94 |
+
if "Circle" in annotators:
|
|
|
|
| 95 |
# Draw circle
|
| 96 |
circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
|
| 97 |
img = circle_annotator.annotate(img, detections=detections)
|
|
|
|
| 102 |
label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
|
| 103 |
img = label_annotator.annotate(img, detections=detections, labels=labels)
|
| 104 |
|
| 105 |
+
if "Pixelate" in annotators:
|
| 106 |
+
# Draw PixelateAnnotator
|
| 107 |
+
pixelate_annotator = sv.PixelateAnnotator()
|
| 108 |
+
img = pixelate_annotator.annotate(img, detections=detections)
|
| 109 |
|
| 110 |
+
if "Halo" in annotators:
|
| 111 |
+
# Draw HaloAnnotator
|
| 112 |
+
halo_annotator = sv.HaloAnnotator(sv.Color.from_hex(str(colorhalo)))
|
| 113 |
+
img = halo_annotator.annotate(img, detections=detections)
|
| 114 |
|
| 115 |
+
if "HeatMap" in annotators:
|
| 116 |
+
# Draw HeatMapAnnotator
|
| 117 |
+
heatmap_annotator = sv.HeatMapAnnotator()
|
| 118 |
+
img = heatmap_annotator.annotate(img, detections=detections)
|
| 119 |
+
|
| 120 |
+
if "Dot" in annotators:
|
| 121 |
+
# Draw DotAnnotator
|
| 122 |
+
dot_annotator = sv.DotAnnotator()
|
| 123 |
+
img = dot_annotator.annotate(img, detections=detections)
|
| 124 |
+
|
| 125 |
+
if "Triangle" in annotators:
|
| 126 |
+
# Draw TriangleAnnotator
|
| 127 |
+
tri_annotator = sv.TriangleAnnotator(sv.Color.from_hex(str(colortri)))
|
| 128 |
+
img = tri_annotator.annotate(img, detections=detections)
|
| 129 |
|
| 130 |
+
# crop image for the largest possible square
|
| 131 |
+
res_img = Image.fromarray(img)
|
| 132 |
+
# print(type(res_img))
|
| 133 |
+
x = 0
|
| 134 |
+
y = 0
|
| 135 |
|
| 136 |
+
# print("size of the pil im=", res_img.size)
|
| 137 |
+
(v1, v2) = res_img.size
|
| 138 |
width, height = calculate_crop_dim(v1, v2)
|
| 139 |
+
# print(width, height)
|
| 140 |
my_img = np.array(res_img)
|
| 141 |
|
| 142 |
+
crop_img = my_img[y : y + height, x : x + width]
|
| 143 |
+
# print(type(crop_img))
|
| 144 |
|
| 145 |
+
return crop_img[..., ::-1].copy() # BGR to RGB using numpy
|
| 146 |
|
| 147 |
|
| 148 |
+
purple_theme = theme = gr.themes.Soft(primary_hue=gr.themes.colors.purple).set(
|
| 149 |
+
button_primary_background_fill="*primary_600",
|
| 150 |
+
button_primary_background_fill_hover="*primary_700",
|
| 151 |
+
checkbox_label_background_fill_selected="*primary_600",
|
| 152 |
+
checkbox_background_color_selected="*primary_400",
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
with gr.Blocks(theme=purple_theme) as app:
|
| 156 |
gr.Markdown("""# Supervision Annotators""")
|
| 157 |
+
annotators = gr.CheckboxGroup(
|
| 158 |
+
choices=[
|
| 159 |
+
"BoundingBox",
|
| 160 |
+
"Mask",
|
| 161 |
+
"Halo",
|
| 162 |
+
"Ellipse",
|
| 163 |
+
"BoxCorner",
|
| 164 |
+
"Circle",
|
| 165 |
+
"Label",
|
| 166 |
+
"Blur",
|
| 167 |
+
"Pixelate",
|
| 168 |
+
"HeatMap",
|
| 169 |
+
"Dot",
|
| 170 |
+
"Triangle"
|
| 171 |
+
],
|
| 172 |
+
value=["BoundingBox", "Mask"],
|
| 173 |
+
label="Select Annotators:",
|
| 174 |
+
)
|
| 175 |
|
| 176 |
+
gr.Markdown("🎨 **Color Picker**")
|
| 177 |
+
with gr.Row(variant="compact"):
|
| 178 |
with gr.Column():
|
| 179 |
+
colorbb = gr.ColorPicker(value="#A351FB", label="BoundingBox")
|
| 180 |
with gr.Column():
|
| 181 |
+
colormask = gr.ColorPicker(value="#A351FB", label="Mask")
|
| 182 |
with gr.Column():
|
| 183 |
+
colorellipse = gr.ColorPicker(value="#A351FB", label="Ellipse")
|
| 184 |
with gr.Column():
|
| 185 |
+
colorbc = gr.ColorPicker(value="#A351FB", label="BoxCorner")
|
| 186 |
with gr.Column():
|
| 187 |
+
colorcir = gr.ColorPicker(value="#A351FB", label="Circle")
|
| 188 |
with gr.Column():
|
| 189 |
+
colorlabel = gr.ColorPicker(value="#A351FB", label="Label")
|
| 190 |
+
with gr.Column():
|
| 191 |
+
colorhalo = gr.ColorPicker(value="#A351FB", label="Halo")
|
| 192 |
+
with gr.Column():
|
| 193 |
+
colordot = gr.ColorPicker(value="#A351FB", label="Dot")
|
| 194 |
+
with gr.Column():
|
| 195 |
+
colortri = gr.ColorPicker(value="#A351FB", label="Triangle")
|
| 196 |
+
|
| 197 |
with gr.Row():
|
| 198 |
+
with gr.Column():
|
| 199 |
+
with gr.Tab("Input image"):
|
| 200 |
+
image_input = gr.Image(type="numpy", show_label=False)
|
| 201 |
+
with gr.Column():
|
| 202 |
+
with gr.Tab("Result image"):
|
| 203 |
+
image_output = gr.Image(type="numpy", show_label=False)
|
| 204 |
image_button = gr.Button(value="Annotate it!", variant="primary")
|
| 205 |
|
| 206 |
+
image_button.click(
|
| 207 |
+
annotator,
|
| 208 |
+
inputs=[
|
| 209 |
+
image_input,
|
| 210 |
+
annotators,
|
| 211 |
+
colorbb,
|
| 212 |
+
colormask,
|
| 213 |
+
colorellipse,
|
| 214 |
+
colorbc,
|
| 215 |
+
colorcir,
|
| 216 |
+
colorlabel,
|
| 217 |
+
colorhalo,
|
| 218 |
+
colortri,
|
| 219 |
+
],
|
| 220 |
+
outputs=image_output,
|
| 221 |
+
)
|
| 222 |
|
| 223 |
gr.Markdown("## Image Examples")
|
| 224 |
gr.Examples(
|
| 225 |
+
examples=[
|
| 226 |
+
os.path.join(os.path.abspath(""), "city.jpg"),
|
| 227 |
+
os.path.join(os.path.abspath(""), "household.jpg"),
|
| 228 |
+
os.path.join(os.path.abspath(""), "industry.jpg"),
|
| 229 |
+
os.path.join(os.path.abspath(""), "retail.jpg"),
|
| 230 |
+
os.path.join(os.path.abspath(""), "aerodefence.jpg"),
|
| 231 |
+
],
|
| 232 |
inputs=image_input,
|
| 233 |
outputs=image_output,
|
| 234 |
fn=annotator,
|
|
|
|
| 236 |
)
|
| 237 |
|
| 238 |
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
+
print("Starting app...")
|
| 241 |
+
print("Dark theme is available at: http://localhost:7860/?__theme=dark")
|
| 242 |
+
app.launch(debug=False)
|