Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

run_colmap.py 3.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. import os
  2. import sys
  3. import click
  4. import shutil
  5. from pathlib import Path
  6. def do_system(arg):
  7. print(f"==== running: {arg}")
  8. err = os.system(arg)
  9. if err:
  10. print("FATAL: command failed")
  11. sys.exit(err)
  12. @click.command()
  13. @click.option("--images", default="images", help="input path to the images")
  14. @click.option("--colmap_db", default="colmap.db", help="colmap database filename")
  15. @click.option(
  16. "--text",
  17. default="colmap_text",
  18. help="input path to the colmap text files",
  19. )
  20. @click.option(
  21. "--colmap_camera_model",
  22. default="OPENCV",
  23. type=click.Choice(
  24. ["SIMPLE_PINHOLE", "PINHOLE", "SIMPLE_RADIAL", "RADIAL", "OPENCV"]
  25. ),
  26. help="camera model",
  27. )
  28. @click.option(
  29. "--colmap_camera_params",
  30. default="",
  31. help="intrinsic parameters, depending on the chosen model"
  32. "Format: fx,fy,cx,cy,dist",
  33. )
  34. @click.option(
  35. "--colmap_matcher",
  36. default="sequential",
  37. type=click.Choice(
  38. ["exhaustive", "sequential", "spatial", "transitive", "vocab_tree"]
  39. ),
  40. help="select which matcher colmap should use. sequential for videos,"
  41. " exhaustive for adhoc images",
  42. )
  43. def run_colmap(
  44. images, colmap_db, text, colmap_camera_model, colmap_camera_params, colmap_matcher
  45. ):
  46. # vars preprocessing
  47. print("start run_colmap")
  48. os.makedirs(Path(colmap_db).parent, exist_ok=True)
  49. # images
  50. images = '"' + images + '"'
  51. # db
  52. db = colmap_db
  53. db_noext = str(Path(db).with_suffix(""))
  54. # text
  55. if text == "text":
  56. text = db_noext + "_text"
  57. # sparse
  58. sparse = db_noext + "_sparse"
  59. print(
  60. f"running colmap with:"
  61. f"\n\timages={images}\n\tdb={db}\n\tsparse={sparse}\n\ttext={text}"
  62. )
  63. warning_msg = (
  64. input(
  65. f"warning! folders '{sparse}' and '{text}' "
  66. f"will be deleted/replaced. continue? (Y/n)"
  67. )
  68. .lower()
  69. .strip()
  70. + "y"
  71. )
  72. if warning_msg[:1] != "y":
  73. sys.exit(1)
  74. if os.path.exists(db):
  75. os.remove(db)
  76. do_system(
  77. f"colmap feature_extractor "
  78. f"--ImageReader.camera_model {colmap_camera_model} "
  79. f'--ImageReader.camera_params "{colmap_camera_params}" '
  80. f"--SiftExtraction.estimate_affine_shape=true "
  81. f"--SiftExtraction.domain_size_pooling=true "
  82. f"--ImageReader.single_camera 1 "
  83. f"--database_path {db} "
  84. f"--image_path {images}"
  85. )
  86. do_system(
  87. f"colmap {colmap_matcher}_matcher "
  88. f"--SiftMatching.guided_matching=true "
  89. f"--database_path {db}"
  90. )
  91. try:
  92. shutil.rmtree(sparse)
  93. except:
  94. pass
  95. do_system(f"mkdir {sparse}")
  96. do_system(
  97. f"colmap mapper "
  98. f"--database_path {db} "
  99. f"--image_path {images} "
  100. f"--output_path {sparse}"
  101. )
  102. do_system(
  103. f"colmap bundle_adjuster "
  104. f"--input_path {sparse}/0 "
  105. f"--output_path {sparse}/0 --BundleAdjustment.refine_principal_point 1"
  106. )
  107. try:
  108. shutil.rmtree(text)
  109. except:
  110. pass
  111. do_system(f"mkdir {text}")
  112. do_system(
  113. f"colmap model_converter "
  114. f"--input_path {sparse}/0 "
  115. f"--output_path {text} "
  116. f"--output_type TXT"
  117. )
  118. if __name__ == "__main__":
  119. run_colmap()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...