From f5528e6e11a3cc15b5785de97dabc1aa44d664b1 Mon Sep 17 00:00:00 2001 From: Derek Haynes Date: Mon, 3 Feb 2020 16:50:02 -0700 Subject: [PATCH 1/2] Pipeline Config options override argument --- python-api/calibrate.py | 16 ++++++++++++++++ python-api/calibration_utils.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/python-api/calibrate.py b/python-api/calibrate.py index 01402a485..0e849392d 100644 --- a/python-api/calibrate.py +++ b/python-api/calibrate.py @@ -8,6 +8,7 @@ from pathlib import Path import shutil import consts.resource_paths +import json use_cv = True try: @@ -45,6 +46,9 @@ def parse_args(): Capture 3 images per polygon: python calibrate.py -c 3 + + Pass thru pipeline config options: + python calibrate.py -co '{"board_config": {"swap_left_and_right_cameras": true, "left_to_right_distance_cm": 9.0}}' ''' parser = ArgumentParser(epilog=epilog_text,formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("-p", "--polygons", default=list(np.arange(len(setPolygonCoordinates(1000,600)))), nargs='*', @@ -62,11 +66,19 @@ def parse_args(): parser.add_argument("-m", "--mode", default=['capture','process'], nargs='*', type=str, required=False, help="Space-separated list of calibration options to run. By default, executes the full 'capture process' pipeline. To execute a single step, enter just that step (ex: 'process').") + parser.add_argument("-co", "--config_overwrite", default=None, + type=str, required=False, + help="JSON-formatted pipeline config object. This will be override defaults used in this script.") + options = parser.parse_args() return options args = vars(parse_args()) + +if args['config_overwrite']: + args['config_overwrite'] = json.loads(args['config_overwrite']) + print("Using Arguments=",args) if 'capture' in args['mode']: @@ -115,6 +127,10 @@ def parse_args(): } } + if args['config_overwrite'] is not None: + config = merge(args['config_overwrite'],config) + print("Merged Pipeline config with overwrite",config) + pipeline = depthai.create_pipeline(config) if pipeline is None: diff --git a/python-api/calibration_utils.py b/python-api/calibration_utils.py index 51991a962..df3219bcc 100644 --- a/python-api/calibration_utils.py +++ b/python-api/calibration_utils.py @@ -6,6 +6,25 @@ import re import time +# https://stackoverflow.com/questions/20656135/python-deep-merge-dictionary-data#20666342 +def merge(source, destination): + """ + run me with nosetests --with-doctest file.py + + >>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } } + >>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } } + >>> merge(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } } + True + """ + for key, value in source.items(): + if isinstance(value, dict): + # get node or create one + node = destination.setdefault(key, {}) + merge(value, node) + else: + destination[key] = value + + return destination def mkdir_overwrite(dir): if not os.path.exists(dir): os.makedirs(dir) From 8926e6c78de43699945e88951d3ec7607f7826be Mon Sep 17 00:00:00 2001 From: Derek Haynes Date: Mon, 3 Feb 2020 16:55:02 -0700 Subject: [PATCH 2/2] Removing unused variable --- python-api/calibrate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-api/calibrate.py b/python-api/calibrate.py index 0e849392d..2aed29809 100644 --- a/python-api/calibrate.py +++ b/python-api/calibrate.py @@ -106,7 +106,7 @@ def parse_args(): print("[ERROR] Unable to initialize device. Try to reset it. Exiting.") exit(1) - config = configs = { + config = { 'streams': ['left', 'right'], 'depth': {