def vis_fn_kitti(data_dir, idx, detection, x_range=(-35.2, 35.2), y_range=(-40, 40), grid_size=(0.1, 0.1)): itm = detection output_dict = { "calib": True, "image": False, "label": True, "velodyne": True } if "nusc" in data_dir: calib, _, label, pc = KittiData( data_dir, idx, output_dict=output_dict).read_data(num_feature=5) else: calib, _, label, pc = KittiData(data_dir, idx, output_dict=output_dict).read_data() bevimg = BEVImage(x_range, y_range, grid_size) bevimg.from_lidar(pc) for obj in label.data: bevimg.draw_box(obj, calib, bool_gt=True) box3d_lidar = itm["box3d_lidar"] score = itm["scores"] for box3d_lidar_, score_ in zip(box3d_lidar, score): x, y, z, w, l, h, ry = box3d_lidar_ obj = KittiObj() bcenter_Flidar = np.array([x, y, z]).reshape(1, -1) bcenter_Fcam = calib.lidar2leftcam(bcenter_Flidar) obj.x, obj.y, obj.z = bcenter_Fcam.flatten() obj.w, obj.l, obj.h = w, l, h obj.ry = ry bevimg.draw_box(obj, calib, bool_gt=False, width=2) return bevimg
def create_db_file_kitti_wk_fn(idx): global g_data_dir, g_dbinfos root_dir = g_data_dir tag = idx output_dict = { "calib": True, "image": False, "label": True, "velodyne": True } calib, _, label, pc_Flidar = KittiData( root_dir=str(root_dir), idx=tag, output_dict=output_dict).read_data() for i, obj in enumerate(label.data): name = obj.type gt_idx = i gtpc_path = str(root_dir.parent / "gt_database" / f"{tag}_{name}_{gt_idx}.bin") gtpc_idx = obj.get_pts_idx(pc_Flidar[:, :3], calib) gtpc = pc_Flidar[gtpc_idx, :] box3d_cam = obj num_points_in_gt = gtpc.shape[0] write_pc_to_file(gtpc, gtpc_path) obj_info = { "name": name, "gtpc_path": gtpc_path, "tag": tag, "gt_idx": gt_idx, "box3d_cam": box3d_cam, "num_points_in_gt": num_points_in_gt, "calib": calib } g_dbinfos.append(obj_info)
def create_info_file_kitti_wk_fn(idx): global g_data_dir, g_infos root_dir = g_data_dir infos = g_infos output_dict = { "calib": True, "image": False, "label": True, "velodyne": True } if root_dir.stem == "testing": output_dict[ 'label'] = False # testing folder do not have label_2/ data. info = dict() tag = idx pc_path = str(root_dir / "velodyne" / f"{tag}.bin") reduced_pc_path = str(root_dir / "reduced_velodyne" / f"{tag}.bin") img_path = str(root_dir / "image_2" / f"{tag}.png") calib, _, label, pc_Flidar = KittiData( root_dir=str(root_dir), idx=tag, output_dict=output_dict).read_data() mask = reduce_pc(pc_Flidar, calib) pc_reduced_Flidar = pc_Flidar[mask, :] with open(reduced_pc_path, 'wb') as f: pc_reduced_Flidar.tofile(f) info["tag"] = tag info["pc_path"] = pc_path info["reduced_pc_path"] = reduced_pc_path info["img_path"] = img_path info["calib"] = calib info["label"] = label infos.append(info)
def vis_fn(idx): global dataset, data_dir, output_dir if dataset == "KITTI": from det3.dataloader.kittidata import KittiData calib, img, label, pc = KittiData(data_dir, idx).read_data() elif dataset == "CARLA": from det3.dataloader.carladata import CarlaData import numpy as np pc_dict, label, calib = CarlaData(data_dir, idx).read_data() pc = np.vstack([ calib.lidar2imu(v, key="Tr_imu_to_{}".format(k)) for k, v in pc_dict.items() ]) elif dataset == "UDI": from det3.dataloader.udidata import UdiData, UdiFrame import numpy as np udidata = UdiData(data_dir, idx).read_data() calib = udidata["calib"] label = udidata["label"] pc_dict = udidata["lidar"] for k, v in pc_dict.items(): pc_dict[k] = calib.transform(v[:, :3], source_frame=UdiFrame( UdiData.lidar_to_frame(k)), target_frame=UdiFrame("BASE")) pc_merge = np.vstack([v for k, v in pc_dict.items()]) bevimg = BEVImage(x_range=(-70, 70), y_range=(-40, 40), grid_size=(0.05, 0.05)) bevimg.from_lidar(pc_merge, scale=1) for obj in label: bevimg.draw_box(obj, calib, bool_gt=True) bevimg_img = Image.fromarray(bevimg.data) bevimg_img.save(os.path.join(output_dir, idx + '.png')) for k, v in pc_dict.items(): bevimg = BEVImage(x_range=(-70, 70), y_range=(-40, 40), grid_size=(0.05, 0.05)) bevimg.from_lidar(v, scale=1) for obj in label: bevimg.draw_box(obj, calib, bool_gt=True) bevimg_img = Image.fromarray(bevimg.data) bevimg_img.save(os.path.join(output_dir, idx + f'_{k}.png')) fv_img = FVImage() vcam_T = np.eye(4) vcam_T[:3, 3] = np.array([-3, 0, 3]) vcam_T[:3, :3] = rotz(0) @ roty(np.pi * 0.1) calib.vcam_T = vcam_T fv_img.from_lidar(calib, pc_merge, scale=2) for obj in label: fv_img.draw_3dbox(obj, calib, bool_gt=True) fv_img.save(os.path.join(output_dir, idx + '_fv.png')) return bevimg = BEVImage(x_range=(0, 70), y_range=(-40, 40), grid_size=(0.05, 0.05)) bevimg.from_lidar(pc, scale=1) for obj in label.read_label_file().data: bevimg.draw_box(obj, calib, bool_gt=True) bevimg_img = Image.fromarray(bevimg.data) bevimg_img.save(os.path.join(output_dir, idx + '.png'))
def main(is_val, data_dir, vis_dir, result_path): dt_data = pickle.load(open(result_path, 'rb')) save_dir = vis_dir if is_val: for dt in tqdm(dt_data): idx = dt['metadata']['image_idx'] idx = "{:06d}".format(idx) calib, img, label, pc = KittiData(root_dir=data_dir, idx=idx).read_data() label_est = KittiLabel() label_est.data = [] num_dt = len(dt['name']) for i in range(num_dt): obj = KittiObj() obj.type = dt['name'][i] obj.truncated = dt['truncated'][i] obj.occluded = dt['occluded'][i] obj.alpha = dt['alpha'][i] obj.bbox_l, obj.bbox_t, obj.bbox_r, obj.bbox_b = dt['bbox'][i] obj.l, obj.h, obj.w = dt['dimensions'][i] obj.x, obj.y, obj.z = dt['location'][i] obj.ry = dt['rotation_y'][i] obj.score = dt['score'][i] label_est.data.append(obj) fvimg = FVImage() fvimg.from_image(img) for obj in label.data: if (obj.x > obj.z) or (-obj.x > obj.z): continue if obj.type in ['Car', 'Van']: fvimg.draw_3dbox(obj, calib, bool_gt=True, width=3) for obj in label_est.data: if (obj.x > obj.z) or (-obj.x > obj.z): continue fvimg.draw_3dbox(obj, calib, bool_gt=False, width=2, c=(255, 255, int(255 * obj.score), 255)) fvimg_img = Image.fromarray(fvimg.data) fvimg_img.save(os.path.join(save_dir, "fv_{}.png".format(idx))) else: pc_dir = os.path.join(data_dir, "velodyne_points", "data") img2_dir = os.path.join(data_dir, "image_02", "data") calib_dir = os.path.join(data_dir, "calib") calib = read_calib(calib_dir) for dt in tqdm(dt_data): idx = dt['metadata']['image_idx'] idx = "{:010d}".format(idx) pc = read_pc_from_bin(os.path.join(pc_dir, idx + ".bin")) img = read_image(os.path.join(img2_dir, idx + ".png")) label_est = KittiLabel() label_est.data = [] num_dt = len(dt['name']) for i in range(num_dt): obj = KittiObj() obj.type = dt['name'][i] obj.truncated = dt['truncated'][i] obj.occluded = dt['occluded'][i] obj.alpha = dt['alpha'][i] obj.bbox_l, obj.bbox_t, obj.bbox_r, obj.bbox_b = dt['bbox'][i] obj.l, obj.h, obj.w = dt['dimensions'][i] obj.x, obj.y, obj.z = dt['location'][i] obj.ry = dt['rotation_y'][i] obj.score = dt['score'][i] label_est.data.append(obj) fvimg = FVImage() fvimg.from_image(img) # for obj in label.data: # if obj.type in ['Car', 'Van']: # fvimg.draw_3dbox(obj, calib, bool_gt=True, width=3) for obj in label_est.data: if (obj.x > obj.z) or (-obj.x > obj.z): continue fvimg.draw_3dbox(obj, calib, bool_gt=False, width=2, c=(255, 255, int(obj.score * 255), 255)) fvimg_img = Image.fromarray(fvimg.data) fvimg_img.save(os.path.join(save_dir, "fv_{}.png".format(idx)))