Improved error handling at convert.py ()

Print error and exists if colmap or imagemagic fails. This avoid continuing the execution with errors and failing weirldly afterwards.
This commit is contained in:
Javi Carnero 2023-07-13 18:44:58 +02:00 committed by GitHub
parent 9f45a4f4e7
commit 493535a576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,7 @@
# #
import os import os
import logging
from argparse import ArgumentParser from argparse import ArgumentParser
import shutil import shutil
@ -31,34 +32,50 @@ if not args.skip_matching:
os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True) os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
## Feature extraction ## Feature extraction
os.system(colmap_command + " feature_extractor "\ feat_extracton_cmd = colmap_command + " feature_extractor "\
"--database_path " + args.source_path + "/distorted/database.db \ "--database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \ --image_path " + args.source_path + "/input \
--ImageReader.single_camera 1 \ --ImageReader.single_camera 1 \
--ImageReader.camera_model " + args.camera + " \ --ImageReader.camera_model " + args.camera + " \
--SiftExtraction.use_gpu " + str(use_gpu)) --SiftExtraction.use_gpu " + str(use_gpu)
exit_code = os.system(feat_extracton_cmd)
if exit_code != 0:
logging.error(f"Feature extraction failed with code {exit_code}. Exiting.")
exit(exit_code)
## Feature matching ## Feature matching
os.system(colmap_command + " exhaustive_matcher \ feat_matching_cmd = colmap_command + " exhaustive_matcher \
--database_path " + args.source_path + "/distorted/database.db \ --database_path " + args.source_path + "/distorted/database.db \
--SiftMatching.use_gpu " + str(use_gpu)) --SiftMatching.use_gpu " + str(use_gpu)
exit_code = os.system(feat_matching_cmd)
if exit_code != 0:
logging.error(f"Feature matching failed with code {exit_code}. Exiting.")
exit(exit_code)
### Bundle adjustment ### Bundle adjustment
# The default Mapper tolerance is unnecessarily large, # The default Mapper tolerance is unnecessarily large,
# decreasing it speeds up bundle adjustment steps. # decreasing it speeds up bundle adjustment steps.
os.system(colmap_command + " mapper \ mapper_cmd = (colmap_command + " mapper \
--database_path " + args.source_path + "/distorted/database.db \ --database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \ --image_path " + args.source_path + "/input \
--output_path " + args.source_path + "/distorted/sparse \ --output_path " + args.source_path + "/distorted/sparse \
--Mapper.ba_global_function_tolerance=0.000001") --Mapper.ba_global_function_tolerance=0.000001")
exit_code = os.system(mapper_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
### Image undistortion ### Image undistortion
## We need to undistort our images into ideal pinhole intrinsics. ## We need to undistort our images into ideal pinhole intrinsics.
os.system(colmap_command + " image_undistorter \ img_undist_cmd = (colmap_command + " image_undistorter \
--image_path " + args.source_path + "/input \ --image_path " + args.source_path + "/input \
--input_path " + args.source_path + "/distorted/sparse/0 \ --input_path " + args.source_path + "/distorted/sparse/0 \
--output_path " + args.source_path + "\ --output_path " + args.source_path + "\
--output_type COLMAP") --output_type COLMAP")
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
files = os.listdir(args.source_path + "/sparse") files = os.listdir(args.source_path + "/sparse")
os.makedirs(args.source_path + "/sparse/0", exist_ok=True) os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
@ -85,14 +102,23 @@ if(args.resize):
destination_file = os.path.join(args.source_path, "images_2", file) destination_file = os.path.join(args.source_path, "images_2", file)
shutil.copy2(source_file, destination_file) shutil.copy2(source_file, destination_file)
os.system(magick_command + " mogrify -resize 50% " + destination_file) exit_code = os.system(magick_command + " mogrify -resize 50% " + destination_file)
if exit_code != 0:
logging.error(f"50% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
destination_file = os.path.join(args.source_path, "images_4", file) destination_file = os.path.join(args.source_path, "images_4", file)
shutil.copy2(source_file, destination_file) shutil.copy2(source_file, destination_file)
os.system(magick_command + " mogrify -resize 25% " + destination_file) exit_code = os.system(magick_command + " mogrify -resize 25% " + destination_file)
if exit_code != 0:
logging.error(f"25% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
destination_file = os.path.join(args.source_path, "images_8", file) destination_file = os.path.join(args.source_path, "images_8", file)
shutil.copy2(source_file, destination_file) shutil.copy2(source_file, destination_file)
os.system(magick_command + " mogrify -resize 12.5% " + destination_file) exit_code = os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
if exit_code != 0:
logging.error(f"12.5% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
print("Done.") print("Done.")