def testone(): # for param in model.parameters(): # print(param.data) line = '000016' print("Img = ", line) x, y, h, w, _ = load_data(line, testing=True) y_pred = model(x) loss_fn = torch.nn.MSELoss(reduction='mean') loss = loss_fn(w * y_pred, w * y) print("loss = ", loss) # print(x) # print(y_pred) y_pred = y_pred.cpu() pred_img = np.reshape(y_pred.detach().numpy(), (h, w)) disp = pred_img pred_img = (256 * pred_img).astype('uint16') # print(pred_img) # print(np.max(pred_img)) im = Image.fromarray(pred_img) directory = './disp_1d_w/' if not path.exists(directory): os.makedirs(directory) im.save(directory + line + '.png') calib_file = CALIB_PATH + line + '.txt' calib = kitti_util.Calibration(calib_file) max_high = 1 lidar = generate_lidar.project_disp_to_points(calib, disp, max_high) lidar = np.concatenate([lidar, np.ones((lidar.shape[0], 1))], 1) lidar = lidar.astype(np.float32) lidar.tofile('{}/{}.bin'.format('./pl/', line))
def test_one(index): disp = imageio.imread(DISPARITY_PATH + index + '.png') / 256 # disp = filter_one(disp) disp = test_mask(index, disp) #disp = semantic_mask(index, disp) # disp = filter_one(disp) print("Saved Disparity") imageio.imwrite('./filtered_disp/' + index + '.png', (disp * 256).astype('uint16')) calib_file = CALIB_PATH + index + '.txt' calib = kitti_util.Calibration(calib_file) max_high = 1 lidar = generate_lidar.project_disp_to_points(calib, disp, max_high) lidar = np.concatenate([lidar, np.ones((lidar.shape[0], 1))], 1) lidar = lidar.astype(np.float32) lidar.tofile('{}/{}.bin'.format('./pl/', index)) print("Saved point cloud")
def semantic_filter_all(): file1 = open('../KITTI/object/trainval.txt', 'r') lines = file1.read().splitlines() file1.close() if not os.path.exists(SAVE_PL_PATH): os.makedirs(SAVE_PL_PATH) for line in lines: index = line disp = imageio.imread(DISPARITY_PATH + index + '.png') / 256 # disp = obj_mask(index, disp) disp = dedge_obj_mask(index, disp) calib_file = CALIB_PATH + index + '.txt' calib = kitti_util.Calibration(calib_file) max_high = 1 lidar = generate_lidar.project_disp_to_points(calib, disp, max_high) lidar = np.concatenate([lidar, np.ones((lidar.shape[0], 1))], 1) lidar = lidar.astype(np.float32) lidar.tofile(SAVE_PL_PATH + index + '.bin') print("Saved point cloud # " + index)
def depth_to_disp(): DEPTH_PATH = '/home/maxc303/aer1515/ip_basic/demos/outputs/kitti_depth_ipbasic/' file1 = open('../KITTI/object/train.txt', 'r') lines = file1.read().splitlines() file1.close() if not os.path.exists(SAVE_PL_PATH): os.makedirs(SAVE_PL_PATH) for line in lines: index = line depth = imageio.imread(DEPTH_PATH + index + '.png') / 256 # disp = obj_mask(index, disp) calib_file = CALIB_PATH + index + '.txt' calib = kitti_util.Calibration(calib_file) max_high = 1 baseline = 0.54 depth[depth == 0] = -1 disp = (calib.f_u * baseline) / depth disp[disp < 0] = 0 disp[disp > 192] = 0 imageio.imwrite(SAVE_DISP_PATH + index + '.png', (disp * 256).astype('uint16')) print("Saved depth # " + index)
def run_from_file(self): if self.args_disp.KITTI == '2015': from psmnet.dataloader import KITTI_submission_loader as DA else: from psmnet.dataloader import KITTI_submission_loader2012 as DA test_left_img, test_right_img = DA.dataloader(self.args_disp.datapath) if not os.path.isdir(self.args_disp.save_path): os.makedirs(self.args_disp.save_path) # vis = o3d.visualization.Visualizer() # vis.create_window() # pcd = o3d.geometry.PointCloud() # pcd.points = o3d.utility.Vector3dVector(np.ones([100,3])) # vis.add_geometry(pcd) for inx in range(len(test_left_img)): ################ part 1 ####################### imgL_o = (skimage.io.imread(test_left_img[inx]).astype('float32')) imgR_o = (skimage.io.imread(test_right_img[inx]).astype('float32')) img = self.disp_pred_net.run(imgL_o, imgR_o) #####visualize cv2.imshow('disparity image', img.astype(np.uint8)) cv2.waitKey(1) ################## # # file output # print(test_left_img[inx].split('/')[-1]) # if self.args.save_figure: # skimage.io.imsave(self.args.save_path+'/'+test_left_img[inx].split('/')[-1],(img*256).astype('uint16')) # else: # np.save(self.args.save_path+'/'+test_left_img[inx].split('/')[-1][:-4], img) ################# part 2 ################### predix = test_left_img[inx].split('/')[-1][:-4] calib_file = '{}/{}.txt'.format(self.args_gen_lidar.calib_dir, predix) calib = kitti_util.Calibration(calib_file) img = (img * 256).astype(np.uint16) / 256. lidar, valid_idx = self.pcl_generator.run(calib, img) # pad 1 in the indensity dimension # lidar = np.concatenate([lidar, np.ones((lidar.shape[0], 1))], 1) lidar = lidar.astype(np.float32) # from float64 to float32 # lidar.tofile('{}/{}.bin'.format(self.args_gen_lidar.save_dir, predix)) print('Finish Depth {}'.format(predix)) ##### visualize # pcd.points = o3d.utility.Vector3dVector(lidar) imgL_o_flat = imgL_o.reshape([-1, 3]) imgL_o_valid = imgL_o_flat[valid_idx] / 255. # print(imgL_o_valid) # print(imgL_o_valid.shape) # print(lidar.shape) pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(lidar) pcd.colors = o3d.utility.Vector3dVector(imgL_o_valid) # pcd.paint_uniform_color([1, 0.706, 0]) o3d.visualization.draw_geometries([pcd]) # vis.update_geometry() # vis.poll_events() # vis.update_renderer() ##################### part 3 ############################ vis.destroy_window()
lidar.tofile(path) return path if __name__ == "__main__": if len(sys.argv) < 2: print('Usage ./exe 000025') sys.exit(0) img_file = sys.argv[1] + '.png' calib_file = sys.argv[1] + '.txt' depth_file = sys.argv[1] + '.png.npy' seg_file = sys.argv[1] + 's.png' gt_lidar_file = sys.argv[1] + 'gt.bin' image = cv2.imread(img_file) calib = kitti_util.Calibration(calib_file) depth_map = np.load(depth_file) seg_map = cv2.imread(seg_file, 0) seg_mask = (seg_map==13).astype(np.uint8) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10, 10)) # 椭圆 seg_mask = cv2.erode(seg_mask, kernel) > 0 gt_lidar = np.fromfile(gt_lidar_file, dtype=np.float32).reshape(-1, 4) proj_lidar = project_disp_to_depth(calib, depth_map, seg_mask) # pad 1 in the indensity dimension proj_lidar = np.concatenate([proj_lidar, np.ones((proj_lidar.shape[0], 1))], 1) out_lidar = np.concatenate([gt_lidar, proj_lidar], axis=0) f = to_bin_file(out_lidar, sys.argv[1]+'p') print('Finish Depth {} {}'.format(sys.argv[1], out_lidar.shape[0]))