Download code/prepare_geometry.py from Sirenna0796/depth: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/datasets/Sirenna0796/depth/resolve/main/code/prepare_geometry.py
- Command line
-
hf download hf://datasets/Sirenna0796/depth/code/prepare_geometry.py
-
curl -L -o prepare_geometry.py https://huggingface.co/datasets/Sirenna0796/depth/resolve/main/code/prepare_geometry.py
3.19 kB
| """Convert recorded intrinsics and optional exported ARKit poses to per-frame geometry.""" | |
| import argparse | |
| import csv | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| from scipy.spatial.transform import Rotation | |
| def read_csv(path): | |
| with path.open(encoding='utf-8-sig', newline='') as stream: | |
| return list(csv.DictReader(line for line in stream if not line.startswith('#'))) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--intrinsics', type=Path, required=True) | |
| parser.add_argument('--poses', type=Path) | |
| parser.add_argument('--alignment', type=Path) | |
| parser.add_argument('--parameters', type=Path) | |
| parser.add_argument('--out', type=Path, required=True) | |
| args = parser.parse_args() | |
| intrinsics = read_csv(args.intrinsics) | |
| poses = alignment = None | |
| if args.poses: | |
| if not args.parameters or not args.alignment: | |
| parser.error('--poses requires --parameters and --alignment') | |
| parameters = json.loads(args.parameters.read_text(encoding='utf-8-sig')) | |
| pose_info = parameters['pose'] | |
| if (pose_info['world_alignment'] != 'gravity' or | |
| pose_info['convention'] != 'T_world_camera (parent_from_child)' or | |
| pose_info['camera_coordinates'] != 'OpenCV/Rerun RDF (x right, y down, z forward)'): | |
| raise ValueError('Expected gravity-aligned T_world_camera already exported in camera RDF coordinates') | |
| poses, alignment = read_csv(args.poses), read_csv(args.alignment) | |
| if len(alignment) != len(intrinsics): | |
| raise ValueError('Intrinsics and alignment must have the same frame count') | |
| rows = [] | |
| for index, params in enumerate(intrinsics): | |
| if int(params['frame_idx']) != index: | |
| raise ValueError('Intrinsics frame_idx must start at zero and be in order') | |
| row = dict(frame_idx=index, width=params['native_width_px'], height=params['native_height_px'], | |
| fx=params['native_fx_px'], fy=params['native_fy_px'], | |
| cx=params['native_cx_px'], cy=params['native_cy_px']) | |
| if poses is not None: | |
| match = alignment[index] | |
| pose = poses[int(match['arkit_row_index'])] | |
| if int(match['frame_idx']) != index or abs(float(pose['sensor_sec'])-float(params['sensor_sec'])) > 1e-5: | |
| raise ValueError(f'Pose/intrinsics alignment mismatch at frame {index}') | |
| if pose['tracking_state'] != 'normal': | |
| raise ValueError(f'ARKit tracking is not normal at frame {index}') | |
| q = np.array([float(pose[key]) for key in ['qx','qy','qz','qw']]) | |
| q /= np.linalg.norm(q) | |
| up = Rotation.from_quat(q).as_matrix().T @ np.array([0.,1.,0.]) | |
| row.update(stamp=match['stamp'], up_x=up[0], up_y=up[1], up_z=up[2]) | |
| rows.append(row) | |
| with args.out.open('w', encoding='utf-8', newline='') as stream: | |
| writer = csv.DictWriter(stream, fieldnames=list(rows[0])) | |
| writer.writeheader() | |
| writer.writerows(rows) | |
| print(f'Saved {len(rows)} frames; gravity aligned: {poses is not None}') | |
| if __name__ == '__main__': | |
| main() | |