Skip to content

Commit

Permalink
small updates in export to openmvg functions
Browse files Browse the repository at this point in the history
  • Loading branch information
franioli committed Mar 14, 2024
1 parent d287b03 commit 8631727
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 104 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fra_*
sb_*
config.yaml
run_casalbagliano.sh
config/cameras_test.yaml

# VScode
.vscode/
Expand Down
3 changes: 3 additions & 0 deletions config/openmvg_linux.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
general:
path_to_binaries: null # If None, the binaries are assumed to be in the PATH
openmvg_database: null # If None, it will be downloaded from the openMVG repository
File renamed without changes.
14 changes: 4 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import os
import yaml
import subprocess
from importlib import import_module
from pathlib import Path

import yaml
from deep_image_matching import logger, timer
from deep_image_matching.config import Config
from deep_image_matching.image_matching import ImageMatching
Expand Down Expand Up @@ -72,7 +69,6 @@

# Visualize view graph
if config.general["graph"]:

try:
graph = import_module("deep_image_matching.graph")
graph.view_graph(database_path, output_dir, imgs_dir)
Expand All @@ -85,9 +81,8 @@
if config.general["openmvg_conf"]:
with open(config.general["openmvg_conf"], "r") as file:
openmvgcfg = yaml.safe_load(file)
system_OS = openmvgcfg["general"]["OS"]
openmvg_sfm_bin = Path(openmvgcfg["general"]["path_to_binaries"])
openmvg_database = Path(openmvgcfg["general"]["openmvg_database"])
openmvg_sfm_bin = openmvgcfg["general"]["path_to_binaries"]
openmvg_database = openmvgcfg["general"]["openmvg_database"]
openmvg_out_path = output_dir / "openmvg"

export_to_openmvg(
Expand All @@ -106,7 +101,6 @@
reconstruction.main(
openmvg_out_path=openmvg_out_path,
skip_reconstruction=config.general["skip_reconstruction"],
system_OS=system_OS,
openmvg_sfm_bin=openmvg_sfm_bin,
)

Expand All @@ -126,7 +120,7 @@
reconstruction = import_module("deep_image_matching.reconstruction")

# Define database path
#database = output_dir / "database_pycolmap.db"
# database = output_dir / "database_pycolmap.db"
database = output_dir / "database.db"

# Define how pycolmap create the cameras. Possible CameraMode are:
Expand Down
45 changes: 24 additions & 21 deletions src/deep_image_matching/io/h5_to_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import yaml
import argparse
import os
import warnings
from pathlib import Path
from typing import Union

import h5py
import numpy as np
Expand Down Expand Up @@ -63,7 +61,7 @@ def create_camera(db, image_path, camera_model):

focal = get_focal(image_path)

if camera_model == "simple-pinhole":
if camera_model == "simple-pinhole":
model = 0 # simple pinhole
param_arr = np.array([focal, width / 2, height / 2])
elif camera_model == "pinhole":
Expand All @@ -80,31 +78,32 @@ def create_camera(db, image_path, camera_model):

return db.add_camera(model, width, height, param_arr)


def parse_camera_options(camera_options: dict, db, image_path):
grouped_images = {}
n_cameras = len(camera_options.keys())-1
n_cameras = len(camera_options.keys()) - 1
for camera in range(n_cameras):
cam_opt = camera_options[f"cam{camera}"]
images = cam_opt['images'].split(',')
for i,img in enumerate(images):
grouped_images[img] = {
'camera_id' : camera + 1
}
images = cam_opt["images"].split(",")
for i, img in enumerate(images):
grouped_images[img] = {"camera_id": camera + 1}
if i == 0:
path = os.path.join(image_path, img)
try:
create_camera(db, path, cam_opt['camera_model'])
create_camera(db, path, cam_opt["camera_model"])
except:
logger.warning(f"Was not possible to load the first image to initialize cam{camera}")
logger.warning(
f"Was not possible to load the first image to initialize cam{camera}"
)
return grouped_images

def add_keypoints(db, h5_path, image_path, camera_options):

def add_keypoints(db, h5_path, image_path, camera_options):
grouped_images = parse_camera_options(camera_options, db, image_path)

keypoint_f = h5py.File(str(h5_path), "r")

#camera_id = None
# camera_id = None
fname_to_id = {}
k = 0
for filename in tqdm(list(keypoint_f.keys())):
Expand All @@ -115,17 +114,21 @@ def add_keypoints(db, h5_path, image_path, camera_options):
raise IOError(f"Invalid image path {path}")

if filename not in list(grouped_images.keys()):
if camera_options['general']['single_camera'] == False:
camera_id = create_camera(db, path, camera_options['general']['camera_model'])
elif camera_options['general']['single_camera'] == True:
if camera_options["general"]["single_camera"] is False:
camera_id = create_camera(
db, path, camera_options["general"]["camera_model"]
)
elif camera_options["general"]["single_camera"] is True:
if k == 0:
camera_id = create_camera(db, path, camera_options['general']['camera_model'])
camera_id = create_camera(
db, path, camera_options["general"]["camera_model"]
)
single_camera_id = camera_id
k += 1
elif k > 0:
camera_id = single_camera_id
else:
camera_id = grouped_images[filename]['camera_id']
camera_id = grouped_images[filename]["camera_id"]
image_id = db.add_image(filename, camera_id)
fname_to_id[filename] = image_id
# print('keypoints')
Expand Down Expand Up @@ -161,7 +164,7 @@ def add_raw_matches(db, h5_path, fname_to_id):

matches = group[key_2][()]
db.add_matches(id_1, id_2, matches)
#db.add_two_view_geometry(id_1, id_2, matches)
# db.add_two_view_geometry(id_1, id_2, matches)

added.add(pair_id)

Expand Down Expand Up @@ -189,7 +192,7 @@ def add_matches(db, h5_path, fname_to_id):
continue

matches = group[key_2][()]
#db.add_matches(id_1, id_2, matches)
# db.add_matches(id_1, id_2, matches)
db.add_two_view_geometry(id_1, id_2, matches)

added.add(pair_id)
Expand Down
Loading

0 comments on commit 8631727

Please sign in to comment.