-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrun_colmap.py
More file actions
80 lines (68 loc) · 2.15 KB
/
Copy pathrun_colmap.py
File metadata and controls
80 lines (68 loc) · 2.15 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
import logging
import shutil
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Annotated
import pycolmap
import tyro
from PIL import Image
from tqdm import tqdm
from tyro.conf import arg
@dataclass
class CLI:
source_path: Annotated[str, arg(aliases=["-s"])]
camera: str = "OPENCV"
gpu: bool = True
delete_input: bool = False
cli = tyro.cli(CLI)
src = Path(cli.source_path)
assert (src / "input").is_dir(), f"Input directory not found: {src / 'input'}"
(src / "distorted" / "sparse").mkdir(parents=True, exist_ok=True)
# * Feature extraction
device = pycolmap.Device.cuda if cli.gpu else pycolmap.Device.cpu
pycolmap.extract_features(
database_path=src / "distorted" / "database.db",
image_path=src / "input",
camera_mode=pycolmap.CameraMode.SINGLE,
reader_options=pycolmap.ImageReaderOptions(camera_model=cli.camera),
extraction_options=pycolmap.FeatureExtractionOptions(use_gpu=cli.gpu),
device=device,
)
# * Feature matching
pycolmap.match_exhaustive(
database_path=src / "distorted" / "database.db",
device=device,
)
# * Incremental mapping (bundle adjustment)
maps = pycolmap.incremental_mapping(
database_path=src / "distorted" / "database.db",
image_path=src / "input",
output_path=src / "distorted" / "sparse",
options=pycolmap.IncrementalPipelineOptions(
ba_global_function_tolerance=1e-6 # * speeds up bundle adjustment
),
)
if not maps:
logging.error("Incremental mapping failed. Exiting.")
raise SystemExit(1)
# * Image undistortion
pycolmap.undistort_images(
output_path=src,
input_path=src / "distorted" / "sparse" / "0",
image_path=src / "input",
output_type="COLMAP",
)
# * Flatten sparse output into sparse/0
(src / "sparse" / "0").mkdir(parents=True, exist_ok=True)
for f in (src / "sparse").iterdir():
if f.name == "0":
continue
shutil.move(str(f), str(src / "sparse" / "0" / f.name))
# * Cleanup
shutil.rmtree(src / "distorted")
shutil.rmtree(src / "stereo", ignore_errors=True)
for f in src.glob("*.sh"):
f.unlink()
if cli.delete_input:
shutil.rmtree(src / "input")