Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] enable visualization of the demo results online #473

Merged
merged 2 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion demo/multi_modality_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ def main():
'--score-thr', type=float, default=0.0, help='bbox score threshold')
parser.add_argument(
'--out-dir', type=str, default='demo', help='dir to save results')
parser.add_argument(
'--show', action='store_true', help='show online visuliaztion results')
parser.add_argument(
'--snapshot',
action='store_true',
help='whether to save online visuliaztion results')
args = parser.parse_args()

# build the model from a config file and a checkpoint file
Expand All @@ -25,7 +31,13 @@ def main():
result, data = inference_multi_modality_detector(model, args.pcd,
args.image, args.ann)
# show the results
show_result_meshlab(data, result, args.out_dir, args.score_thr)
show_result_meshlab(
data,
result,
args.out_dir,
args.score_thr,
show=args.show,
snapshot=args.snapshot)


if __name__ == '__main__':
Expand Down
14 changes: 13 additions & 1 deletion demo/pcd_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ def main():
'--score-thr', type=float, default=0.0, help='bbox score threshold')
parser.add_argument(
'--out-dir', type=str, default='demo', help='dir to save results')
parser.add_argument(
'--show', action='store_true', help='show online visuliaztion results')
parser.add_argument(
'--snapshot',
action='store_true',
help='whether to save online visuliaztion results')
args = parser.parse_args()

# build the model from a config file and a checkpoint file
model = init_detector(args.config, args.checkpoint, device=args.device)
# test a single image
result, data = inference_detector(model, args.pcd)
# show the results
show_result_meshlab(data, result, args.out_dir, args.score_thr)
show_result_meshlab(
data,
result,
args.out_dir,
args.score_thr,
show=args.show,
snapshot=args.snapshot)


if __name__ == '__main__':
Expand Down
22 changes: 18 additions & 4 deletions mmdet3d/apis/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,21 @@ def inference_multi_modality_detector(model, pcd, image, ann_file):
return result, data


def show_result_meshlab(data, result, out_dir, score_thr=0.0):
def show_result_meshlab(data,
result,
out_dir,
score_thr=0.0,
show=False,
snapshot=False):
"""Show result by meshlab.

Args:
data (dict): Contain data from pipeline.
result (dict): Predicted result from model.
out_dir (str): Directory to save visualized result.
score_thr (float): Minimum score of bboxes to be shown. Default: 0.0
show (bool): Visualize the results online. Defaults to False.
snapshot (bool): Whether to save the online results. Defaults to False.
"""
points = data['points'][0][0].cpu().numpy()
pts_filename = data['img_metas'][0][0]['pts_filename']
Expand Down Expand Up @@ -220,7 +227,14 @@ def show_result_meshlab(data, result, out_dir, score_thr=0.0):
show_bboxes = Box3DMode.convert(pred_bboxes, box_mode, Box3DMode.DEPTH)
else:
show_bboxes = deepcopy(pred_bboxes)
show_result(points, None, show_bboxes, out_dir, file_name, show=False)
show_result(
points,
None,
show_bboxes,
out_dir,
file_name,
show=show,
snapshot=snapshot)

if 'img' not in data.keys():
return out_dir, file_name
Expand All @@ -242,7 +256,7 @@ def show_result_meshlab(data, result, out_dir, score_thr=0.0):
data['img_metas'][0][0]['lidar2img'],
out_dir,
file_name,
show=False)
show=show)
elif box_mode == Box3DMode.DEPTH:
if 'calib' not in data.keys():
raise NotImplementedError(
Expand All @@ -260,7 +274,7 @@ def show_result_meshlab(data, result, out_dir, score_thr=0.0):
file_name,
depth_bbox=True,
img_metas=data['img_metas'][0][0],
show=False)
show=show)
else:
raise NotImplementedError(
f'visualization of {box_mode} bbox is not supported')
Expand Down
40 changes: 27 additions & 13 deletions mmdet3d/core/visualizer/show_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ def convert_oriented_box_to_trimesh_fmt(box):
return


def show_result(points, gt_bboxes, pred_bboxes, out_dir, filename, show=True):
def show_result(points,
gt_bboxes,
pred_bboxes,
out_dir,
filename,
show=False,
snapshot=False):
"""Convert results into format that is directly readable for meshlab.

Args:
Expand All @@ -77,8 +83,12 @@ def show_result(points, gt_bboxes, pred_bboxes, out_dir, filename, show=True):
pred_bboxes (np.ndarray): Predicted boxes.
out_dir (str): Path of output directory
filename (str): Filename of the current frame.
show (bool): Visualize the results online. Defaults to True.
show (bool): Visualize the results online. Defaults to False.
snapshot (bool): Whether to save the online results. Defaults to False.
"""
result_path = osp.join(out_dir, filename)
mmcv.mkdir_or_exist(result_path)

if show:
from .open3d_vis import Visualizer

Expand All @@ -87,10 +97,9 @@ def show_result(points, gt_bboxes, pred_bboxes, out_dir, filename, show=True):
vis.add_bboxes(bbox3d=pred_bboxes)
if gt_bboxes is not None:
vis.add_bboxes(bbox3d=gt_bboxes, bbox_color=(0, 0, 1))
vis.show()

result_path = osp.join(out_dir, filename)
mmcv.mkdir_or_exist(result_path)
show_path = osp.join(result_path,
f'{filename}_online.png') if snapshot else None
vis.show(show_path)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should save the online results every time, at least let's pass a parameter to determine whether to save? Like what I've done here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice. I have also considered the problem of mandatory saving online results. I will modify the corresponding operation lately.


if points is not None:
_write_obj(points, osp.join(result_path, f'{filename}_points.obj'))
Expand Down Expand Up @@ -119,7 +128,8 @@ def show_seg_result(points,
filename,
palette,
ignore_index=None,
show=False):
show=False,
snapshot=False):
"""Convert results into format that is directly readable for meshlab.

Args:
Expand All @@ -132,6 +142,8 @@ def show_seg_result(points,
ignore_index (int, optional): The label index to be ignored, e.g. \
unannotated points. Defaults to None.
show (bool, optional): Visualize the results online. Defaults to False.
snapshot (bool, optional): Whether to save the online results. \
Defaults to False.
"""
# we need 3D coordinates to visualize segmentation mask
if gt_seg is not None or pred_seg is not None:
Expand All @@ -154,6 +166,9 @@ def show_seg_result(points,
pred_seg_color = np.concatenate([points[:, :3], pred_seg_color],
axis=1)

result_path = osp.join(out_dir, filename)
mmcv.mkdir_or_exist(result_path)

# online visualization of segmentation mask
# we show three masks in a row, scene_points, gt_mask, pred_mask
if show:
Expand All @@ -164,10 +179,9 @@ def show_seg_result(points,
vis.add_seg_mask(gt_seg_color)
if pred_seg is not None:
vis.add_seg_mask(pred_seg_color)
vis.show()

result_path = osp.join(out_dir, filename)
mmcv.mkdir_or_exist(result_path)
show_path = osp.join(result_path,
f'{filename}_online.png') if snapshot else None
vis.show(show_path)

if points is not None:
_write_obj(points, osp.join(result_path, f'{filename}_points.obj'))
Expand All @@ -188,7 +202,7 @@ def show_multi_modality_result(img,
filename,
depth_bbox=False,
img_metas=None,
show=True,
show=False,
gt_bbox_color=(61, 102, 255),
pred_bbox_color=(241, 101, 72)):
"""Convert multi-modality detection results into 2D results.
Expand All @@ -205,7 +219,7 @@ def show_multi_modality_result(img,
filename (str): Filename of the current frame.
depth_bbox (bool): Whether we are projecting camera bbox or lidar bbox.
img_metas (dict): Used in projecting cameta bbox.
show (bool): Visualize the results online. Defaults to True.
show (bool): Visualize the results online. Defaults to False.
gt_bbox_color (str or tuple(int)): Color of bbox lines.
The tuple of color should be in BGR order. Default: (255, 102, 61)
pred_bbox_color (str or tuple(int)): Color of bbox lines.
Expand Down