def __init__(self, file_path_without_ext, has_label, use_color, box_size_x, box_size_y): """ Loads file data """ self.file_path_without_ext = file_path_without_ext self.box_size_x = box_size_x self.box_size_y = box_size_y # Load points pcd = open3d.read_point_cloud(file_path_without_ext + ".pcd") self.points = np.asarray(pcd.points) # Load label. In pure test set, fill with zeros. if has_label: self.labels = load_labels(file_path_without_ext + ".labels") else: self.labels = np.zeros(len(self.points)).astype(bool) # Load colors. If not use_color, fill with zeros. if use_color: self.colors = np.asarray(pcd.colors) else: self.colors = np.zeros_like(self.points) # Sort according to x to speed up computation of boxes and z-boxes sort_idx = np.argsort(self.points[:, 0]) self.points = self.points[sort_idx] self.labels = self.labels[sort_idx] self.colors = self.colors[sort_idx]
def down_sample( dense_pcd_path, dense_label_path, sparse_pcd_path, sparse_label_path, voxel_size ): # Skip if done if os.path.isfile(sparse_pcd_path) and ( not os.path.isfile(dense_label_path) or os.path.isfile(sparse_label_path) ): print("Skipped:", file_prefix) return else: print("Processing:", file_prefix) # Inputs dense_pcd = open3d.io.read_point_cloud(dense_pcd_path) try: dense_labels = load_labels(dense_label_path) except: dense_labels = None # Skip label 0, we use explicit frees to reduce memory usage print("Num points:", np.asarray(dense_pcd.points).shape[0]) if dense_labels is not None: non_zero_indexes = dense_labels != 0 dense_points = np.asarray(dense_pcd.points)[non_zero_indexes] dense_pcd.points = open3d.utility.Vector3dVector() dense_pcd.points = open3d.utility.Vector3dVector(dense_points) del dense_points dense_colors = np.asarray(dense_pcd.colors)[non_zero_indexes] dense_pcd.colors = open3d.utility.Vector3dVector() dense_pcd.colors = open3d.utility.Vector3dVector(dense_colors) del dense_colors dense_labels = dense_labels[non_zero_indexes] print("Num points after 0-skip:", np.asarray(dense_pcd.points).shape[0]) # Downsample points min_bound = dense_pcd.get_min_bound() - voxel_size * 0.5 max_bound = dense_pcd.get_max_bound() + voxel_size * 0.5 sparse_pcd, cubics_ids = open3d.geometry.PointCloud.voxel_down_sample_and_trace( dense_pcd, voxel_size, min_bound, max_bound, False ) print("Num points after down sampling:", np.asarray(sparse_pcd.points).shape[0]) open3d.io.write_point_cloud(sparse_pcd_path, sparse_pcd) print("Point cloud written to:", sparse_pcd_path) # Downsample labels if dense_labels is not None: sparse_labels = [] for cubic_ids in cubics_ids: cubic_ids = cubic_ids[cubic_ids != -1] cubic_labels = dense_labels[cubic_ids] sparse_labels.append(np.bincount(cubic_labels).argmax()) sparse_labels = np.array(sparse_labels) write_labels(sparse_label_path, sparse_labels) print("Labels written to:", sparse_label_path)
def colorize(input_pcd_path, input_labels_path, output_pcd_path): pcd = open3d.read_point_cloud(input_pcd_path) print("Point cloud loaded from", input_pcd_path) labels = load_labels(input_labels_path) print("Labels loaded from", input_labels_path) s = time.time() colorize_point_cloud(pcd, labels) print("time colorize_point_cloud pd", time.time() - s, flush=True) open3d.write_point_cloud(output_pcd_path, pcd) print("Output written to", output_pcd_path)
flags = parser.parse_args() # Load point cloud if not os.path.isfile(flags.pcd_path): raise ValueError("pcd path not found at {}".format(flags.pcd_path)) pcd = open3d.read_point_cloud(flags.pcd_path) # pcd = open3d.crop_point_cloud(pcd, [-30, -10, -2], [30, 10, 100]) # batched_points = np.expand_dims(np.asarray(pcd.points), axis=0) # batched_points = rotate_point_cloud(batched_points, rotation_axis="y") # pcd.points = open3d.Vector3dVector(batched_points[0]) # Load labels and colorize pcd, if labels available if flags.labels_path != "": if not os.path.isfile(flags.pcd_path): raise ValueError("labels path not found at {}".format( flags.labels_path)) labels = load_labels(flags.labels_path) colorize_point_cloud(pcd, labels) # points = np.asarray(pcd.points) # colors = np.asarray(pcd.colors) # points_with_colors = np.concatenate((points, colors), axis=1) # points_with_colors = rotate_feature_point_cloud(points_with_colors) # points = points_with_colors[:, :3] # colors = points_with_colors[:, 3:] # pcd.points = open3d.Vector3dVector(points) # pcd.colors = open3d.Vector3dVector(colors) open3d.draw_geometries([pcd])
sparse_labels_path = os.path.join(sparse_dir, file_prefix + ".labels") dense_points_path = os.path.join(gt_dir, file_prefix + ".pcd") dense_labels_path = os.path.join(dense_dir, file_prefix + ".labels") dense_points_colored_path = os.path.join( dense_dir, file_prefix + "_colored.pcd" ) dense_gt_labels_path = os.path.join(gt_dir, file_prefix + ".labels") # Sparse points sparse_pcd = open3d.io.read_point_cloud(sparse_points_path) sparse_points = np.asarray(sparse_pcd.points) del sparse_pcd print("sparse_points loaded", flush=True) # Sparse labels sparse_labels = load_labels(sparse_labels_path) print("sparse_labels loaded", flush=True) # Dense points dense_pcd = open3d.io.read_point_cloud(dense_points_path) dense_points = np.asarray(dense_pcd.points) print("dense_points loaded", flush=True) # Dense Ground-truth labels try: dense_gt_labels = load_labels(os.path.join(gt_dir, file_prefix + ".labels")) print("dense_gt_labels loaded", flush=True) except: print("dense_gt_labels not found, treat as test set") dense_gt_labels = None