Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/folder structure #55

Merged
merged 9 commits into from
Jan 20, 2022
203 changes: 119 additions & 84 deletions src/correct_images/correct_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import argparse
import os
import string
import sys
import time
from pathlib import Path
Expand Down Expand Up @@ -52,6 +53,12 @@ def main(args=None):
action="store_true",
help="Force overwrite if correction parameters already exist.",
)
subparser_correct.add_argument(
"--suffix",
dest="suffix",
default="",
help="Expected suffix for correct_images configuration and output folders.",
)
subparser_correct.set_defaults(func=call_correct)

# subparser parse
Expand All @@ -66,6 +73,12 @@ def main(args=None):
action="store_true",
help="Force overwrite if correction parameters already exist.",
)
subparser_parse.add_argument(
"--suffix",
dest="suffix",
default="",
help="Expected suffix for correct_images configuration and output folders.",
)
subparser_parse.set_defaults(func=call_parse)

# subparser process
Expand All @@ -80,13 +93,25 @@ def main(args=None):
action="store_true",
help="Force overwrite if correction parameters already exist.",
)
subparser_process.add_argument(
"--suffix",
dest="suffix",
default="",
help="Expected suffix for correct_images configuration and output folders.",
)
subparser_process.set_defaults(func=call_process)

# subparser rescale image
subparser_rescale = subparsers.add_parser(
"rescale", help="Rescale processed images"
)
subparser_rescale.add_argument("path", help="Path to raw folder")
subparser_rescale.add_argument(
"--suffix",
dest="suffix",
default="",
help="Expected suffix for correct_images configuration and output folders.",
)
subparser_rescale.set_defaults(func=call_rescale)

if len(sys.argv) == 1 and args is None:
Expand All @@ -99,7 +124,13 @@ def main(args=None):
args.func(args)
else:
args = parser.parse_args()
args.func(args)

# Check suffix is only text, digits, dash and underscores
allowed_chars = string.ascii_letters+ "-" + "_" + string.digits
if all([c in allowed_chars for c in args.suffix]):
args.func(args)
else:
Console.error("Suffix must only contain letters, digits, dash and underscores")


def call_parse(args):
Expand All @@ -120,7 +151,7 @@ def call_parse(args):
/ ("log/" + time_string + "_correct_images_parse.log")
)

correct_config, camerasystem = load_configuration_and_camera_system(path)
correct_config, camerasystem = load_configuration_and_camera_system(path, args.suffix)

for camera in camerasystem.cameras:
Console.info("Parsing for camera", camera.name)
Expand All @@ -129,7 +160,7 @@ def call_parse(args):
Console.info("No images found for the camera at the path provided...")
continue
else:
corrector = Corrector(args.force, camera, correct_config, path)
corrector = Corrector(args.force, args.suffix, camera, correct_config, path)
if corrector.camera_found:
corrector.parse()

Expand Down Expand Up @@ -157,7 +188,7 @@ def call_process(args):
/ ("log/" + time_string + "_correct_images_process.log")
)

correct_config, camerasystem = load_configuration_and_camera_system(path)
correct_config, camerasystem = load_configuration_and_camera_system(path, args.suffix)

for camera in camerasystem.cameras:
Console.info("Processing for camera", camera.name)
Expand All @@ -166,7 +197,7 @@ def call_process(args):
Console.info("No images found for the camera at the path provided...")
continue
else:
corrector = Corrector(args.force, camera, correct_config, path)
corrector = Corrector(args.force, args.suffix, camera, correct_config, path)
if corrector.camera_found:
corrector.process()
Console.info("Process completed for all cameras...")
Expand All @@ -193,11 +224,16 @@ def call_rescale(args):
/ ("log/" + time_string + "_correct_images_rescale.log")
)

correct_config, camerasystem = load_configuration_and_camera_system(path)
correct_config, camerasystem = load_configuration_and_camera_system(path, args.suffix)

# install freeimage plugins if not installed
imageio.plugins.freeimage.download()

if correct_config.camerarescale is None:
Console.error("Camera rescale configuration not found")
Console.error("Please populate the correct_images.yaml file with a rescale configuration")
Console.quit("Malformed correct_images.yaml file")

# obtain parameters for rescale from correct_config
rescale_cameras = correct_config.camerarescale.rescale_cameras

Expand All @@ -206,7 +242,7 @@ def call_rescale(args):
Console.info("Rescaling completed for all cameras ...")


def load_configuration_and_camera_system(path):
def load_configuration_and_camera_system(path, suffix=None):
"""Generate correct_config and camera system objects from input config
yaml files

Expand All @@ -229,93 +265,88 @@ def load_configuration_and_camera_system(path):
else:
Console.quit("File mission.yaml file not found at", path_raw_folder)

# load mission parameters
mission = Mission(path_mission)
acfr_std_camera_file = "auv_nav/default_yaml/ts1/SSK17-01/camera.yaml"
sx3_camera_file = "auv_nav/default_yaml/ae2000/YK17-23C/camera.yaml"
biocam_camera_file = "auv_nav/default_yaml/as6/DY109/camera.yaml"
biocam4000_15c_camera_file = "auv_nav/default_yaml/alr/jc220/camera.yaml"
hybis_camera_file = "auv_nav/default_yaml/hybis/camera.yaml"
ntnu_camera_file = "auv_nav/default_yaml/ntnu_stereo/tautra21/camera.yaml"
rosbag_extracted_camera_file = (
"auv_nav/default_yaml/rosbag/grassmap/camera.yaml"
)

# resolve path to camera.yaml file
temp_path = path_raw_folder / "camera.yaml"
acfr_std_correct_config_file = (
"correct_images/default_yaml/acfr/correct_images.yaml"
)
sx3_std_correct_config_file = (
"correct_images/default_yaml/sx3/correct_images.yaml"
)
biocam_std_correct_config_file = (
"correct_images/default_yaml/biocam/correct_images.yaml"
)
biocam4000_15c_std_correct_config_file = (
"correct_images/default_yaml/biocam4000_15c/correct_images.yaml"
)
hybis_std_correct_config_file = (
"correct_images/default_yaml/hybis/correct_images.yaml"
)
ntnu_std_correct_config_file = (
"correct_images/default_yaml/ntnu_stereo/correct_images.yaml"
)
rosbag_extracted_images_std_correct_config_file = (
"correct_images/default_yaml/rosbag_extracted_images/correct_images.yaml"
)

default_file_path_correct_config = None
camera_yaml_path = None
default_file_path_correct_config = None

if not temp_path.exists():
Console.info(
"Not found camera.yaml file in /raw folder...Using default ",
"camera.yaml file...",
)
# find out default yaml paths
root = Path(__file__).resolve().parents[1]

acfr_std_camera_file = "auv_nav/default_yaml/ts1/SSK17-01/camera.yaml"
sx3_camera_file = "auv_nav/default_yaml/ae2000/YK17-23C/camera.yaml"
biocam_camera_file = "auv_nav/default_yaml/as6/DY109/camera.yaml"
biocam4000_15c_camera_file = "auv_nav/default_yaml/alr/jc220/camera.yaml"
hybis_camera_file = "auv_nav/default_yaml/hybis/camera.yaml"
ntnu_camera_file = "auv_nav/default_yaml/ntnu_stereo/tautra21/camera.yaml"
rosbag_extracted_camera_file = (
"auv_nav/default_yaml/rosbag/grassmap/camera.yaml"
)
# load mission parameters
mission = Mission(path_mission)

acfr_std_correct_config_file = (
"correct_images/default_yaml/acfr/correct_images.yaml"
)
sx3_std_correct_config_file = (
"correct_images/default_yaml/sx3/correct_images.yaml"
)
biocam_std_correct_config_file = (
"correct_images/default_yaml/biocam/correct_images.yaml"
)
biocam4000_15c_std_correct_config_file = (
"correct_images/default_yaml/biocam4000_15c/correct_images.yaml"
# find out default yaml paths
root = Path(__file__).resolve().parents[1]

if mission.image.format == "acfr_standard":
camera_yaml_path = root / acfr_std_camera_file
default_file_path_correct_config = root / acfr_std_correct_config_file
elif mission.image.format == "seaxerocks_3":
camera_yaml_path = root / sx3_camera_file
default_file_path_correct_config = root / sx3_std_correct_config_file
elif mission.image.format == "biocam":
camera_yaml_path = root / biocam_camera_file
default_file_path_correct_config = root / biocam_std_correct_config_file
elif mission.image.format == "biocam4000_15c":
camera_yaml_path = root / biocam4000_15c_camera_file
default_file_path_correct_config = (
root / biocam4000_15c_std_correct_config_file
)
hybis_std_correct_config_file = (
"correct_images/default_yaml/hybis/correct_images.yaml"
elif mission.image.format == "hybis":
camera_yaml_path = root / hybis_camera_file
default_file_path_correct_config = root / hybis_std_correct_config_file
elif mission.image.format == "ntnu_stereo":
camera_yaml_path = root / ntnu_camera_file
default_file_path_correct_config = root / ntnu_std_correct_config_file
elif mission.image.format == "rosbag_extracted_images":
camera_yaml_path = root / rosbag_extracted_camera_file
default_file_path_correct_config = (
root / rosbag_extracted_images_std_correct_config_file
)
ntnu_std_correct_config_file = (
"correct_images/default_yaml/ntnu_stereo/correct_images.yaml"
)
rosbag_extracted_images_std_correct_config_file = (
"correct_images/default_yaml/rosbag_extracted_images/correct_images.yaml"
else:
Console.quit(
"Image system in camera.yaml does not match with mission.yaml",
"Provide correct camera.yaml in /raw folder... ",
)

Console.info("Image format:", mission.image.format)

if mission.image.format == "acfr_standard":
camera_yaml_path = root / acfr_std_camera_file
default_file_path_correct_config = root / acfr_std_correct_config_file
elif mission.image.format == "seaxerocks_3":
camera_yaml_path = root / sx3_camera_file
default_file_path_correct_config = root / sx3_std_correct_config_file
elif mission.image.format == "biocam":
camera_yaml_path = root / biocam_camera_file
default_file_path_correct_config = root / biocam_std_correct_config_file
elif mission.image.format == "biocam4000_15c":
camera_yaml_path = root / biocam4000_15c_camera_file
default_file_path_correct_config = (
root / biocam4000_15c_std_correct_config_file
)
elif mission.image.format == "hybis":
camera_yaml_path = root / hybis_camera_file
default_file_path_correct_config = root / hybis_std_correct_config_file
elif mission.image.format == "ntnu_stereo":
camera_yaml_path = root / ntnu_camera_file
default_file_path_correct_config = root / ntnu_std_correct_config_file
elif mission.image.format == "rosbag_extracted_images":
camera_yaml_path = root / rosbag_extracted_camera_file
default_file_path_correct_config = (
root / rosbag_extracted_images_std_correct_config_file
)
else:
Console.quit(
"Image system in camera.yaml does not match with mission.yaml",
"Provide correct camera.yaml in /raw folder... ",
)
# resolve path to camera.yaml file
expected_camera_yaml_path = path_raw_folder / "camera.yaml"
if not expected_camera_yaml_path.exists():
Console.info(
"Not found camera.yaml file in /raw folder...Using default ",
"camera.yaml file...",
)
else:
Console.info("Found camera.yaml file in /raw folder...")
camera_yaml_path = temp_path

Console.info("camera.yaml:", camera_yaml_path)
Console.info("raw folder:", path_raw_folder)
camera_yaml_path = expected_camera_yaml_path

# instantiate the camera system and setup cameras from mission and
# config files / auv_nav
Expand All @@ -327,7 +358,11 @@ def load_configuration_and_camera_system(path):
)

# check for correct_config yaml path
path_correct_images = path_config_folder / "correct_images.yaml"
path_correct_images = None
if suffix == "" or suffix is None:
path_correct_images = path_config_folder / "correct_images.yaml"
else:
path_correct_images = path_config_folder / ("correct_images_" + suffix + ".yaml")
if path_correct_images.exists():
Console.info(
"Configuration file correct_images.yaml file found at", path_correct_images,
Expand Down
Loading