Skip to content

Commit 255d33a

Browse files
author
spencer@primus
committed
Add option for text on boxes
1 parent 84582cc commit 255d33a

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

avapi/visualize/snapshot.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def show_lidar_on_image(
235235
def show_image_with_boxes(
236236
img,
237237
boxes,
238+
text=None,
238239
inline=False,
239240
box_colors="green",
240241
box_thickness=3,
@@ -289,13 +290,14 @@ def show_image_with_boxes(
289290
if isinstance(box, Box2D) or (
290291
isinstance(box, (ObjectState, BoxDetection, MaskDetection, BasicBoxTrack2D))
291292
and isinstance(box.box, Box2D)
292-
):
293+
):
293294
if isinstance(
294295
box, (ObjectState, BoxDetection, MaskDetection, BasicBoxTrack2D)
295296
):
296297
if isinstance(box, MaskDetection):
297298
mask = box.mask
298299
box = box.box
300+
# add box
299301
cv2.rectangle(
300302
img1,
301303
(int(box.xmin), int(box.ymin)),
@@ -304,7 +306,11 @@ def show_image_with_boxes(
304306
box_thickness,
305307
)
306308
bl_edge = (box.xmin, box.ymin)
307-
add_ID_to_image(img1, bl_edge, ID, fontscale=fontscale)
309+
# add text
310+
if text is not None:
311+
add_text_to_image(img1, bl_edge, text[i], fontscale=fontscale)
312+
# add id
313+
add_text_to_image(img1, bl_edge, ID, fontscale=fontscale)
308314
elif (
309315
isinstance(box, (Box3D, BasicBoxTrack3D))
310316
or (
@@ -347,7 +353,7 @@ def show_image_with_boxes(
347353
img1, (int(pt[0]), int(pt[1])), radius, color=(0, 255, 0), thickness=-1
348354
)
349355
bl_edge = (pt[0], pt[1])
350-
add_ID_to_image(img1, bl_edge, ID, fontscale=fontscale)
356+
add_text_to_image(img1, bl_edge, ID, fontscale=fontscale)
351357
else:
352358
raise NotImplementedError(type(box))
353359
if addbox:
@@ -379,8 +385,8 @@ def show_image_with_boxes(
379385
return img1
380386

381387

382-
def add_ID_to_image(img, bl_edge, ID, fontscale=1):
383-
if ID is not None:
388+
def add_text_to_image(img, bl_edge, text, fontscale=1):
389+
if text is not None:
384390
# name on top of box
385391
font = cv2.FONT_HERSHEY_SIMPLEX
386392
edge = 15
@@ -389,18 +395,22 @@ def add_ID_to_image(img, bl_edge, ID, fontscale=1):
389395
max(edge, bl_edge[1] - sep)
390396
)
391397
fontColor = (255, 255, 255)
392-
font_thickness = 1
398+
font_thickness = 2
393399
lineType = 2
394-
cv2.putText(
395-
img,
396-
str(ID),
397-
bottomLeftCornerOfText,
398-
font,
399-
fontscale,
400-
fontColor,
401-
font_thickness,
402-
lineType,
403-
)
400+
x, y = bottomLeftCornerOfText
401+
dy = 10
402+
for line in str(text).splitlines():
403+
cv2.putText(
404+
img,
405+
line,
406+
(x, y),
407+
font,
408+
fontscale,
409+
fontColor,
410+
font_thickness,
411+
lineType,
412+
)
413+
y += 20
404414

405415

406416
def show_lidar_bev_with_boxes(

0 commit comments

Comments
 (0)