| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
|
| 4 |
import json |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
|
| 8 |
import pycolmap |
| 9 |
|
| 10 |
from ..utils import Dataset, SceneInfo |
| 11 |
from .depth_covisibility import covisibility_is_current, write_covisibility |
| 12 |
|
| 13 |
# Precomputed TartanAir panoramas have a -90 degree longitude shift: panorama |
| 14 |
# +X is NED forward, +Y is down, and +Z is left. |
| 15 |
NED_FROM_COLMAP = np.array([[1.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]) |
| 16 |
|
| 17 |
|
| 18 |
def tartanair_world_from_camera( |
| 19 |
translation: np.ndarray, quaternion_xyzw: np.ndarray |
| 20 |
) -> pycolmap.Rigid3d: |
| 21 |
"""Convert a TartanAir NED camera pose to COLMAP camera coordinates.""" |
| 22 |
world_from_ned = pycolmap.Rigid3d( |
| 23 |
pycolmap.Rotation3d(quaternion_xyzw), translation[:, np.newaxis] |
| 24 |
) |
| 25 |
# The translation defaults to zero. Passing np.zeros((3, 1)) explicitly |
| 26 |
# fails mypy with numpy 2.4, the latest version available on Python 3.11. |
| 27 |
ned_from_colmap = pycolmap.Rigid3d( |
| 28 |
rotation=pycolmap.Rotation3d(NED_FROM_COLMAP) |
| 29 |
) |
| 30 |
return world_from_ned * ned_from_colmap |
| 31 |
|
| 32 |
|
| 33 |
class DatasetTartanAir(Dataset): |
| 34 |
dataset_name = "" |
| 35 |
render_type = "" |
| 36 |
|
| 37 |
@property |
| 38 |
def position_accuracy_gt(self) -> float: |
| 39 |
return 0.001 |
| 40 |
|
| 41 |
@property |
| 42 |
def supports_covisibility_filtering(self) -> bool: |
| 43 |
return False |
| 44 |
|
| 45 |
def list_scenes(self) -> list[SceneInfo]: |
| 46 |
scene_infos: list[SceneInfo] = [] |
| 47 |
dataset_path = self.data_path / "tartanair-v2" |
| 48 |
if not dataset_path.exists(): |
| 49 |
return scene_infos |
| 50 |
|
| 51 |
for category_path in sorted(dataset_path.iterdir()): |
| 52 |
if not category_path.is_dir() or ( |
| 53 |
self.categories and category_path.name not in self.categories |
| 54 |
): |
| 55 |
continue |
| 56 |
category = category_path.name |
| 57 |
for scene_path in sorted(category_path.iterdir()): |
| 58 |
if not scene_path.is_dir(): |
| 59 |
continue |
| 60 |
scene = scene_path.name |
| 61 |
if self.scenes and scene not in self.scenes: |
| 62 |
continue |
| 63 |
|
| 64 |
metadata_path = scene_path / "scene.json" |
| 65 |
if not metadata_path.exists(): |
| 66 |
continue |
| 67 |
metadata = json.loads(metadata_path.read_text()) |
| 68 |
workspace_path = ( |
| 69 |
self.run_path |
| 70 |
/ self.run_name |
| 71 |
/ self.dataset_name |
| 72 |
/ category |
| 73 |
/ scene |
| 74 |
) |
| 75 |
reconstruction_subdir = ( |
| 76 |
"sparse_equirectangular" |
| 77 |
if self.render_type == "perspective_overlapping" |
| 78 |
else "sparse" |
| 79 |
) |
| 80 |
scene_infos.append( |
| 81 |
SceneInfo( |
| 82 |
dataset=self.dataset_name, |
| 83 |
category=category, |
| 84 |
scene=scene, |
| 85 |
num_images=len(metadata["frames"]), |
| 86 |
workspace_path=workspace_path, |
| 87 |
image_path=scene_path / "images", |
| 88 |
sparse_gt_path=scene_path / "sparse_gt", |
| 89 |
has_camera_priors=False, |
| 90 |
colmap_extra_args=[], |
| 91 |
reconstruction_backend=f"panorama-{self.render_type}", |
| 92 |
reconstruction_subdir=reconstruction_subdir, |
| 93 |
covisibility_path=scene_path / "covisibility.npz", |
| 94 |
covisibility_min_shared_points=1, |
| 95 |
) |
| 96 |
) |
| 97 |
return scene_infos |
| 98 |
|
| 99 |
def prepare_scene(self, scene_info: SceneInfo) -> None: |
| 100 |
scene_path = scene_info.image_path.parent |
| 101 |
metadata = json.loads((scene_path / "scene.json").read_text()) |
| 102 |
image_names = [frame["image_name"] for frame in metadata["frames"]] |
| 103 |
depth_path = scene_path / "depth" |
| 104 |
covisibility_path = scene_info.covisibility_path |
| 105 |
if covisibility_path is None or not depth_path.exists(): |
| 106 |
needs_covisibility = False |
| 107 |
covisibility_current = True |
| 108 |
else: |
| 109 |
needs_covisibility = True |
| 110 |
covisibility_current = covisibility_is_current( |
| 111 |
covisibility_path, image_names |
| 112 |
) |
| 113 |
if scene_info.sparse_gt_path.exists() and covisibility_current: |
| 114 |
return |
| 115 |
|
| 116 |
pose_by_frame = {} |
| 117 |
with open(scene_path / "poses.txt", encoding="ascii") as fid: |
| 118 |
for line in fid: |
| 119 |
values = line.split() |
| 120 |
if not values: |
| 121 |
continue |
| 122 |
frame_id = int(values[0]) |
| 123 |
pose_by_frame[frame_id] = np.array(values[1:8], dtype=float) |
| 124 |
|
| 125 |
world_from_cameras = [] |
| 126 |
for frame_info in metadata["frames"]: |
| 127 |
pose = pose_by_frame[frame_info["source_frame"]] |
| 128 |
world_from_camera_pose = tartanair_world_from_camera( |
| 129 |
pose[:3], pose[3:] |
| 130 |
) |
| 131 |
matrix = np.eye(4) |
| 132 |
matrix[:3, :3] = world_from_camera_pose.rotation.matrix() |
| 133 |
matrix[:3, 3] = world_from_camera_pose.translation |
| 134 |
world_from_cameras.append(matrix) |
| 135 |
|
| 136 |
if scene_info.sparse_gt_path.exists(): |
| 137 |
reconstruction = None |
| 138 |
else: |
| 139 |
width, height = metadata["image_size"] |
| 140 |
reconstruction = pycolmap.Reconstruction() |
| 141 |
camera = pycolmap.Camera( |
| 142 |
camera_id=1, |
| 143 |
model=pycolmap.CameraModelId.EQUIRECTANGULAR, |
| 144 |
width=width, |
| 145 |
height=height, |
| 146 |
params=[width, height], |
| 147 |
) |
| 148 |
rig = pycolmap.Rig(rig_id=1) |
| 149 |
rig.add_ref_sensor(camera.sensor_id) |
| 150 |
reconstruction.add_camera(camera) |
| 151 |
reconstruction.add_rig(rig) |
| 152 |
|
| 153 |
if reconstruction is not None: |
| 154 |
for image_id, (frame_info, world_from_camera_matrix) in enumerate( |
| 155 |
zip(metadata["frames"], world_from_cameras, strict=True), |
| 156 |
start=1, |
| 157 |
): |
| 158 |
image = pycolmap.Image( |
| 159 |
image_id=image_id, |
| 160 |
camera_id=camera.camera_id, |
| 161 |
name=frame_info["image_name"], |
| 162 |
) |
| 163 |
image.frame_id = image_id |
| 164 |
frame = pycolmap.Frame(frame_id=image_id) |
| 165 |
frame.rig_id = rig.rig_id |
| 166 |
frame.add_data_id(image.data_id) |
| 167 |
frame.rig_from_world = pycolmap.Rigid3d( |
| 168 |
pycolmap.Rotation3d(world_from_camera_matrix[:3, :3]), |
| 169 |
world_from_camera_matrix[:3, 3], |
| 170 |
).inverse() |
| 171 |
reconstruction.add_frame(frame) |
| 172 |
reconstruction.add_image(image) |
| 173 |
|
| 174 |
scene_info.sparse_gt_path.mkdir(parents=True) |
| 175 |
reconstruction.write(scene_info.sparse_gt_path) |
| 176 |
|
| 177 |
if ( |
| 178 |
covisibility_path is not None |
| 179 |
and needs_covisibility |
| 180 |
and not covisibility_current |
| 181 |
): |
| 182 |
write_covisibility( |
| 183 |
covisibility_path, |
| 184 |
[ |
| 185 |
depth_path / frame["depth_name"] |
| 186 |
for frame in metadata["frames"] |
| 187 |
], |
| 188 |
image_names, |
| 189 |
np.stack(world_from_cameras), |
| 190 |
metadata["depth_scale"], |
| 191 |
) |
| 192 |
|
| 193 |
|
| 194 |
class DatasetTartanAirPerspective(DatasetTartanAir): |
| 195 |
dataset_name = "tartanair-v2-perspective" |
| 196 |
render_type = "perspective_overlapping" |
| 197 |
|
| 198 |
|
| 199 |
class DatasetTartanAirSpherical(DatasetTartanAir): |
| 200 |
dataset_name = "tartanair-v2-spherical" |
| 201 |
render_type = "spherical" |