Skip to content

Commit e990105

Browse files
[Fix] Fix indoor det visualization (#2625)
* fix visual * add AttributeError * fix keep index bug * fix open3d version bug
1 parent fb5a323 commit e990105

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

mmdet3d/datasets/transforms/formating.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def __init__(
7575
'affine_aug', 'sweep_img_metas', 'ori_cam2img',
7676
'cam2global', 'crop_offset', 'img_crop_offset',
7777
'resize_img_shape', 'lidar2cam', 'ori_lidar2img',
78-
'num_ref_frames', 'num_views', 'ego2global')
78+
'num_ref_frames', 'num_views', 'ego2global',
79+
'axis_align_matrix')
7980
) -> None:
8081
self.keys = keys
8182
self.meta_keys = meta_keys

mmdet3d/engine/hooks/visualization_hook.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class Det3DVisualizationHook(Hook):
4343
show (bool): Whether to display the drawn image. Default to False.
4444
vis_task (str): Visualization task. Defaults to 'mono_det'.
4545
wait_time (float): The interval of show (s). Defaults to 0.
46+
draw_gt (bool): Whether to draw ground truth. Defaults to True.
47+
draw_pred (bool): Whether to draw prediction. Defaults to True.
48+
show_pcd_rgb (bool): Whether to show RGB point cloud. Defaults to
49+
False.
4650
test_out_dir (str, optional): directory where painted images
4751
will be saved in testing process.
4852
backend_args (dict, optional): Arguments to instantiate the
@@ -57,8 +61,9 @@ def __init__(self,
5761
vis_task: str = 'mono_det',
5862
wait_time: float = 0.,
5963
test_out_dir: Optional[str] = None,
60-
draw_gt: bool = True,
64+
draw_gt: bool = False,
6165
draw_pred: bool = True,
66+
show_pcd_rgb: bool = False,
6267
backend_args: Optional[dict] = None):
6368
self._visualizer: Visualizer = Visualizer.get_current_instance()
6469
self.interval = interval
@@ -87,6 +92,7 @@ def __init__(self,
8792
self._test_index = 0
8893
self.draw_gt = draw_gt
8994
self.draw_pred = draw_pred
95+
self.show_pcd_rgb = show_pcd_rgb
9096

9197
def after_val_iter(self, runner: Runner, batch_idx: int, data_batch: dict,
9298
outputs: Sequence[Det3DDataSample]) -> None:
@@ -142,11 +148,14 @@ def after_val_iter(self, runner: Runner, batch_idx: int, data_batch: dict,
142148
'val sample',
143149
data_input,
144150
data_sample=outputs[0],
151+
draw_gt=self.draw_gt,
152+
draw_pred=self.draw_pred,
145153
show=self.show,
146154
vis_task=self.vis_task,
147155
wait_time=self.wait_time,
148156
pred_score_thr=self.score_thr,
149-
step=total_curr_iter)
157+
step=total_curr_iter,
158+
show_pcd_rgb=self.show_pcd_rgb)
150159

151160
def after_test_iter(self, runner: Runner, batch_idx: int, data_batch: dict,
152161
outputs: Sequence[Det3DDataSample]) -> None:
@@ -228,4 +237,5 @@ def after_test_iter(self, runner: Runner, batch_idx: int, data_batch: dict,
228237
pred_score_thr=self.score_thr,
229238
out_file=out_file,
230239
o3d_save_path=o3d_save_path,
231-
step=self._test_index)
240+
step=self._test_index,
241+
show_pcd_rgb=self.show_pcd_rgb)

mmdet3d/visualization/local_visualizer.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from mmdet3d.registry import VISUALIZERS
2424
from mmdet3d.structures import (BaseInstance3DBoxes, Box3DMode,
2525
CameraInstance3DBoxes, Coord3DMode,
26-
DepthInstance3DBoxes, Det3DDataSample,
27-
LiDARInstance3DBoxes, PointData,
28-
points_cam2img)
26+
DepthInstance3DBoxes, DepthPoints,
27+
Det3DDataSample, LiDARInstance3DBoxes,
28+
PointData, points_cam2img)
2929
from .vis_utils import (proj_camera_bbox3d_to_img, proj_depth_bbox3d_to_img,
3030
proj_lidar_bbox3d_to_img, to_depth_mode)
3131

@@ -293,7 +293,7 @@ def draw_bboxes_3d(self,
293293
# convert bboxes to numpy dtype
294294
bboxes_3d = tensor2ndarray(bboxes_3d.tensor)
295295

296-
in_box_color = np.array(points_in_box_color)
296+
# in_box_color = np.array(points_in_box_color)
297297

298298
for i in range(len(bboxes_3d)):
299299
center = bboxes_3d[i, 0:3]
@@ -320,7 +320,7 @@ def draw_bboxes_3d(self,
320320
if self.pcd is not None and mode == 'xyz':
321321
indices = box3d.get_point_indices_within_bounding_box(
322322
self.pcd.points)
323-
self.points_colors[indices] = in_box_color
323+
self.points_colors[indices] = np.array(bbox_color[i]) / 255.
324324

325325
# update points colors
326326
if self.pcd is not None:
@@ -606,6 +606,7 @@ def _draw_instances_3d(self,
606606
instances: InstanceData,
607607
input_meta: dict,
608608
vis_task: str,
609+
show_pcd_rgb: bool = False,
609610
palette: Optional[List[tuple]] = None) -> dict:
610611
"""Draw 3D instances of GT or prediction.
611612
@@ -616,6 +617,7 @@ def _draw_instances_3d(self,
616617
input_meta (dict): Meta information.
617618
vis_task (str): Visualization task, it includes: 'lidar_det',
618619
'multi-modality_det', 'mono_det'.
620+
show_pcd_rgb (bool): Whether to show RGB point cloud.
619621
palette (List[tuple], optional): Palette information corresponding
620622
to the category. Defaults to None.
621623
@@ -643,13 +645,22 @@ def _draw_instances_3d(self,
643645
else:
644646
bboxes_3d_depth = bboxes_3d.clone()
645647

648+
if 'axis_align_matrix' in input_meta:
649+
points = DepthPoints(points, points_dim=points.shape[1])
650+
rot_mat = input_meta['axis_align_matrix'][:3, :3]
651+
trans_vec = input_meta['axis_align_matrix'][:3, -1]
652+
points.rotate(rot_mat.T)
653+
points.translate(trans_vec)
654+
points = tensor2ndarray(points.tensor)
655+
646656
max_label = int(max(labels_3d) if len(labels_3d) > 0 else 0)
647657
bbox_color = palette if self.bbox_color is None \
648658
else self.bbox_color
649659
bbox_palette = get_palette(bbox_color, max_label + 1)
650660
colors = [bbox_palette[label] for label in labels_3d]
651661

652-
self.set_points(points, pcd_mode=2)
662+
self.set_points(
663+
points, pcd_mode=2, mode='xyzrgb' if show_pcd_rgb else 'xyz')
653664
self.draw_bboxes_3d(bboxes_3d_depth, bbox_color=colors)
654665

655666
data_3d['bboxes_3d'] = tensor2ndarray(bboxes_3d_depth.tensor)
@@ -871,7 +882,7 @@ def show(self,
871882
self.o3d_vis.clear_geometries()
872883
try:
873884
del self.pcd
874-
except KeyError:
885+
except (KeyError, AttributeError):
875886
pass
876887
if save_path is not None:
877888
if not (save_path.endswith('.png')
@@ -923,7 +934,8 @@ def add_datasample(self,
923934
o3d_save_path: Optional[str] = None,
924935
vis_task: str = 'mono_det',
925936
pred_score_thr: float = 0.3,
926-
step: int = 0) -> None:
937+
step: int = 0,
938+
show_pcd_rgb: bool = False) -> None:
927939
"""Draw datasample and save to all backends.
928940
929941
- If GT and prediction are plotted at the same time, they are displayed
@@ -954,6 +966,8 @@ def add_datasample(self,
954966
pred_score_thr (float): The threshold to visualize the bboxes
955967
and masks. Defaults to 0.3.
956968
step (int): Global step value to record. Defaults to 0.
969+
show_pcd_rgb (bool): Whether to show RGB point cloud. Defaults to
970+
False.
957971
"""
958972
assert vis_task in (
959973
'mono_det', 'multi-view_det', 'lidar_det', 'lidar_seg',
@@ -976,7 +990,7 @@ def add_datasample(self,
976990
if 'gt_instances_3d' in data_sample:
977991
gt_data_3d = self._draw_instances_3d(
978992
data_input, data_sample.gt_instances_3d,
979-
data_sample.metainfo, vis_task, palette)
993+
data_sample.metainfo, vis_task, show_pcd_rgb, palette)
980994
if 'gt_instances' in data_sample:
981995
if len(data_sample.gt_instances) > 0:
982996
assert 'img' in data_input
@@ -1006,7 +1020,8 @@ def add_datasample(self,
10061020
pred_data_3d = self._draw_instances_3d(data_input,
10071021
pred_instances_3d,
10081022
data_sample.metainfo,
1009-
vis_task, palette)
1023+
vis_task, show_pcd_rgb,
1024+
palette)
10101025
if 'pred_instances' in data_sample:
10111026
if 'img' in data_input and len(data_sample.pred_instances) > 0:
10121027
pred_instances = data_sample.pred_instances

0 commit comments

Comments
 (0)