-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolmap_flatten_dirs.py
84 lines (65 loc) · 2.24 KB
/
colmap_flatten_dirs.py
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
#!/usr/bin/env python3
"""
Flatten the directory structure of images and COLMAP model
- images will be all moved to the same directory - their relative path will be
preserved in their basename (replace / with _)
- COLMAP model will be adjusted to match the image paths
"""
import os
import argparse
import shutil
import pycolmap
parser = argparse.ArgumentParser(
description="Flatten directory structure of images and COLMAP model"
)
parser.add_argument(
"--input_images",
type=str,
help="Input images directory (the root of the directory structure - must match the COLMAP model)",
)
parser.add_argument(
"--output_images",
type=str,
help="Output images directory"
)
parser.add_argument(
"--input_colmap",
type=str,
help="Input COLMAP model directory"
)
parser.add_argument(
"--output_colmap",
type=str,
help="Output COLMAP model directory"
)
def main(args):
if args.input_colmap is not None and args.output_colmap is not None:
print("- reading COLMAP model")
model = pycolmap.Reconstruction(args.input_colmap)
input_image_list = [img.name for img in model.images.values()]
print(input_image_list)
print("- adjusting COLMAP model")
adjust_colmap_model(model)
print("- writing COLMAP model")
model.write_text(args.output_colmap)
if args.input_images is not None and args.output_images is not None:
print("- flattening images")
flatten_images(args.input_images, args.output_images, input_image_list)
def adjust_colmap_model(model):
for img in model.images.values():
img.name = img.name.replace("/", "_")
def flatten_images(input_images, output_images, input_image_list=None):
for dp, dn, filenames in os.walk(input_images):
for f in filenames:
src = os.path.join(dp, f)
if input_image_list is not None:
src_rel = os.path.relpath(src, input_images)
if src_rel not in input_image_list:
continue
dst = os.path.join(
output_images, os.path.relpath(src, input_images).replace("/", "_")
)
shutil.copy(src, dst)
if __name__ == "__main__":
args = parser.parse_args()
main(args)