@@ -60,23 +60,24 @@ def get_image_ids(database_path):
6060 return images
6161
6262
63- def run_reconstruction (colmap_path , model_path , database_path , image_dir ,
63+ def run_reconstruction (colmap_path , sfm_dir , database_path , image_dir ,
6464 min_num_matches = None ):
6565 logging .info ('Running the 3D reconstruction...' )
66- model_path .mkdir (exist_ok = True )
66+ models_path = sfm_dir / 'models'
67+ models_path .mkdir (exist_ok = True , parents = True )
6768
6869 cmd = [
6970 str (colmap_path ), 'mapper' ,
7071 '--database_path' , str (database_path ),
7172 '--image_path' , str (image_dir ),
72- '--output_path' , str (model_path ),
73+ '--output_path' , str (models_path ),
7374 '--Mapper.num_threads' , str (min (multiprocessing .cpu_count (), 16 ))]
7475 if min_num_matches :
7576 cmd += ['--Mapper.min_num_matches' , str (min_num_matches )]
7677 logging .info (' ' .join (cmd ))
7778 subprocess .run (cmd , check = True )
7879
79- models = list (model_path .iterdir ())
80+ models = list (models_path .iterdir ())
8081 if len (models ) == 0 :
8182 logging .error ('Could not reconstruct any model!' )
8283 return False
@@ -91,7 +92,7 @@ def run_reconstruction(colmap_path, model_path, database_path, image_dir,
9192 largest_model_num_images = num_images
9293 assert largest_model_num_images > 0
9394 logging .info (f'Largest model is #{ largest_model .name } '
94- 'with {largest_model_num_images} images.' )
95+ f 'with { largest_model_num_images } images.' )
9596
9697 stats_raw = subprocess .check_output (
9798 [str (colmap_path ), 'model_analyzer' ,
@@ -112,6 +113,9 @@ def run_reconstruction(colmap_path, model_path, database_path, image_dir,
112113 elif stat .startswith ("Mean reprojection error" ):
113114 stats ['mean_reproj_error' ] = float (stat .split ()[- 1 ][:- 2 ])
114115
116+ for filename in ['images.bin' , 'cameras.bin' , 'points3D.bin' ]:
117+ shutil .move (str (largest_model / filename ), str (sfm_dir ))
118+
115119 return stats
116120
117121
@@ -126,8 +130,6 @@ def main(sfm_dir, image_dir, pairs, features, matches,
126130
127131 sfm_dir .mkdir (parents = True , exist_ok = True )
128132 database = sfm_dir / 'database.db'
129- models = sfm_dir / 'models'
130- models .mkdir (exist_ok = True )
131133
132134 create_empty_db (database )
133135 import_images (
@@ -139,7 +141,7 @@ def main(sfm_dir, image_dir, pairs, features, matches,
139141 if not skip_geometric_verification :
140142 geometric_verification (colmap_path , database , pairs )
141143 stats = run_reconstruction (
142- colmap_path , models , database , image_dir , min_num_matches )
144+ colmap_path , sfm_dir , database , image_dir , min_num_matches )
143145 stats ['num_input_images' ] = len (image_ids )
144146 logging .info (f'Statistics:\n { pprint .pformat (stats )} ' )
145147
0 commit comments