136 lines
6.8 KiB
Python
136 lines
6.8 KiB
Python
import argparse
|
|
import glob
|
|
import json
|
|
import os
|
|
from collections import OrderedDict
|
|
from os.path import basename
|
|
|
|
# Defaults for camera config.
|
|
from shutil import copyfile
|
|
|
|
DEFAULT_CAMERA_CALIBRATION = None
|
|
DEFAULT_REFERENCE_MARKER = [0, 0, 100, 100]
|
|
DEFAULT_REFERENCE_MARKER_WIDTH = 100.0
|
|
DEFAULT_REFERENCE_MARKER_HEIGHT = 100.0
|
|
DEFAULT_HORIZONTAL_CAMERA_ANGLE = 0.0
|
|
DEFAULT_VERTICAL_CAMERA_ANGLE = 0.0
|
|
|
|
# Defaults for pipeline config.
|
|
DEFAULT_IMAGE_BACKGROUND = None
|
|
DEFAULT_IMAGE_THRESHOLD = None
|
|
DEFAULT_REGION_OF_INTEREST = None
|
|
|
|
DEFAULT_IMAGE_GOOD_REGION = None
|
|
DEFAULT_IMAGE_GOOD_REGION_THRESHOLD = 0.3
|
|
DEFAULT_IMAGE_GOOD_REGION_GFSCALE = 1.8
|
|
|
|
DEFAULT_IMAGE_SLICES = None
|
|
DEFAULT_IMAGE_SLICES_NUMBER_OF_SLICES = 1
|
|
DEFAULT_IMAGE_SLICES_SCALE = 2
|
|
|
|
|
|
def convert_config(old_base_dir, new_cam_base_dir, new_pipeline_base_dir, new_background_base_dir):
|
|
for old_config_file in glob.glob(old_base_dir + '/*.json'):
|
|
# Skip the parameters (read as part of the config)
|
|
if old_config_file.endswith("_parameters.json"):
|
|
continue
|
|
|
|
with open(old_config_file) as data_file:
|
|
old_config = json.load(data_file)
|
|
|
|
prefix = old_config["camera"]["prefix"]
|
|
mirror_x = old_config["camera"].get("mirror_x", False)
|
|
mirror_y = old_config["camera"].get("mirror_y", False)
|
|
rotate = old_config["camera"].get("rotate", 0)
|
|
calibration = old_config["camera"].get("calibration")
|
|
if calibration is not None:
|
|
new_calibration = {"reference_marker": calibration.get("reference_marker", DEFAULT_REFERENCE_MARKER),
|
|
"reference_marker_width": calibration.get("reference_marker_width",
|
|
DEFAULT_REFERENCE_MARKER_WIDTH),
|
|
"reference_marker_height": calibration.get("reference_marker_height",
|
|
DEFAULT_REFERENCE_MARKER_HEIGHT),
|
|
"angle_horizontal": calibration.get("horizontal_camera_angle",
|
|
DEFAULT_HORIZONTAL_CAMERA_ANGLE),
|
|
"angle_vertical": calibration.get("vertical_camera_angle",
|
|
DEFAULT_VERTICAL_CAMERA_ANGLE)}
|
|
else:
|
|
new_calibration = DEFAULT_CAMERA_CALIBRATION
|
|
|
|
config_name = os.path.splitext(basename(old_config_file))[0]
|
|
|
|
new_cam_config = OrderedDict({"name": config_name,
|
|
"source": prefix,
|
|
"mirror_x": mirror_x,
|
|
"mirror_y": mirror_y,
|
|
"rotate": rotate,
|
|
"camera_calibration": new_calibration,
|
|
"source_type": "epics"})
|
|
|
|
cam_config_filename = os.path.join(new_cam_base_dir, config_name + ".json")
|
|
with open(cam_config_filename, 'w') as outfile:
|
|
json.dump(new_cam_config, outfile, indent=4)
|
|
|
|
new_pipeline_config = OrderedDict({"name": config_name,
|
|
"camera_name": config_name})
|
|
|
|
# We have pipeline parameters.
|
|
old_pipeline_config_file = os.path.join(old_base_dir, config_name + "_parameters.json")
|
|
if os.path.exists(old_pipeline_config_file):
|
|
with open(old_pipeline_config_file) as data_file:
|
|
old_pipeline_config = json.load(data_file)["parameter"]
|
|
|
|
image_background = config_name if old_pipeline_config.get("background_subtraction", False) else \
|
|
DEFAULT_IMAGE_BACKGROUND
|
|
image_threshold = old_pipeline_config.get("threshold", DEFAULT_IMAGE_THRESHOLD)
|
|
image_region_of_interest = old_pipeline_config.get("region_of_interest",
|
|
DEFAULT_REGION_OF_INTEREST)
|
|
|
|
# Expand with default parameters.
|
|
image_good_region = old_pipeline_config.get("good_region", DEFAULT_IMAGE_GOOD_REGION)
|
|
if image_good_region is not None:
|
|
image_good_region["threshold"] = image_good_region.get("threshold",
|
|
DEFAULT_IMAGE_GOOD_REGION_THRESHOLD)
|
|
image_good_region["gfscale"] = image_good_region.get("gfscale",
|
|
DEFAULT_IMAGE_GOOD_REGION_GFSCALE)
|
|
|
|
# Expand with default parameters.
|
|
image_slices = old_pipeline_config.get("slices", DEFAULT_IMAGE_SLICES)
|
|
if image_slices:
|
|
image_slices["number_of_slices"] = image_slices.get("number_of_slices",
|
|
DEFAULT_IMAGE_SLICES_NUMBER_OF_SLICES)
|
|
image_slices["scale"] = image_slices.get("scale",
|
|
DEFAULT_IMAGE_SLICES_SCALE)
|
|
|
|
new_pipeline_config["image_background"] = image_background
|
|
new_pipeline_config["image_threshold"] = image_threshold
|
|
new_pipeline_config["image_region_of_interest"] = image_region_of_interest
|
|
new_pipeline_config["image_good_region"] = image_good_region
|
|
new_pipeline_config["image_slices"] = image_slices
|
|
|
|
# We have a background image - need to copy this as well.
|
|
if image_background:
|
|
old_background_image_file = os.path.join(old_base_dir, config_name + "_background.npy")
|
|
new_background_image_file = os.path.join(new_background_base_dir, image_background + ".npy")
|
|
copyfile(old_background_image_file, new_background_image_file)
|
|
|
|
pipeline_config_filename = os.path.join(new_pipeline_base_dir, config_name + ".json")
|
|
with open(pipeline_config_filename, 'w') as outfile:
|
|
json.dump(new_pipeline_config, outfile, indent=4)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description='Convert old to new config format.')
|
|
parser.add_argument('--old_base_dir', default="to_convert", help="Old config base directory")
|
|
parser.add_argument('--new_cam_base_dir', default="configuration/camera_config", help="New camera base directory.")
|
|
parser.add_argument('--new_pipeline_base_dir', default="configuration/pipeline_config",
|
|
help="New pipeline base directory.")
|
|
parser.add_argument('--new_background_base_dir', default="configuration/background_config",
|
|
help="New background base directory.")
|
|
|
|
arguments = parser.parse_args()
|
|
|
|
convert_config(arguments.old_base_dir,
|
|
arguments.new_cam_base_dir,
|
|
arguments.new_pipeline_base_dir,
|
|
arguments.new_background_base_dir)
|