Esempio n. 1
0
def main():
    command_modes = ["gui", "benchmark", "headless", "graph"]

    parser = argparse.ArgumentParser()
    parser.add_argument("command", nargs="?", default="gui", choices=command_modes, help="Program mode")
    parser.add_argument("--debug", action="store_true", default=False, help="Turns on debug logging (extra verbose)")
    parser.add_argument(
        "--wait", action="store_true", default=False, help="Wait for keyboard input (useful for debugging)"
    )
    parser.add_argument("-a", "--algorithm", default="reference", help="Fusion algorithm to use")
    parser.add_argument("-X", "--extra", action="append", default=[], help="Extra options for algorithm")
    parser.add_argument("-C", "--chart-extra", action="append", default=[], help="Extra options for chart generation")
    parser.add_argument("-s", "--source", help="Source image path")
    parser.add_argument("-t", "--target", help="Target image path")
    parser.add_argument("-m", "--mask", help="Mask image path")
    parser.add_argument("-o", "--output", help="Output image path")
    parser.add_argument("--offset", help="Offset for source image (--offset y,x)")

    args = parser.parse_args()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    algorithm_kwargs = {}
    if args.extra:
        for extra_option in args.extra:
            algorithm_kwargs.update(parse_extra(extra_option))

    chart_kwargs = {}
    if args.chart_extra:
        for extra_option in args.chart_extra:
            chart_kwargs.update(parse_extra(extra_option))

    if args.wait:
        raw_input(
            (
                "Wait mode (--wait) enabled.\n"
                "This feature lets you easily attach a debugger or profiler before starting.\n"
                "PID: %d\n\n"
                "Press any key to continue..."
            )
            % os.getpid()
        )

    if args.command == "gui":
        editor_view = EditorView(args.algorithm, algorithm_kwargs=algorithm_kwargs)
        if args.source:
            editor_view.handle_loadsource(image_path=args.source)
        if args.target:
            editor_view.handle_loadtarget(image_path=args.target)
        editor_view.mainloop()
    elif args.command == "benchmark":
        benchmark_default(args.algorithm, algorithm_kwargs=algorithm_kwargs)
    elif args.command in ("headless", "graph"):
        assert args.algorithm
        assert args.source
        assert args.target
        assert args.mask
        if args.offset:
            offset_match = re.match(r"^(?P<y>-?\d+),(?P<x>-?\d+)$", args.offset)
            assert offset_match, 'Syntax for --offset should be "--offset 41,62"'
            offset = [int(offset_match.group("y")), int(offset_match.group("x"))]
        else:
            offset = None
        if args.command == "headless":
            fusion_from_file(
                args.algorithm,
                args.source,
                args.target,
                args.mask,
                offset=offset,
                output_path=args.output,
                algorithm_kwargs=algorithm_kwargs,
            )
        elif args.command == "graph":
            fusion_with_charts(
                args.algorithm,
                args.source,
                args.target,
                args.mask,
                offset=offset,
                output_path=args.output,
                algorithm_kwargs=algorithm_kwargs,
                chart_kwargs=chart_kwargs,
            )
Esempio n. 2
0
def benchmark_default(algorithm="reference", algorithm_kwargs=None):
    """Runs some basic benchmarking cases."""

    if algorithm_kwargs is None:
        algorithm_kwargs = {}

    with log_timer("benchmark: gradient", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "gradient-source.png"),
                         join(EXAMPLES_ROOT, "gradient-target.png"),
                         join(EXAMPLES_ROOT, "gradient-mask.png"),
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: easy", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "easy-source.jpg"),
                         join(EXAMPLES_ROOT, "easy-target.jpg"),
                         join(EXAMPLES_ROOT, "easy-mask.jpg"),
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: brick", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "brick-source.jpg"),
                         join(EXAMPLES_ROOT, "brick-target.jpg"),
                         join(EXAMPLES_ROOT, "brick-mask.jpg"),
                         offset=[81, 137],
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: window", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "window.jpg"),
                         join(EXAMPLES_ROOT, "window.jpg"),
                         join(EXAMPLES_ROOT, "window-mask.jpg"),
                         offset=[-1, -226],
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: penguin", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "penguin-source3.jpg"),
                         join(EXAMPLES_ROOT, "penguin-target.jpg"),
                         join(EXAMPLES_ROOT, "penguin-mask3.jpg"),
                         offset=[356, 287],
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: bear", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "bear-source3.jpg"),
                         join(EXAMPLES_ROOT, "bear-target.jpg"),
                         join(EXAMPLES_ROOT, "bear-mask3.jpg"),
                         offset=[0, 600],
                         algorithm_kwargs=algorithm_kwargs)
    with log_timer("benchmark: winterfell", level="info"):
        fusion_from_file(algorithm, join(EXAMPLES_ROOT, "winterfell-source.jpg"),
                         join(EXAMPLES_ROOT, "winterfell-target.jpg"),
                         join(EXAMPLES_ROOT, "winterfell-mask.jpg"),
                         offset=[287,421],
                         algorithm_kwargs=algorithm_kwargs)