Example #1
0
def main():
    parser = ArgumentParser()
    parser.add_argument('pcd', help='Point cloud file')
    parser.add_argument('image', help='image file')
    parser.add_argument('ann', help='ann file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--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_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=args.show,
        snapshot=args.snapshot)
Example #2
0
def main():
    parser = ArgumentParser()
    parser.add_argument('pcd', help='Point cloud file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    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_model(args.config, args.checkpoint, device=args.device)
    # test a single image
    result, data = inference_segmentor(model, args.pcd)
    # show the results
    show_result_meshlab(data,
                        result,
                        args.out_dir,
                        show=args.show,
                        snapshot=args.snapshot,
                        task='seg',
                        palette=model.PALETTE)
Example #3
0
def test_show_result_meshlab():
    pcd = 'tests/data/nuscenes/samples/LIDAR_TOP/n015-2018-08-02-17-16-37+' \
              '0800__LIDAR_TOP__1533201470948018.pcd.bin'
    box_3d = LiDARInstance3DBoxes(
        torch.tensor(
            [[8.7314, -1.8559, -1.5997, 0.4800, 1.2000, 1.8900, 0.0100]]))
    labels_3d = torch.tensor([0])
    scores_3d = torch.tensor([0.5])
    points = np.random.rand(100, 4)
    img_meta = dict(
        pts_filename=pcd, boxes_3d=box_3d, box_mode_3d=Box3DMode.LIDAR)
    data = dict(points=[[torch.tensor(points)]], img_metas=[[img_meta]])
    result = [
        dict(
            pts_bbox=dict(
                boxes_3d=box_3d, labels_3d=labels_3d, scores_3d=scores_3d))
    ]
    temp_out_dir = tempfile.mkdtemp()
    out_dir, file_name = show_result_meshlab(data, result, temp_out_dir)
    expected_outfile_ply = file_name + '_pred.ply'
    expected_outfile_obj = file_name + '_points.obj'
    expected_outfile_ply_path = os.path.join(out_dir, file_name,
                                             expected_outfile_ply)
    expected_outfile_obj_path = os.path.join(out_dir, file_name,
                                             expected_outfile_obj)
    assert os.path.exists(expected_outfile_ply_path)
    assert os.path.exists(expected_outfile_obj_path)
    os.remove(expected_outfile_obj_path)
    os.remove(expected_outfile_ply_path)
    os.removedirs(os.path.join(temp_out_dir, file_name))
Example #4
0
def execution(idx):
    global dataset, model, cfg, args
    print()

    # Select and infer a sample
    sample = dataset.__getitem__(idx)
    if isinstance(sample['img_metas'], list):
        filename = sample['img_metas'][0].data['pts_filename']
    else:
        filename = sample['img_metas'].data['pts_filename']

    pcd_file = os.path.join(dataset.root_split, dataset.pts_prefix, filename)
    print("Selected pcd_file:", pcd_file)

    result, data = inference_detector(model, pcd_file)
    print("Number of detected bboxes:", len(result['labels_3d']))

    # Convert to MeshLab
    os.makedirs(args.outdir, exist_ok=True)
    show_result_meshlab(data, result, args.outdir)
    print("Visualization result is saved in \'{}\'. "
          "Please use MeshLab to visualize it.".format(
              os.path.join(args.outdir,
                           os.path.basename(pcd_file).split('.')[0])))
Example #5
0
def main():
    parser = ArgumentParser()
    parser.add_argument('pcd', help='Point cloud file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--score-thr',
                        type=float,
                        default=0.6,
                        help='bbox score threshold')
    parser.add_argument('--out-dir',
                        type=str,
                        default='demo',
                        help='dir to save 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)
Example #6
0
def test_show_result_meshlab():
    pcd = 'tests/data/nuscenes/samples/LIDAR_TOP/n015-2018-08-02-17-16-37+' \
              '0800__LIDAR_TOP__1533201470948018.pcd.bin'
    box_3d = LiDARInstance3DBoxes(
        torch.tensor(
            [[8.7314, -1.8559, -1.5997, 0.4800, 1.2000, 1.8900, 0.0100]]))
    labels_3d = torch.tensor([0])
    scores_3d = torch.tensor([0.5])
    points = np.random.rand(100, 4)
    img_meta = dict(pts_filename=pcd,
                    boxes_3d=box_3d,
                    box_mode_3d=Box3DMode.LIDAR)
    data = dict(points=[[torch.tensor(points)]], img_metas=[[img_meta]])
    result = [
        dict(pts_bbox=dict(
            boxes_3d=box_3d, labels_3d=labels_3d, scores_3d=scores_3d))
    ]
    tmp_dir = tempfile.TemporaryDirectory()
    temp_out_dir = tmp_dir.name
    out_dir, file_name = show_result_meshlab(data, result, temp_out_dir)
    expected_outfile_pred = file_name + '_pred.obj'
    expected_outfile_pts = file_name + '_points.obj'
    expected_outfile_pred_path = os.path.join(out_dir, file_name,
                                              expected_outfile_pred)
    expected_outfile_pts_path = os.path.join(out_dir, file_name,
                                             expected_outfile_pts)
    assert os.path.exists(expected_outfile_pred_path)
    assert os.path.exists(expected_outfile_pts_path)
    tmp_dir.cleanup()

    # test multi-modality show
    # indoor scene
    pcd = 'tests/data/sunrgbd/points/000001.bin'
    filename = 'tests/data/sunrgbd/sunrgbd_trainval/image/000001.jpg'
    box_3d = DepthInstance3DBoxes(
        torch.tensor(
            [[-1.1580, 3.3041, -0.9961, 0.3829, 0.4647, 0.5574, 1.1213]]))
    img = np.random.randn(1, 3, 608, 832)
    k_mat = np.array([[529.5000, 0.0000, 365.0000],
                      [0.0000, 529.5000, 265.0000], [0.0000, 0.0000, 1.0000]])
    rt_mat = np.array([[0.9980, 0.0058, -0.0634], [0.0058, 0.9835, 0.1808],
                       [0.0634, -0.1808, 0.9815]])
    rt_mat = np.array([[1, 0, 0], [0, 0, -1], [0, 1, 0]]) @ rt_mat.transpose(
        1, 0)
    depth2img = k_mat @ rt_mat
    img_meta = dict(filename=filename,
                    depth2img=depth2img,
                    pcd_horizontal_flip=False,
                    pcd_vertical_flip=False,
                    box_mode_3d=Box3DMode.DEPTH,
                    box_type_3d=DepthInstance3DBoxes,
                    pcd_trans=np.array([0., 0., 0.]),
                    pcd_scale_factor=1.0,
                    pts_filename=pcd,
                    transformation_3d_flow=['R', 'S', 'T'])
    data = dict(points=[[torch.tensor(points)]],
                img_metas=[[img_meta]],
                img=[img])
    result = [dict(boxes_3d=box_3d, labels_3d=labels_3d, scores_3d=scores_3d)]
    tmp_dir = tempfile.TemporaryDirectory()
    temp_out_dir = tmp_dir.name
    out_dir, file_name = show_result_meshlab(data,
                                             result,
                                             temp_out_dir,
                                             0.3,
                                             task='multi_modality-det')
    expected_outfile_pred = file_name + '_pred.obj'
    expected_outfile_pts = file_name + '_points.obj'
    expected_outfile_png = file_name + '_img.png'
    expected_outfile_proj = file_name + '_pred.png'
    expected_outfile_pred_path = os.path.join(out_dir, file_name,
                                              expected_outfile_pred)
    expected_outfile_pts_path = os.path.join(out_dir, file_name,
                                             expected_outfile_pts)
    expected_outfile_png_path = os.path.join(out_dir, file_name,
                                             expected_outfile_png)
    expected_outfile_proj_path = os.path.join(out_dir, file_name,
                                              expected_outfile_proj)
    assert os.path.exists(expected_outfile_pred_path)
    assert os.path.exists(expected_outfile_pts_path)
    assert os.path.exists(expected_outfile_png_path)
    assert os.path.exists(expected_outfile_proj_path)
    tmp_dir.cleanup()
    # outdoor scene
    pcd = 'tests/data/kitti/training/velodyne_reduced/000000.bin'
    filename = 'tests/data/kitti/training/image_2/000000.png'
    box_3d = LiDARInstance3DBoxes(
        torch.tensor(
            [[6.4495, -3.9097, -1.7409, 1.5063, 3.1819, 1.4716, 1.8782]]))
    img = np.random.randn(1, 3, 384, 1280)
    lidar2img = np.array(
        [[6.09695435e+02, -7.21421631e+02, -1.25125790e+00, -1.23041824e+02],
         [1.80384201e+02, 7.64479828e+00, -7.19651550e+02, -1.01016693e+02],
         [9.99945343e-01, 1.24365499e-04, 1.04513029e-02, -2.69386917e-01],
         [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]])
    img_meta = dict(filename=filename,
                    pcd_horizontal_flip=False,
                    pcd_vertical_flip=False,
                    box_mode_3d=Box3DMode.LIDAR,
                    box_type_3d=LiDARInstance3DBoxes,
                    pcd_trans=np.array([0., 0., 0.]),
                    pcd_scale_factor=1.0,
                    pts_filename=pcd,
                    lidar2img=lidar2img)
    data = dict(points=[[torch.tensor(points)]],
                img_metas=[[img_meta]],
                img=[img])
    result = [
        dict(pts_bbox=dict(
            boxes_3d=box_3d, labels_3d=labels_3d, scores_3d=scores_3d))
    ]
    out_dir, file_name = show_result_meshlab(data,
                                             result,
                                             temp_out_dir,
                                             0.1,
                                             task='multi_modality-det')
    tmp_dir = tempfile.TemporaryDirectory()
    temp_out_dir = tmp_dir.name
    expected_outfile_pred = file_name + '_pred.obj'
    expected_outfile_pts = file_name + '_points.obj'
    expected_outfile_png = file_name + '_img.png'
    expected_outfile_proj = file_name + '_pred.png'
    expected_outfile_pred_path = os.path.join(out_dir, file_name,
                                              expected_outfile_pred)
    expected_outfile_pts_path = os.path.join(out_dir, file_name,
                                             expected_outfile_pts)
    expected_outfile_png_path = os.path.join(out_dir, file_name,
                                             expected_outfile_png)
    expected_outfile_proj_path = os.path.join(out_dir, file_name,
                                              expected_outfile_proj)
    assert os.path.exists(expected_outfile_pred_path)
    assert os.path.exists(expected_outfile_pts_path)
    assert os.path.exists(expected_outfile_png_path)
    assert os.path.exists(expected_outfile_proj_path)
    tmp_dir.cleanup()
    # test mono-3d show
    filename = 'tests/data/nuscenes/samples/CAM_BACK_LEFT/n015-2018-' \
               '07-18-11-07-57+0800__CAM_BACK_LEFT__1531883530447423.jpg'
    box_3d = CameraInstance3DBoxes(
        torch.tensor(
            [[6.4495, -3.9097, -1.7409, 1.5063, 3.1819, 1.4716, 1.8782]]))
    img = np.random.randn(1, 3, 384, 1280)
    cam2img = np.array([[100.0, 0.0, 50.0], [0.0, 100.0, 50.0],
                        [0.0, 0.0, 1.0]])
    img_meta = dict(filename=filename,
                    pcd_horizontal_flip=False,
                    pcd_vertical_flip=False,
                    box_mode_3d=Box3DMode.CAM,
                    box_type_3d=CameraInstance3DBoxes,
                    pcd_trans=np.array([0., 0., 0.]),
                    pcd_scale_factor=1.0,
                    cam2img=cam2img)
    data = dict(points=[[torch.tensor(points)]],
                img_metas=[[img_meta]],
                img=[img])
    result = [
        dict(img_bbox=dict(
            boxes_3d=box_3d, labels_3d=labels_3d, scores_3d=scores_3d))
    ]
    out_dir, file_name = show_result_meshlab(data,
                                             result,
                                             temp_out_dir,
                                             0.1,
                                             task='mono-det')
    tmp_dir = tempfile.TemporaryDirectory()
    temp_out_dir = tmp_dir.name
    expected_outfile_png = file_name + '_img.png'
    expected_outfile_proj = file_name + '_pred.png'
    expected_outfile_png_path = os.path.join(out_dir, file_name,
                                             expected_outfile_png)
    expected_outfile_proj_path = os.path.join(out_dir, file_name,
                                              expected_outfile_proj)
    assert os.path.exists(expected_outfile_png_path)
    assert os.path.exists(expected_outfile_proj_path)
    tmp_dir.cleanup()

    # test seg show
    pcd = 'tests/data/scannet/points/scene0000_00.bin'
    points = np.random.rand(100, 6)
    img_meta = dict(pts_filename=pcd)
    data = dict(points=[[torch.tensor(points)]], img_metas=[[img_meta]])
    pred_seg = torch.randint(0, 20, (100, ))
    result = [dict(semantic_mask=pred_seg)]
    tmp_dir = tempfile.TemporaryDirectory()
    temp_out_dir = tmp_dir.name
    out_dir, file_name = show_result_meshlab(data,
                                             result,
                                             temp_out_dir,
                                             task='seg')
    expected_outfile_pred = file_name + '_pred.obj'
    expected_outfile_pts = file_name + '_points.obj'
    expected_outfile_pred_path = os.path.join(out_dir, file_name,
                                              expected_outfile_pred)
    expected_outfile_pts_path = os.path.join(out_dir, file_name,
                                             expected_outfile_pts)
    assert os.path.exists(expected_outfile_pred_path)
    assert os.path.exists(expected_outfile_pts_path)
    tmp_dir.cleanup()
Example #7
0
def main():
    parser = ArgumentParser()
    parser.add_argument(
        '--pcd',
        type=str,
        default='./demo/kitti_000008.bin',
        help='Point cloud file'
    )  #/data/cmpe249-fa21/4c_train5678/training/velodyne/008118.bin
    parser.add_argument(
        '--config',
        type=str,
        default=
        './configs/second/myhv_second_secfpn_sbn_2x16_2x_waymoD5-3d-4class.py',
        help='Config file')
    parser.add_argument(
        '--checkpoint',
        type=str,
        default=
        '../modelzoo_mmdetection3d/hv_second_secfpn_6x8_80e_kitti-3d-3class_20200620_230238-9208083a.pth',
        help='Checkpoint file')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--score-thr',
                        type=float,
                        default=0.6,
                        help='bbox score threshold')
    parser.add_argument('--out-dir',
                        type=str,
                        default='demo',
                        help='dir to save 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)
    print(result)  # three keys: 'boxes_3d', 'scores_3d', 'labels_3d'

    points = data['points'][0][0].cpu().numpy()  # points number *4
    pts_filename = data['img_metas'][0][0]['pts_filename']
    print("pts_filename:", pts_filename)
    file_name = osp.split(pts_filename)[-1].split('.')[0]  #006767
    print(data['img_metas'])
    print(data['img_metas'][0][0]['box_mode_3d'])  #Box3DMode.LIDAR

    print("results len:", len(result[0]))  # len=1,
    for res in result[0].keys():
        print(res)
    #boxes_3d, scores_3d, labels_3d
    pred_bboxes = result[0]['boxes_3d'].tensor.numpy()
    print(
        pred_bboxes
    )  # [11,7], Each row is (x, y, z, x_size, y_size, z_size, yaw) in Box3DMode.LIDAR
    print(type(pred_bboxes))  #numpy.ndarray

    print("box_mode_3d:",
          data['img_metas'][0][0]['box_mode_3d'])  #Box3DMode.LIDAR

    # show the results (points save to xxx_points.obj file, pred3d box save to xxx_pred.ply, these two files can opend by meshlab)
    show_result_meshlab(data, result, args.out_dir)
parser.add_argument("--outdir",
                    type=str,
                    default='cache/',
                    help="Path to the output directory")

args = parser.parse_args()

# Build dataset
cfg = mmcv.Config.fromfile(args.cfg)
dataset = build_dataset(cfg.data.train)

# Select a sample
sample = dataset.__getitem__(args.idx)

# Convert to MeshLab
data = dict(
    img_metas=[[sample['img_metas'].data]],
    points=[[sample['points'].data]],
)
gts = dict(
    boxes_3d=sample['gt_bboxes_3d'].data,
    scores_3d=torch.ones([len(sample['gt_labels_3d'].data)]),
    labels_3d=sample['gt_labels_3d'].data,
)

os.makedirs(args.outdir, exist_ok=True)
show_result_meshlab(data, gts, args.outdir)
print(
    "\nVisualization result is saved in \'{}\'. Please use MeshLab to visualize it."
    .format(args.outdir))