Image size fallback fix, osfm extract camera

Former-commit-id: 7b8ca293fb
pull/1161/head
Piero Toffanin 2019-06-25 11:04:54 -04:00
rodzic 83a6d82091
commit e6e62aae36
3 zmienionych plików z 37 dodań i 5 usunięć

Wyświetl plik

@ -14,7 +14,7 @@ def get_image_size(file_path, fallback_on_error=True):
width, height = img.size
except Exception as e:
if fallback_on_error:
log.warning("Cannot read %s with PIL, fallback to cv2: %s" % (file_path, str(e)))
log.ODM_WARNING("Cannot read %s with PIL, fallback to cv2: %s" % (file_path, str(e)))
img = cv2.imread(file_path)
width = img.shape[1]
height = img.shape[0]

Wyświetl plik

@ -2,7 +2,7 @@
OpenSfM related utils
"""
import os, shutil, sys
import os, shutil, sys, json
import yaml
from opendm import io
from opendm import log
@ -85,11 +85,12 @@ class OSFMContext:
log.ODM_DEBUG("Copied image_groups.txt to OpenSfM directory")
io.copy(image_groups_file, os.path.join(self.opensfm_project_path, "image_groups.txt"))
# check for camera_models.json
camera_models_file = os.path.join(args.project_path, "camera_models.json")
# check for cameras.json
# TODO: use config.cameras
camera_models_file = os.path.join(args.project_path, "cameras.json")
has_camera_calibration = io.file_exists(camera_models_file)
if has_camera_calibration:
log.ODM_DEBUG("Copied camera_models.json to OpenSfM directory (camera_models_overrides.json)")
log.ODM_DEBUG("Copied cameras.json to OpenSfM directory (camera_models_overrides.json)")
io.copy(camera_models_file, os.path.join(self.opensfm_project_path, "camera_models_overrides.json"))
# create config file for OpenSfM
@ -192,6 +193,36 @@ class OSFMContext:
def path(self, *paths):
return os.path.join(self.opensfm_project_path, *paths)
def extract_cameras(self, output, rerun=False):
reconstruction_file = self.path("reconstruction.json")
if not os.path.exists(output) or rerun:
if os.path.exists(reconstruction_file):
result = {}
with open(reconstruction_file, 'r') as fin:
json_f = json.loads(fin.read())
for recon in json_f:
if 'cameras' in recon:
for camera_id in recon['cameras']:
# Strip "v2" from OpenSfM camera IDs
new_camera_id = camera_id
if new_camera_id.startswith("v2 "):
new_camera_id = new_camera_id[3:]
result[new_camera_id] = recon['cameras'][camera_id]
# Remove "_prior" keys
keys = list(result[new_camera_id].keys())
for k in keys:
if k.endswith('_prior'):
result[new_camera_id].pop(k)
with open(output, 'w') as fout:
fout.write(json.dumps(result))
else:
log.ODM_WARNING("Cannot export cameras to %s. reconstruction.json does not exist." % output)
else:
log.ODM_INFO("Already extracted cameras")
def update_config(self, cfg_dict):
cfg_file = self.get_config_file_path()
log.ODM_DEBUG("Updating %s" % cfg_file)

Wyświetl plik

@ -27,6 +27,7 @@ class ODMOpenSfMStage(types.ODM_Stage):
octx.feature_matching(self.rerun())
self.update_progress(30)
octx.reconstruct(self.rerun())
octx.extract_cameras(tree.path("cameras.json"), self.rerun())
self.update_progress(70)
# If we find a special flag file for split/merge we stop right here