From 85ee75d6c69b0b5b521a50143c1bd7392c663103 Mon Sep 17 00:00:00 2001 From: Xiaoyu Ye Date: Sun, 8 Sep 2024 17:15:56 -0700 Subject: [PATCH 1/3] changes sparse output dir --- hloc/reconstruction.py | 9 +++++---- requirements.txt | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index ea1e7fc0..7b5df51d 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -68,7 +68,8 @@ def run_reconstruction( options: Optional[Dict[str, Any]] = None, ) -> pycolmap.Reconstruction: models_path = sfm_dir / "models" - models_path.mkdir(exist_ok=True, parents=True) + sparse_dir = sfm_dir / "sparse" + sparse_dir.mkdir(exist_ok=True, parents=True) logger.info("Running 3D reconstruction...") if options is None: options = {} @@ -97,9 +98,9 @@ def run_reconstruction( ) 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)) + if (sparse_dir / filename).exists(): + (sparse_dir / filename).unlink() + shutil.move(str(models_path / str(largest_index) / filename), str(sparse_dir)) return reconstructions[largest_index] diff --git a/requirements.txt b/requirements.txt index 609c61fb..dab88555 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,4 +10,4 @@ h5py pycolmap>=0.6.0 kornia>=0.6.11 gdown -lightglue @ git+https://github.com/cvg/LightGlue +# lightglue @ git+https://github.com/cvg/LightGlue From 922585c4a0bccba4da409ade7558bab5a0a00a48 Mon Sep 17 00:00:00 2001 From: jcheng14 Date: Fri, 4 Oct 2024 07:12:40 -0700 Subject: [PATCH 2/3] Update reconstruction.py to fix the correct largest model selection --- hloc/reconstruction.py | 66 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 7b5df51d..61810247 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -69,7 +69,8 @@ def run_reconstruction( ) -> pycolmap.Reconstruction: models_path = sfm_dir / "models" sparse_dir = sfm_dir / "sparse" - sparse_dir.mkdir(exist_ok=True, parents=True) + assert not sparse_dir.exists(), "sparse directory already exists" + sparse_dir.mkdir(exist_ok=False, parents=True) logger.info("Running 3D reconstruction...") if options is None: options = {} @@ -85,23 +86,64 @@ def run_reconstruction( return None logger.info(f"Reconstructed {len(reconstructions)} model(s).") - largest_index = None + # Get the list of model directories + model_dirs = [d for d in models_path.iterdir() if d.is_dir()] largest_num_images = 0 - for index, rec in reconstructions.items(): + largest_model_dir = None + largest_reconstruction = None + + # Iterate over model directories + for model_dir in model_dirs: + rec = pycolmap.Reconstruction(model_dir) num_images = rec.num_reg_images() + logger.info(f"Model {model_dir.name} has {num_images} 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." - ) + largest_model_dir = model_dir + largest_reconstruction = rec + + if largest_model_dir is None: + logger.error("No valid models found.") + return None + + logger.info(f"Largest model is {largest_model_dir.name} with {largest_num_images} images.") + + # Clean up the sparse_dir before copying new files + for existing_file in sparse_dir.glob("*"): + logger.info(f"Deleting existing file: {existing_file}") + existing_file.unlink() + # Copy files from the largest model directory for filename in ["images.bin", "cameras.bin", "points3D.bin"]: - if (sparse_dir / filename).exists(): - (sparse_dir / filename).unlink() - shutil.move(str(models_path / str(largest_index) / filename), str(sparse_dir)) - return reconstructions[largest_index] + source_file = largest_model_dir / filename + target_file = sparse_dir / filename + logger.info(f"Copying file: {source_file} -> {target_file}") + shutil.copy2(str(source_file), str(target_file)) + logger.info(f"File copied successfully: {target_file.exists()}") + + # Load and summarize the reconstruction from sparse_dir + sparse_reconstruction = pycolmap.Reconstruction(sparse_dir) + logger.info(f"Reconstruction summary (sparse_dir model):\n{sparse_reconstruction.summary()}") + + # Delete other models + logger.info("Deleting other models...") + for model_dir in model_dirs: + if model_dir != largest_model_dir: + try: + shutil.rmtree(model_dir) + logger.info(f"Successfully deleted model {model_dir.name}") + except Exception as e: + logger.error(f"Error deleting model {model_dir.name}: {str(e)}") + + # Confirm all other models are deleted + remaining_models = list(models_path.glob("*")) + if len(remaining_models) == 1 and remaining_models[0] == largest_model_dir: + logger.info("All other models successfully deleted") + else: + logger.warning(f"Unexpected models remaining: {remaining_models}") + + # Return the largest reconstruction + return largest_reconstruction def main( From dbc23d51858863caf723514e09422bc244dcdfaf Mon Sep 17 00:00:00 2001 From: felix cheng Date: Fri, 4 Oct 2024 11:25:35 -0700 Subject: [PATCH 3/3] fix a typo in reconstruction.py --- hloc/reconstruction.py | 1 + 1 file changed, 1 insertion(+) diff --git a/hloc/reconstruction.py b/hloc/reconstruction.py index 61810247..bceae794 100644 --- a/hloc/reconstruction.py +++ b/hloc/reconstruction.py @@ -1,6 +1,7 @@ import argparse import multiprocessing import shutil +import logging from pathlib import Path from typing import Any, Dict, List, Optional