Read a staged sparse reference cloud and all camera frustums.
(data_dir, sparse_path, verbose=True)
| 204 | |
| 205 | |
| 206 | def load_sparse_reference(data_dir, sparse_path, verbose=True): |
| 207 | """Read a staged sparse reference cloud and all camera frustums.""" |
| 208 | payload = np.load(sparse_path) |
| 209 | pts = payload["points"].astype(np.float32) |
| 210 | colors = payload["colors"] |
| 211 | if colors.dtype == np.uint8: |
| 212 | colors = colors.astype(np.uint8) |
| 213 | else: |
| 214 | colors = colors.astype(np.float32) |
| 215 | if colors.max(initial=0.0) <= 1.0: |
| 216 | colors = (np.clip(colors, 0.0, 1.0) * 255).astype(np.uint8) |
| 217 | else: |
| 218 | colors = np.clip(colors, 0.0, 255.0).astype(np.uint8) |
| 219 | conf = np.full((len(pts),), 2.0, dtype=np.float32) |
| 220 | |
| 221 | cam_files = sorted(glob.glob(os.path.join(data_dir, "camera", "*.npz"))) |
| 222 | if not cam_files: |
| 223 | raise ValueError(f"No cameras found under {data_dir}/camera/") |
| 224 | cam_c2ws, cam_intrinsics, frame_ids = [], [], [] |
| 225 | for f in cam_files: |
| 226 | cam = np.load(f) |
| 227 | pose = cam["pose"] |
| 228 | if pose.shape == (3, 4): |
| 229 | pose_4x4 = np.eye(4, dtype=pose.dtype) |
| 230 | pose_4x4[:3, :] = pose |
| 231 | pose = pose_4x4 |
| 232 | cam_c2ws.append(pose.astype(np.float32)) |
| 233 | cam_intrinsics.append(cam["intrinsics"].astype(np.float32)) |
| 234 | frame_ids.append(int(os.path.splitext(os.path.basename(f))[0])) |
| 235 | |
| 236 | if verbose: |
| 237 | print(f" loaded sparse reference: {len(pts):,} points, {len(cam_c2ws)} cameras") |
| 238 | return { |
| 239 | "points": pts, |
| 240 | "colors": colors, |
| 241 | "conf": conf, |
| 242 | "cam_c2ws": np.stack(cam_c2ws), |
| 243 | "cam_intrinsics": np.stack(cam_intrinsics), |
| 244 | "frame_ids": np.array(frame_ids, dtype=np.int64), |
| 245 | "frame_offsets": np.array([0, len(pts)], dtype=np.int64), |
| 246 | "has_per_frame_points": False, |
| 247 | } |
| 248 | |
| 249 | |
| 250 | def _stride_downsample(pts, colors, conf, target_count): |