Skip to content

Commit 2728477

Browse files
committed
Fix formatting with black
1 parent 10fec1e commit 2728477

File tree

1 file changed

+121
-57
lines changed

1 file changed

+121
-57
lines changed

bbox_visualizer/bbox_visualizer.py

Lines changed: 121 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,71 @@ def add_label(
122122
the image with the label written
123123
"""
124124

125-
126125
(text_width, text_height), baseline = cv2.getTextSize(label, font, size, thickness)
127126

128127
if top and bbox[1] - text_height > text_height:
129-
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] - text_height - (15 * size)]
128+
label_bg = [
129+
bbox[0],
130+
bbox[1],
131+
bbox[0] + text_width,
132+
bbox[1] - text_height - (15 * size),
133+
]
130134
if draw_bg:
131-
cv2.rectangle(img, (label_bg[0], label_bg[1]),
132-
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
133-
134-
cv2.putText(img, label, (bbox[0] + 5, bbox[1] - (15 * size)), font, size, text_color, thickness)
135+
cv2.rectangle(
136+
img,
137+
(label_bg[0], label_bg[1]),
138+
(label_bg[2] + 5, label_bg[3]),
139+
text_bg_color,
140+
-1,
141+
)
142+
143+
cv2.putText(
144+
img,
145+
label,
146+
(bbox[0] + 5, bbox[1] - (15 * size)),
147+
font,
148+
size,
149+
text_color,
150+
thickness,
151+
)
135152
else:
136-
label_bg = [bbox[0], bbox[1], bbox[0] + text_width, bbox[1] + text_height + (15 * size)]
153+
label_bg = [
154+
bbox[0],
155+
bbox[1],
156+
bbox[0] + text_width,
157+
bbox[1] + text_height + (15 * size),
158+
]
137159
if draw_bg:
138160

139-
cv2.rectangle(img, (label_bg[0], label_bg[1]),
140-
(label_bg[2] + 5, label_bg[3]), text_bg_color, -1)
141-
cv2.putText(img, label, (bbox[0] + 5, bbox[1] + (16 * size) + (4 * thickness)), font, size, text_color, thickness)
161+
cv2.rectangle(
162+
img,
163+
(label_bg[0], label_bg[1]),
164+
(label_bg[2] + 5, label_bg[3]),
165+
text_bg_color,
166+
-1,
167+
)
168+
cv2.putText(
169+
img,
170+
label,
171+
(bbox[0] + 5, bbox[1] + (16 * size) + (4 * thickness)),
172+
font,
173+
size,
174+
text_color,
175+
thickness,
176+
)
142177
return img
143178

144-
def add_T_label(img,
145-
label,
146-
bbox,
147-
size=1,
148-
thickness=2,
149-
draw_bg=True,
150-
text_bg_color=(255, 255, 255),
151-
text_color=(0, 0, 0)):
152179

180+
def add_T_label(
181+
img,
182+
label,
183+
bbox,
184+
size=1,
185+
thickness=2,
186+
draw_bg=True,
187+
text_bg_color=(255, 255, 255),
188+
text_color=(0, 0, 0),
189+
):
153190
"""adds a T label to the rectangle, originating from the top of the rectangle
154191
155192
Parameters
@@ -177,7 +214,9 @@ def add_T_label(img,
177214
the image with the T label drawn/written
178215
"""
179216

180-
(label_width, label_height), baseline = cv2.getTextSize(label, font, size, thickness)
217+
(label_width, label_height), baseline = cv2.getTextSize(
218+
label, font, size, thickness
219+
)
181220
# draw vertical line
182221
x_center = (bbox[0] + bbox[2]) // 2
183222
line_top = y_top = bbox[1] - 50
@@ -196,27 +235,31 @@ def add_T_label(img,
196235
x_left = x_center - (label_width // 2) - 5
197236
x_right = x_center + (label_width // 2) + 5
198237
if draw_bg:
199-
cv2.rectangle(img, (x_left, y_top - 30), (x_right, y_bottom),
200-
text_bg_color, -1)
201-
cv2.putText(img, label, (x_left + 5, y_bottom - (8 * size)),
202-
font, size, text_color, thickness)
203-
204-
238+
cv2.rectangle(img, (x_left, y_top - 30), (x_right, y_bottom), text_bg_color, -1)
239+
cv2.putText(
240+
img,
241+
label,
242+
(x_left + 5, y_bottom - (8 * size)),
243+
font,
244+
size,
245+
text_color,
246+
thickness,
247+
)
205248

206249
return img
207250

208251

209-
def draw_flag_with_label(img,
210-
label,
211-
bbox,
212-
size=1,
213-
thickness=2,
214-
write_label=True,
215-
line_color=(255, 255, 255),
216-
text_bg_color=(255, 255, 255),
217-
text_color=(0, 0, 0),
218-
):
219-
252+
def draw_flag_with_label(
253+
img,
254+
label,
255+
bbox,
256+
size=1,
257+
thickness=2,
258+
write_label=True,
259+
line_color=(255, 255, 255),
260+
text_bg_color=(255, 255, 255),
261+
text_color=(0, 0, 0),
262+
):
220263
"""draws a pole from the middle of the object that is to be labeled and adds the label to the flag
221264
222265
Parameters
@@ -247,7 +290,9 @@ def draw_flag_with_label(img,
247290
"""
248291

249292
# draw vertical line
250-
(label_width, label_height), baseline = cv2.getTextSize(label, font, size, thickness)
293+
(label_width, label_height), baseline = cv2.getTextSize(
294+
label, font, size, thickness
295+
)
251296

252297
x_center = (bbox[0] + bbox[2]) // 2
253298
y_bottom = int((bbox[1] * 0.75 + bbox[3] * 0.25))
@@ -286,7 +331,10 @@ def draw_flag_with_label(img,
286331
img,
287332
label,
288333
(start_point[0] + 5, start_point[1] - 5 + 30),
289-
font, size, text_color, thickness
334+
font,
335+
size,
336+
text_color,
337+
thickness,
290338
)
291339

292340
return img
@@ -325,16 +373,17 @@ def draw_multiple_rectangles(
325373
return img
326374

327375

328-
def add_multiple_labels(img,
329-
labels,
330-
bboxes,
331-
size=1,
332-
thickness=2,
333-
draw_bg=True,
334-
text_bg_color=(255, 255, 255),
335-
text_color=(0, 0, 0),
336-
top=True):
337-
376+
def add_multiple_labels(
377+
img,
378+
labels,
379+
bboxes,
380+
size=1,
381+
thickness=2,
382+
draw_bg=True,
383+
text_bg_color=(255, 255, 255),
384+
text_color=(0, 0, 0),
385+
top=True,
386+
):
338387
"""add labels, inside or outside the rectangles
339388
340389
Parameters
@@ -360,8 +409,9 @@ def add_multiple_labels(img,
360409
the image with the labels written
361410
"""
362411
for label, bbox in zip(labels, bboxes):
363-
img = add_label(img, label, bbox, size, thickness, draw_bg, text_bg_color, text_color,
364-
top)
412+
img = add_label(
413+
img, label, bbox, size, thickness, draw_bg, text_bg_color, text_color, top
414+
)
365415
return img
366416

367417

@@ -397,9 +447,16 @@ def add_multiple_T_labels(
397447
"""
398448

399449
for label, bbox in zip(labels, bboxes):
400-
img = add_T_label(img, label, bbox, size=1, thickness=2,
401-
draw_bg=draw_bg, text_bg_color=text_bg_color,
402-
text_color=text_color)
450+
img = add_T_label(
451+
img,
452+
label,
453+
bbox,
454+
size=1,
455+
thickness=2,
456+
draw_bg=draw_bg,
457+
text_bg_color=text_bg_color,
458+
text_color=text_color,
459+
)
403460

404461
return img
405462

@@ -440,9 +497,16 @@ def draw_multiple_flags_with_labels(
440497

441498
for label, bbox in zip(labels, bboxes):
442499

443-
img = draw_flag_with_label(img, label, bbox, size=1, thickness=2,
444-
write_label=write_label, line_color=line_color,
445-
text_bg_color=text_bg_color,
446-
text_color=text_color)
500+
img = draw_flag_with_label(
501+
img,
502+
label,
503+
bbox,
504+
size=1,
505+
thickness=2,
506+
write_label=write_label,
507+
line_color=line_color,
508+
text_bg_color=text_bg_color,
509+
text_color=text_color,
510+
)
447511

448512
return img

0 commit comments

Comments
 (0)