Skip to content

Commit e964c35

Browse files
author
spencer@primus
committed
Add fov input options on lidar viz
1 parent ee0dba1 commit e964c35

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

avapi/_dataset.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import os
33
import random
4-
from typing import Iterable, Tuple, Union, List
4+
from typing import Iterable, List, Tuple, Union
55

66
import numpy as np
77
from avstack import calibration, sensors
@@ -185,7 +185,9 @@ def get_image(self, frame, sensor=None, agent=None) -> sensors.ImageData:
185185
channel_order="rgb",
186186
)
187187

188-
def get_semseg_image(self, frame, sensor=None, agent=None) -> sensors.SemanticSegmentationImageData:
188+
def get_semseg_image(
189+
self, frame, sensor=None, agent=None
190+
) -> sensors.SemanticSegmentationImageData:
189191
if sensor is None:
190192
sensor = self.sensors["semseg"]
191193
sensor = self.get_sensor_name(sensor, agent)

avapi/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def parse_color_string(cstring):
122122
# lcolor = (255, 255, 0)
123123
elif cstring == "brown":
124124
lcolor = (165, 42, 42)
125+
elif cstring[0] == "#":
126+
lcolor = hex_to_rgb(cstring[1:])
125127
else:
126128
raise ValueError(f"Unknown color type {cstring}")
127129
return lcolor
130+
131+
132+
def hex_to_rgb(hexa):
133+
return tuple(int(hexa[i : i + 2], 16) for i in (0, 2, 4))

avapi/visualize/snapshot.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,15 @@ def show_lidar_bev_with_boxes(
414414
lines=None,
415415
line_colors=None,
416416
bev_size=[500, 500],
417-
colormethod="depth",
418-
rescale=True,
419-
show=True,
420-
return_image=False,
417+
fov=None,
418+
fov_color: str = "#069Af3",
419+
fov_filled: bool = True,
420+
fov_filled_alpha: float = 0.1,
421+
colormethod: str = "depth",
422+
background_color: str = "black",
423+
rescale: bool = True,
424+
show: bool = True,
425+
return_image: bool = False,
421426
):
422427
"""
423428
Show lidar and the detection results (optional) in BEV
@@ -503,7 +508,12 @@ def show_lidar_bev_with_boxes(
503508
max_width = max(max_width, max(bev_corners[:, 1]) + 2)
504509

505510
# define the size of the image and scaling factor
506-
img1 = 0 * np.ones([bev_size[0], bev_size[1], 3], dtype=np.uint8)
511+
if background_color == "black":
512+
img1 = 0 * np.ones([bev_size[0], bev_size[1], 3], dtype=np.uint8)
513+
elif background_color == "white":
514+
img1 = 255 * np.ones([bev_size[0], bev_size[1], 3], dtype=np.uint8)
515+
else:
516+
raise NotImplementedError(background_color)
507517
if extent is None:
508518
width_scale = (max_width - min_width) / bev_size[0]
509519
range_scale = (max_range - min_range) / bev_size[1]
@@ -515,6 +525,30 @@ def show_lidar_bev_with_boxes(
515525
sc_arr = np.array([range_scale, width_scale])
516526
pc_bev = (pc2[:, [0, 1]] - min_arr) / sc_arr
517527

528+
# add the field of view by blending
529+
if fov is not None:
530+
# add fov boundary without alpha
531+
boundary_bev = ((fov.boundary[:, [0, 1]] - min_arr) / sc_arr).astype(int)
532+
boundary_bev = boundary_bev.reshape((-1, 1, 2))
533+
thickness = 3
534+
cv2.polylines(
535+
img=img1,
536+
pts=[boundary_bev],
537+
color=parse_color_string(fov_color),
538+
thickness=thickness,
539+
isClosed=True,
540+
)
541+
# add fov filled with alpha
542+
if fov_filled:
543+
img2 = img1.copy()
544+
cv2.fillPoly(
545+
img=img2,
546+
pts=[boundary_bev],
547+
color=parse_color_string(fov_color),
548+
)
549+
w = fov_filled_alpha
550+
img1 = cv2.addWeighted(img1, 1 - w, img2, w, 0)
551+
518552
# get colors for lidar pcs
519553
if colormethod == "depth":
520554
depths = np.linalg.norm(pc2[:, [0, 1]], axis=1)
@@ -524,6 +558,10 @@ def show_lidar_bev_with_boxes(
524558
elif "channel" in colormethod:
525559
channel = int(colormethod.split("-")[1])
526560
pt_colors = get_lidar_color(pc2[:, channel], mode="channel")
561+
elif colormethod == "black":
562+
pt_colors = 0 * np.ones((len(pc2), 3), dtype=float)
563+
elif colormethod == "white":
564+
pt_colors = 255 * np.ones((len(pc2), 3), dtype=float)
527565
else:
528566
raise NotImplementedError
529567

0 commit comments

Comments
 (0)