Browse Source

Json config file for parameters input

* add --json_args <path to json file> : loading parameters from the json file
master
PommeDroid 3 years ago
parent
commit
33b4138cc8
  1. 11
      json_args_sample.json
  2. 77
      main.py

11
json_args_sample.json

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
{
"input" : "input.png",
"output" : "output.png",
"cpu": true,
"bsize": 1,
"asize": 1,
"nsize": 1,
"vsize": 1,
"hsize": 0,
"auto-resize": true
}

77
main.py

@ -1,14 +1,18 @@ @@ -1,14 +1,18 @@
import json
import re
import shutil
import sys
import argparse
import tempfile
from json import JSONDecodeError
import cv2
import time
import os
import imageio
import sentry_sdk
import rook
import utils
import numpy as np
@ -41,7 +45,9 @@ processing_mod.add_argument( @@ -41,7 +45,9 @@ processing_mod.add_argument(
"--gpu",
action="append",
type=int,
help="ID of the GPU to use for processing. It can be used multiple times to specify multiple GPUs (Example: --gpu 0 --gpu 1 --gpu 2) This argument will be ignored if --cpu is active. (default: 0)",
help="ID of the GPU to use for processing. "
"It can be used multiple times to specify multiple GPUs (Example: --gpu 0 --gpu 1 --gpu 2)"
" This argument will be ignored if --cpu is active. (default: 0)",
)
parser.add_argument(
"--bsize",
@ -102,20 +108,67 @@ scale_mod.add_argument( @@ -102,20 +108,67 @@ scale_mod.add_argument(
help="Scale image to 512x512",
)
def check_crops_coord():
def type_func(a):
if not re.match(r"^\d+,\d+:\d+,\d+$", a):
raise argparse.ArgumentTypeError("Incorrect coordinates format. Valid format is <x_top_left>,<y_top_left>:<x_bot_right>,<x_bot_right>")
raise argparse.ArgumentTypeError("Incorrect coordinates format. "
"Valid format is <x_top_left>,<y_top_left>:<x_bot_right>,<x_bot_right>")
return tuple(int(x) for x in re.findall('\d+', a))
return type_func
scale_mod.add_argument(
"--overlay",
type=check_crops_coord(),
help="Processing the part of the image given by the coordinates (<x_top_left>,<y_top_left>:<x_bot_right>,<x_bot_right>) and overlay the result on the original image.",
help="Processing the part of the image given by the coordinates "
"(<x_top_left>,<y_top_left>:<x_bot_right>,<x_bot_right>) and overlay the result on the original image.",
)
def check_json_args_file():
def type_func(a):
if not os.path.isfile(a):
raise argparse.ArgumentTypeError("Arguments json file {} not found.".format(a))
with open(a) as f:
data = {}
try:
data = json.load(f)
except JSONDecodeError:
raise argparse.ArgumentTypeError("Arguments json file {} is not in valid JSON format.".format(a))
l = []
for k, v in data.items():
if not isinstance(v, bool):
l.extend(["--{}".format(k), str(v)])
elif v:
l.append("--{}".format(k))
return l
return type_func
parser.add_argument(
"-j",
"--json_args",
type=check_json_args_file(),
help="Load arguments from json files. "
"If a command line argument is also provide the json value will be ignore for this argument.",
)
args = parser.parse_args()
# Handle special cases for ignoring arguments in json file if provided in command line
l = args.json_args
if "--cpu" in sys.argv[1:] or "--gpu" in sys.argv[1:]:
l = list(filter(lambda a: a not in ("--cpu", "--gpu"), l))
if "--auto-resize" in sys.argv[1:] or "--auto-resize-crop" in sys.argv[1:] \
or "--auto-rescale" in sys.argv[1:] or "--overlay" in sys.argv[1:]:
l = list(filter(lambda a: a not in ("--auto-resize", "--auto-resize-crop", "--auto-rescale", "--overlay"), l))
args = parser.parse_args(l + sys.argv[1:])
"""
main.py
@ -124,6 +177,7 @@ main.py @@ -124,6 +177,7 @@ main.py
"""
# ------------------------------------------------- main()
def main():
if not os.path.isfile(args.input):
@ -159,9 +213,9 @@ def main(): @@ -159,9 +213,9 @@ def main():
exit(1)
# Preprocess
if args.overlay :
if args.overlay:
original_image = image.copy()
image = utils.crop_input(image,args.overlay[0],args.overlay[1],args.overlay[2],args.overlay[3])
image = utils.crop_input(image, args.overlay[0], args.overlay[1], args.overlay[2], args.overlay[3])
elif args.auto_resize:
image = utils.resize_input(image)
elif args.auto_resize_crop:
@ -179,7 +233,8 @@ def main(): @@ -179,7 +233,8 @@ def main():
result = process(image, gpu_ids, prefs)
if args.overlay:
result = utils.overlay_original_img(original_image, result, args.overlay[0], args.overlay[1], args.overlay[2], args.overlay[3])
result = utils.overlay_original_img(original_image, result, args.overlay[0], args.overlay[1],
args.overlay[2], args.overlay[3])
cv2.imwrite(args.output, result)
else:
@ -216,11 +271,14 @@ def main(): @@ -216,11 +271,14 @@ def main():
# Process
if args.n_runs is None or args.n_runs == 1:
process_gif_wrapper(gif_imgs, args.output if args.output != "output.png" else "output.gif", gpu_ids, args.enablepubes, args.n_cores)
process_gif_wrapper(gif_imgs, args.output if args.output != "output.png" else "output.gif", gpu_ids,
args.enablepubes, args.n_cores)
else:
base_output_filename = utils.strip_file_extension(args.output, ".gif") if args.output != "output.png" else "output"
base_output_filename = utils.strip_file_extension(args.output,
".gif") if args.output != "output.png" else "output"
for i in range(args.n_runs):
process_gif_wrapper(gif_imgs, base_output_filename + "%03d.gif" % i, gpu_ids, args.enablepubes, args.n_cores)
process_gif_wrapper(gif_imgs, base_output_filename + "%03d.gif" % i, gpu_ids, args.enablepubes,
args.n_cores)
end = time.time()
duration = end - start
@ -246,7 +304,6 @@ def process_gif_wrapper(gif_imgs, filename, gpu_ids, enablepubes, n_cores): @@ -246,7 +304,6 @@ def process_gif_wrapper(gif_imgs, filename, gpu_ids, enablepubes, n_cores):
shutil.rmtree(tmp_dir)
def start_sentry():
dsn = os.getenv("SENTRY_DSN")

Loading…
Cancel
Save