@@ -122,34 +122,71 @@ def add_label(
122
122
the image with the label written
123
123
"""
124
124
125
-
126
125
(text_width , text_height ), baseline = cv2 .getTextSize (label , font , size , thickness )
127
126
128
127
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
+ ]
130
134
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
+ )
135
152
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
+ ]
137
159
if draw_bg :
138
160
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
+ )
142
177
return img
143
178
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 )):
152
179
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
+ ):
153
190
"""adds a T label to the rectangle, originating from the top of the rectangle
154
191
155
192
Parameters
@@ -177,7 +214,9 @@ def add_T_label(img,
177
214
the image with the T label drawn/written
178
215
"""
179
216
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
+ )
181
220
# draw vertical line
182
221
x_center = (bbox [0 ] + bbox [2 ]) // 2
183
222
line_top = y_top = bbox [1 ] - 50
@@ -196,27 +235,31 @@ def add_T_label(img,
196
235
x_left = x_center - (label_width // 2 ) - 5
197
236
x_right = x_center + (label_width // 2 ) + 5
198
237
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
+ )
205
248
206
249
return img
207
250
208
251
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
+ ):
220
263
"""draws a pole from the middle of the object that is to be labeled and adds the label to the flag
221
264
222
265
Parameters
@@ -247,7 +290,9 @@ def draw_flag_with_label(img,
247
290
"""
248
291
249
292
# 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
+ )
251
296
252
297
x_center = (bbox [0 ] + bbox [2 ]) // 2
253
298
y_bottom = int ((bbox [1 ] * 0.75 + bbox [3 ] * 0.25 ))
@@ -286,7 +331,10 @@ def draw_flag_with_label(img,
286
331
img ,
287
332
label ,
288
333
(start_point [0 ] + 5 , start_point [1 ] - 5 + 30 ),
289
- font , size , text_color , thickness
334
+ font ,
335
+ size ,
336
+ text_color ,
337
+ thickness ,
290
338
)
291
339
292
340
return img
@@ -325,16 +373,17 @@ def draw_multiple_rectangles(
325
373
return img
326
374
327
375
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
+ ):
338
387
"""add labels, inside or outside the rectangles
339
388
340
389
Parameters
@@ -360,8 +409,9 @@ def add_multiple_labels(img,
360
409
the image with the labels written
361
410
"""
362
411
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
+ )
365
415
return img
366
416
367
417
@@ -397,9 +447,16 @@ def add_multiple_T_labels(
397
447
"""
398
448
399
449
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
+ )
403
460
404
461
return img
405
462
@@ -440,9 +497,16 @@ def draw_multiple_flags_with_labels(
440
497
441
498
for label , bbox in zip (labels , bboxes ):
442
499
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
+ )
447
511
448
512
return img
0 commit comments