From 3ef8dae5d873e8341f9dafd92cf9c45dcff31996 Mon Sep 17 00:00:00 2001 From: Francesco Ioli Date: Sat, 13 Apr 2024 14:51:51 +0200 Subject: [PATCH] updated export to openmvg and export to db to read camera option file --- main.py | 6 ++--- src/deep_image_matching/io/h5_to_db.py | 4 +-- src/deep_image_matching/io/h5_to_openmvg.py | 28 +++++++++++++++++++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index daad46a..c1e40b7 100644 --- a/main.py +++ b/main.py @@ -21,15 +21,13 @@ feature_path, match_path = matcher.run() # Export in colmap format -with open(config.general["camera_options"], "r") as file: - camera_options = yaml.safe_load(file) database_path = output_dir / "database.db" dim.io.export_to_colmap( img_dir=imgs_dir, feature_path=feature_path, match_path=match_path, database_path=database_path, - camera_options=camera_options, + camera_config_path=config.general["camera_options"], ) # Visualize view graph @@ -56,7 +54,7 @@ openmvg_out_path=openmvg_out_path, openmvg_sfm_bin=openmvg_sfm_bin, openmvg_database=openmvg_database, - camera_options=camera_options, + camera_config_path=config.general["camera_options"], ) # Reconstruction with OpenMVG diff --git a/src/deep_image_matching/io/h5_to_db.py b/src/deep_image_matching/io/h5_to_db.py index 28ef75e..406dcdf 100644 --- a/src/deep_image_matching/io/h5_to_db.py +++ b/src/deep_image_matching/io/h5_to_db.py @@ -32,7 +32,7 @@ logger = logging.getLogger("dim") -default_camera_options = { +DEFAULT_CAM_OPTIONS = { "general": { "single_camera": False, "camera_model": "simple-radial", @@ -85,7 +85,7 @@ def export_to_colmap( with open(camera_config_path, "r") as file: camera_options = yaml.safe_load(file) else: - camera_options = default_camera_options + camera_options = DEFAULT_CAM_OPTIONS # Create the database and add keypoints and matches db = COLMAPDatabase.connect(database_path) diff --git a/src/deep_image_matching/io/h5_to_openmvg.py b/src/deep_image_matching/io/h5_to_openmvg.py index 9072dec..2175ea5 100644 --- a/src/deep_image_matching/io/h5_to_openmvg.py +++ b/src/deep_image_matching/io/h5_to_openmvg.py @@ -14,6 +14,7 @@ import h5py import numpy as np +import yaml from PIL import Image from tqdm import tqdm @@ -26,6 +27,13 @@ "pinhole_brown_t2": "disto_t2", } +DEFAULT_CAM_OPTIONS = { + "general": { + "single_camera": False, + "openmvg_camera_model": "pinhole_radial_k3", + }, +} + def loadJSON(sfm_data): with open(sfm_data) as file: @@ -331,7 +339,7 @@ def export_to_openmvg( feature_path: Path, match_path: Path, openmvg_out_path: Path, - camera_options: dict, + camera_config_path: Path, openmvg_sfm_bin: Path = None, openmvg_database: Path = None, ) -> None: @@ -345,7 +353,7 @@ def export_to_openmvg( feature_path (Path): Path to the feature file (HDF5 format). match_path (Path): Path to the match file (HDF5 format). openmvg_out_path (Path): Path to the desired output directory for the OpenMVG project. - camera_options (dict): Camera configuration options. + camera_config_path (Path): Path to the camera options yaml file. openmvg_sfm_bin (Path, optional): Path to the OpenMVG SfM executable. If not provided, attempts to find it automatically (Linux only). openmvg_database (Path, optional): Path to the OpenMVG sensor width database. @@ -358,6 +366,22 @@ def export_to_openmvg( os.rmdir(openmvg_out_path) openmvg_out_path.mkdir(parents=True) + if not img_dir.exists(): + raise FileNotFoundError(f"Image directory {img_dir} does not exist.") + if not feature_path.exists(): + raise FileNotFoundError(f"Feature file {feature_path} does not exist.") + if not match_path.exists(): + raise FileNotFoundError(f"Match file {match_path} does not exist.") + if not camera_config_path.exists(): + raise FileNotFoundError(f"Camera options file {camera_config_path} does not exist.") + + # If a config file is provided, read camera options, otherwise use defaults + if camera_config_path is not None: + with open(camera_config_path, "r") as file: + camera_options = yaml.safe_load(file) + else: + camera_options = DEFAULT_CAM_OPTIONS + # NOTE: this part meybe is not needed here... if openmvg_sfm_bin is None: # Try to find openMVG binaries (only on linux)