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
|
- import os
- import sys
- import click
- import shutil
- from pathlib import Path
- def do_system(arg):
- print(f"==== running: {arg}")
- err = os.system(arg)
- if err:
- print("FATAL: command failed")
- sys.exit(err)
- @click.command()
- @click.option("--images", default="images", help="input path to the images")
- @click.option("--colmap_db", default="colmap.db", help="colmap database filename")
- @click.option(
- "--text",
- default="colmap_text",
- help="input path to the colmap text files",
- )
- @click.option(
- "--colmap_camera_model",
- default="OPENCV",
- type=click.Choice(
- ["SIMPLE_PINHOLE", "PINHOLE", "SIMPLE_RADIAL", "RADIAL", "OPENCV"]
- ),
- help="camera model",
- )
- @click.option(
- "--colmap_camera_params",
- default="",
- help="intrinsic parameters, depending on the chosen model"
- "Format: fx,fy,cx,cy,dist",
- )
- @click.option(
- "--colmap_matcher",
- default="sequential",
- type=click.Choice(
- ["exhaustive", "sequential", "spatial", "transitive", "vocab_tree"]
- ),
- help="select which matcher colmap should use. sequential for videos,"
- " exhaustive for adhoc images",
- )
- def run_colmap(
- images, colmap_db, text, colmap_camera_model, colmap_camera_params, colmap_matcher
- ):
- # vars preprocessing
- print("start run_colmap")
- os.makedirs(Path(colmap_db).parent, exist_ok=True)
- # images
- images = '"' + images + '"'
- # db
- db = colmap_db
- db_noext = str(Path(db).with_suffix(""))
- # text
- if text == "text":
- text = db_noext + "_text"
- # sparse
- sparse = db_noext + "_sparse"
- print(
- f"running colmap with:"
- f"\n\timages={images}\n\tdb={db}\n\tsparse={sparse}\n\ttext={text}"
- )
- warning_msg = (
- input(
- f"warning! folders '{sparse}' and '{text}' "
- f"will be deleted/replaced. continue? (Y/n)"
- )
- .lower()
- .strip()
- + "y"
- )
- if warning_msg[:1] != "y":
- sys.exit(1)
- if os.path.exists(db):
- os.remove(db)
- do_system(
- f"colmap feature_extractor "
- f"--ImageReader.camera_model {colmap_camera_model} "
- f'--ImageReader.camera_params "{colmap_camera_params}" '
- f"--SiftExtraction.estimate_affine_shape=true "
- f"--SiftExtraction.domain_size_pooling=true "
- f"--ImageReader.single_camera 1 "
- f"--database_path {db} "
- f"--image_path {images}"
- )
- do_system(
- f"colmap {colmap_matcher}_matcher "
- f"--SiftMatching.guided_matching=true "
- f"--database_path {db}"
- )
- try:
- shutil.rmtree(sparse)
- except:
- pass
- do_system(f"mkdir {sparse}")
- do_system(
- f"colmap mapper "
- f"--database_path {db} "
- f"--image_path {images} "
- f"--output_path {sparse}"
- )
- do_system(
- f"colmap bundle_adjuster "
- f"--input_path {sparse}/0 "
- f"--output_path {sparse}/0 --BundleAdjustment.refine_principal_point 1"
- )
- try:
- shutil.rmtree(text)
- except:
- pass
- do_system(f"mkdir {text}")
- do_system(
- f"colmap model_converter "
- f"--input_path {sparse}/0 "
- f"--output_path {text} "
- f"--output_type TXT"
- )
- if __name__ == "__main__":
- run_colmap()
|