-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample_vi_optimization.py
More file actions
220 lines (199 loc) · 6.08 KB
/
example_vi_optimization.py
File metadata and controls
220 lines (199 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
import shutil
from pathlib import Path
import pycolmap
from lamaria.config.options import (
KeyframeSelectorOptions,
TriangulatorOptions,
VIOptimizerOptions,
)
from lamaria.config.pipeline import PipelineOptions
from lamaria.pipeline.estimate_to_timed_reconstruction import (
convert_estimate_into_timed_reconstruction,
)
from lamaria.pipeline.keyframe_selection import KeyframeSelector
from lamaria.pipeline.optim.session import SingleSeqSession
from lamaria.pipeline.optim.vi_optimization import VIOptimizer
from lamaria.pipeline.triangulation import run as triangulate
from lamaria.structs.timed_reconstruction import TimedReconstruction
from lamaria.structs.trajectory import Trajectory
from lamaria.structs.vi_reconstruction import VIReconstruction
from lamaria.utils.aria import (
extract_images_with_timestamps_from_vrs,
get_imu_data_from_vrs_file,
initialize_reconstruction_from_vrs_file,
)
def run_estimate_to_timed_recon(
vrs: Path,
images_path: Path,
estimate: Path,
) -> TimedReconstruction:
"""Function to convert a general input
estimate file to a TimedReconstruction.
"""
traj = Trajectory.load_from_file(estimate)
init_recon = initialize_reconstruction_from_vrs_file(vrs)
timestamps_to_images = extract_images_with_timestamps_from_vrs(
vrs, images_path
)
timed_recon = convert_estimate_into_timed_reconstruction(
init_recon, traj, timestamps_to_images
)
return timed_recon
def run_keyframe_selection(
options: KeyframeSelectorOptions,
input_recon: TimedReconstruction,
images_path: Path,
keyframes_path: Path,
) -> TimedReconstruction:
kf_vi_recon = KeyframeSelector.run(
options,
input_recon,
images_path,
keyframes_path,
)
return kf_vi_recon
def run_triangulation(
options: TriangulatorOptions,
reference_model_path: Path,
keyframes_path: Path,
triangulation_path: Path,
) -> pycolmap.Reconstruction:
triangulated_model_path = triangulate(
options,
reference_model_path,
keyframes_path,
triangulation_path,
)
return pycolmap.Reconstruction(triangulated_model_path)
def run_optimization(
vi_options: VIOptimizerOptions,
triangulator_options: TriangulatorOptions,
recon: VIReconstruction,
database_path: Path,
) -> VIReconstruction:
session = SingleSeqSession(
vi_options.imu,
recon,
)
optimized_recon = VIOptimizer.optimize(
vi_options, triangulator_options, session, database_path
)
optim_vi_recon = VIReconstruction(
reconstruction=optimized_recon,
timestamps=recon.timestamps,
imu_measurements=recon.imu_measurements,
)
return optim_vi_recon
def run_pipeline(
options: PipelineOptions,
vrs: Path,
output_path: Path,
estimate: Path,
):
if not output_path.exists():
output_path.mkdir(parents=True, exist_ok=True)
recon = None
# Estimate to Lamaria Reconstruction
image_path = output_path / "images"
init_recon_path = output_path / "initial_recon"
if init_recon_path.exists():
recon = TimedReconstruction.read(init_recon_path)
else:
recon = run_estimate_to_timed_recon(
vrs,
output_path / "images",
estimate,
)
# Keyframe Selection
keyframe_path = output_path / "keyframes"
keyframed_recon_path = output_path / "keyframed_recon"
if keyframed_recon_path.exists():
recon = TimedReconstruction.read(keyframed_recon_path)
else:
recon = run_keyframe_selection(
options.keyframing_options,
recon,
image_path,
keyframe_path,
)
recon.write(output_path / "keyframed_recon")
# Triangulation
triangulation_path = output_path / "triangulated"
tri_model_path = triangulation_path / "model"
database_path = tri_model_path / "database.db"
if tri_model_path.exists():
recon = TimedReconstruction.read(tri_model_path)
else:
pycolmap_recon = run_triangulation(
options.triangulator_options,
keyframed_recon_path,
keyframe_path,
triangulation_path,
)
recon = TimedReconstruction(
reconstruction=pycolmap_recon, timestamps=recon.timestamps
)
recon.write(tri_model_path)
# Visual-Inertial Optimization
optim_model_path = output_path / "optim_recon"
imu_measurements = None
if optim_model_path.exists():
imu_measurements = VIReconstruction.read(
optim_model_path
).imu_measurements
shutil.rmtree(optim_model_path)
optim_model_path.mkdir(parents=True, exist_ok=True)
# Load IMU data
if imu_measurements is None:
imu_measurements = get_imu_data_from_vrs_file(
vrs,
)
recon = VIReconstruction(
reconstruction=recon.reconstruction,
timestamps=recon.timestamps,
imu_measurements=imu_measurements,
)
recon_optimized = run_optimization(
options.vi_optimizer_options,
options.triangulator_options,
recon,
database_path,
)
recon_optimized.write(output_path / "optim_recon")
return recon_optimized
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run full pipeline.")
parser.add_argument(
"--config",
type=str,
default="./defaults.yaml",
help="Path to the configuration YAML file.",
)
parser.add_argument(
"--vrs",
type=str,
required=True,
help="Path to the input VRS file.",
)
parser.add_argument(
"--output",
type=str,
required=True,
help="Path to the output directory.",
)
parser.add_argument(
"--estimate",
type=str,
required=True,
help="Path to the input estimate file.",
)
args = parser.parse_args()
options = PipelineOptions()
options.load(args.config)
run_pipeline(
options,
Path(args.vrs),
Path(args.output),
Path(args.estimate),
)