From 7f73071efdb64c6ff46d61b5516a8ca44007eb9f Mon Sep 17 00:00:00 2001 From: Boris Lachev Date: Sat, 1 Feb 2025 13:11:05 +0200 Subject: [PATCH 1/5] Walk model folders to reliably get best reconstruction --- hloc/reconstruction.py | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index ea1e7fc0..06233371 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -84,23 +84,37 @@ def run_reconstruction( return None logger.info(f"Reconstructed {len(reconstructions)} model(s).") - largest_index = None + # Find the largest reconstruction by examining model folders + largest_folder = None largest_num_images = 0 - for index, rec in reconstructions.items(): - num_images = rec.num_reg_images() - if num_images > largest_num_images: - largest_index = index - largest_num_images = num_images - assert largest_index is not None - logger.info( - f"Largest model is #{largest_index} " f"with {largest_num_images} images." - ) + for model_dir in models_path.iterdir(): + if not model_dir.is_dir(): + continue + # Load reconstruction from the binary files + try: + rec = pycolmap.Reconstruction(model_dir) + num_images = rec.num_reg_images() + if num_images > largest_num_images: + largest_folder = model_dir + largest_num_images = num_images + except Exception as e: + logger.warning(f"Could not load reconstruction from {model_dir}: {e}") + continue + + if largest_folder is None: + logger.error("Could not find any valid reconstruction!") + return None + + logger.info(f"Largest model is in {largest_folder.name} with {largest_num_images} images.") + # Move the files from the largest model for filename in ["images.bin", "cameras.bin", "points3D.bin"]: if (sfm_dir / filename).exists(): (sfm_dir / filename).unlink() - shutil.move(str(models_path / str(largest_index) / filename), str(sfm_dir)) - return reconstructions[largest_index] + shutil.move(str(largest_folder / filename), str(sfm_dir)) + + # Return the reconstruction object for the largest model + return pycolmap.Reconstruction(sfm_dir) def main( From 564a237f7425d43bab308bc4adc4f83c43184e38 Mon Sep 17 00:00:00 2001 From: Boris Lachev Date: Sun, 2 Feb 2025 11:47:54 +0200 Subject: [PATCH 2/5] Update logging, assert folder is found --- hloc/reconstruction.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 06233371..1dfd39c0 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -84,13 +84,12 @@ def run_reconstruction( return None logger.info(f"Reconstructed {len(reconstructions)} model(s).") - # Find the largest reconstruction by examining model folders + # Find the largest reconstruction by examining model folders #442 largest_folder = None largest_num_images = 0 for model_dir in models_path.iterdir(): if not model_dir.is_dir(): continue - # Load reconstruction from the binary files try: rec = pycolmap.Reconstruction(model_dir) num_images = rec.num_reg_images() @@ -100,20 +99,15 @@ def run_reconstruction( except Exception as e: logger.warning(f"Could not load reconstruction from {model_dir}: {e}") continue + assert largest_folder is not None - if largest_folder is None: - logger.error("Could not find any valid reconstruction!") - return None - - logger.info(f"Largest model is in {largest_folder.name} with {largest_num_images} images.") + logger.info(f"Largest model is {largest_folder.name} with {largest_num_images} images.") - # Move the files from the largest model for filename in ["images.bin", "cameras.bin", "points3D.bin"]: if (sfm_dir / filename).exists(): (sfm_dir / filename).unlink() shutil.move(str(largest_folder / filename), str(sfm_dir)) - # Return the reconstruction object for the largest model return pycolmap.Reconstruction(sfm_dir) From f0f1f895a3b1a037acea6278c11fdc1eb7a23373 Mon Sep 17 00:00:00 2001 From: Boris Lachev Date: Sun, 2 Feb 2025 11:49:53 +0200 Subject: [PATCH 3/5] Use correct issue # --- hloc/reconstruction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 1dfd39c0..49eb8c1f 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -84,7 +84,7 @@ def run_reconstruction( return None logger.info(f"Reconstructed {len(reconstructions)} model(s).") - # Find the largest reconstruction by examining model folders #442 + # Find the largest reconstruction by examining model folders #422 largest_folder = None largest_num_images = 0 for model_dir in models_path.iterdir(): From b3d2a21a3c9ceb09086557906c2253289d1e760b Mon Sep 17 00:00:00 2001 From: Boris Lachev Date: Sun, 2 Feb 2025 11:52:58 +0200 Subject: [PATCH 4/5] Fix lints --- hloc/reconstruction.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 49eb8c1f..919c39d4 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -84,7 +84,6 @@ def run_reconstruction( return None logger.info(f"Reconstructed {len(reconstructions)} model(s).") - # Find the largest reconstruction by examining model folders #422 largest_folder = None largest_num_images = 0 for model_dir in models_path.iterdir(): @@ -101,13 +100,16 @@ def run_reconstruction( continue assert largest_folder is not None - logger.info(f"Largest model is {largest_folder.name} with {largest_num_images} images.") + logger.info( + f"Largest model is {largest_folder.name} " + f"with {largest_num_images} images." + ) for filename in ["images.bin", "cameras.bin", "points3D.bin"]: if (sfm_dir / filename).exists(): (sfm_dir / filename).unlink() shutil.move(str(largest_folder / filename), str(sfm_dir)) - + return pycolmap.Reconstruction(sfm_dir) From 6033bc05004bedeb5d39d183c59526cfeee6dd45 Mon Sep 17 00:00:00 2001 From: Boris Lachev Date: Sun, 2 Feb 2025 11:56:18 +0200 Subject: [PATCH 5/5] Black format --- hloc/reconstruction.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 919c39d4..e1132a46 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -101,8 +101,7 @@ def run_reconstruction( assert largest_folder is not None logger.info( - f"Largest model is {largest_folder.name} " - f"with {largest_num_images} images." + f"Largest model is {largest_folder.name} " f"with {largest_num_images} images." ) for filename in ["images.bin", "cameras.bin", "points3D.bin"]: