-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbox_visualizer.py
358 lines (301 loc) · 12.8 KB
/
bbox_visualizer.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import cv2
def draw_rectangle(img,
bbox,
bbox_color=(255, 255, 255),
thickness=3,
is_opaque=False,
alpha=0.5):
"""Draws the rectangle around the object
Parameters
----------
img : ndarray
the actual image
bbox : list
a list containing x_min, y_min, x_max and y_max of the rectangle positions
bbox_color : tuple, optional
the color of the box, by default (255,255,255)
thickness : int, optional
thickness of the outline of the box, by default 3
is_opaque : bool, optional
if False, draws a solid rectangular outline. Else, a filled rectangle which is semi transparent, by default False
alpha : float, optional
strength of the opacity, by default 0.5
Returns
-------
ndarray
the image with the bounding box drawn
"""
output = img.copy()
if not is_opaque:
cv2.rectangle(output, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
bbox_color, thickness)
else:
overlay = img.copy()
cv2.rectangle(overlay, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
bbox_color, -1)
cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)
return output
def add_label_to_rectangle(img,
label,
bbox,
draw_bg=True,
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0),
top=True):
"""adds label, inside or outside the rectangle
Parameters
----------
img : ndarray
the image on which the label is to be written, preferably the image with the rectangular bounding box drawn
label : str
the text (label) to be written
bbox : list
a list containing x_min, y_min, x_max and y_max of the rectangle positions
draw_bg : bool, optional
if True, draws the background of the text, else just the text is written, by default True
text_bg_color : tuple, optional
the background color of the label that is filled, by default (255, 255, 255)
text_color : tuple, optional
color of the text (label) to be written, by default (0, 0, 0)
top : bool, optional
if True, writes the label on top of the bounding box, else inside, by default True
Returns
-------
ndarray
the image with the label written
"""
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][0]
if top:
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] - 30]
if draw_bg:
cv2.rectangle(img, (label_bg[0], label_bg[1]),
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - 5),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
else:
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] + 30]
if draw_bg:
cv2.rectangle(img, (label_bg[0], label_bg[1]),
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - 5 + 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
return img
def add_T_label_to_rectangle(img,
label,
bbox,
draw_bg=True,
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0)):
"""adds a T label to the rectangle, originating from the top of the rectangle
Parameters
----------
img : ndarray
the image on which the T label is to be written/drawn, preferably the image with the rectangular bounding box drawn
label : str
the text (label) to be written
bbox : list
a list containing x_min, y_min, x_max and y_max of the rectangle positions
draw_bg : bool, optional
if True, draws the background of the text, else just the text is written, by default True
text_bg_color : tuple, optional
the background color of the label that is filled, by default (255, 255, 255)
text_color : tuple, optional
color of the text (label) to be written, by default (0, 0, 0)
Returns
-------
ndarray
the image with the T label drawn/written
"""
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][0]
text_height = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1, 2)[0][1]
# draw vertical line
x_center = (bbox[0] + bbox[2]) // 2
y_top = bbox[1] - 50
cv2.line(img, (x_center, bbox[1]), (x_center, y_top), text_bg_color, 3)
# draw rectangle with label
y_bottom = y_top
y_top = y_bottom - text_height - 5
x_left = x_center - (text_width // 2) - 5
x_right = x_center + (text_width // 2) + 5
if draw_bg:
cv2.rectangle(img, (x_left, y_top - 3), (x_right, y_bottom),
text_bg_color, -1)
cv2.putText(img, label, (x_left + 5, y_bottom - 7),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
return img
def draw_flag_with_label(img,
label,
bbox,
write_label=True,
line_color=(255, 255, 255),
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0)):
"""draws a pole from the middle of the object that is to be labeled and adds the label to the flag
Parameters
----------
img : ndarray
the image on which the flag is to be drawn
label : str
label that is written inside the flag
bbox : list
a list containing x_min, y_min, x_max and y_max of the rectangle positions
write_label : bool, optional
if True, writes the label, otherwise, it's just a vertical line, by default True
line_color : tuple, optional
the color of the pole of the flag, by default (255, 255, 255)
text_bg_color : tuple, optional
the background color of the label that is filled, by default (255, 255, 255)
text_color : tuple, optional
color of the text (label) to be written, by default (0, 0, 0)
Returns
-------
ndarray
the image with flag drawn and the label written in the flag
"""
# draw vertical line
x_center = (bbox[0] + bbox[2]) // 2
y_bottom = int((bbox[1] * .75 + bbox[3] * .25))
y_top = bbox[1] - (y_bottom - bbox[1])
start_point = (x_center, y_top)
end_point = (x_center, y_bottom)
cv2.line(img, start_point, end_point, line_color, 3)
# write label
if write_label:
text_width = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 1,
2)[0][0]
label_bg = [
start_point[0], start_point[1], start_point[0] + text_width,
start_point[1] + 30
]
cv2.rectangle(img, (label_bg[0], label_bg[1]),
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
cv2.putText(img, label, (start_point[0] + 5, start_point[1] - 5 + 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, text_color, 2)
return img
# THE FOLLOWING ARE OPTIONAL FUNCTIONS THAT CAN BE USED FOR DRAWING OR LABELLING MULTIPLE OBJECTS IN THE SAME
# IMAGE. IN ORDER TO HAVE FULL CONTROL OF YOUR VISUALIZATIONS IT IS ADVISABLE TO USE THE ABOVE FUNCTIONS IN FOR LOOPS
# INSTEAD OF THE FUNCTIONS BELOW
def draw_rectangles(img,
bboxes,
bbox_color=(255, 255, 255),
thickness=3,
is_opaque=False,
alpha=0.5):
"""draws multiple rectangles
img : ndarray
the actual image
bboxes : list
a list of lists, each inner list containing x_min, y_min, x_max and y_max of the rectangle positions
bbox_color : tuple, optional
the color of the boxes, by default (255,255,255)
thickness : int, optional
thickness of the outline of the boxes, by default 3
is_opaque : bool, optional
if False, draws solid rectangular outlines for rectangles. Else, filled rectangles which are semi transparent, by default False
alpha : float, optional
strength of the opacity, by default 0.5
Returns
-------
ndarray
the image with the bounding boxes drawn
"""
for bbox in bboxes:
img = draw_rectangle(img, bbox, bbox_color, thickness, is_opaque,
alpha)
return img
def add_labels_to_rectangles(img,
labels,
bboxes,
draw_bg=True,
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0),
top=True):
"""add labels, inside or outside the rectangles
Parameters
----------
img : ndarray
the image on which the labels are to be written, preferably the image with the rectangular bounding boxes drawn
labels : list
a list of string of the texts (labels) to be written
bboxes : list
a list of lists, each inner list containing x_min, y_min, x_max and y_max of the rectangle positions
draw_bg : bool, optional
if True, draws the background of the texts, else just the texts are written, by default True
text_bg_color : tuple, optional
the background color of the labels that are filled, by default (255, 255, 255)
text_color : tuple, optional
color of the texts (labels) to be written, by default (0, 0, 0)
top : bool, optional
if True, writes the labels on top of the bounding boxes, else inside, by default True
Returns
-------
ndarray
the image with the labels written
"""
for label, bbox in zip(labels, bboxes):
img = add_label_to_rectangle(img, label, bbox, draw_bg, text_bg_color,
text_color, top)
return img
def add_T_labels_to_rectangles(img,
labels,
bboxes,
draw_bg=True,
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0)):
"""adds T labels to the rectangles, each originating from the top of the rectangle
Parameters
----------
img : ndarray
the image on which the T labels are to be written/drawn, preferably the image with the rectangular bounding boxes drawn
labels : list
the texts (labels) to be written
bboxes : list
a list of lists, each inner list containing x_min, y_min, x_max and y_max of the rectangle positions
draw_bg : bool, optional
if True, draws the background of the texts, else just the texts are written, by default True
text_bg_color : tuple, optional
the background color of the labels that are filled, by default (255, 255, 255)
text_color : tuple, optional
color of the texts (labels) to be written, by default (0, 0, 0)
Returns
-------
ndarray
the image with the T labels drawn/written
"""
for label, bbox in zip(labels, bboxes):
add_T_label_to_rectangle(img, label, bbox, draw_bg, text_bg_color,
text_color)
return img
def draw_flags_with_labels(img,
labels,
bboxes,
write_label=True,
line_color=(255, 255, 255),
text_bg_color=(255, 255, 255),
text_color=(0, 0, 0)):
"""draws poles from the middle of the objects that are to be labeled and adds the labels to the flags
Parameters
----------
img : ndarray
the image on which the flags are to be drawn
labels : list
labels that are written inside the flags
bbox : list
a list of lists, each inner list containing x_min, y_min, x_max and y_max of the rectangle positions
write_label : bool, optional
if True, writes the labels, otherwise, it's just a vertical line for each object, by default True
line_color : tuple, optional
the color of the pole of the flags, by default (255, 255, 255)
text_bg_color : tuple, optional
the background color of the labels that are filled, by default (255, 255, 255)
text_color : tuple, optional
color of the texts (labels) to be written, by default (0, 0, 0)
Returns
-------
ndarray
the image with flags drawn and the labels written in the flags
"""
for label, bbox in zip(labels, bboxes):
img = draw_flag_with_label(img, label, bbox, write_label, line_color,
text_bg_color, text_color)
return img