X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/ecc0929cb0e011db2825e79d7495d7ef7b113e1e..45b6aba3bbf3aea1bacad077387ae77a1deb1a3c:/imago_pack/imago.py?ds=sidebyside diff --git a/imago_pack/imago.py b/imago_pack/imago.py index 33e7c69..6238902 100755 --- a/imago_pack/imago.py +++ b/imago_pack/imago.py @@ -1,6 +1,9 @@ #!/usr/bin/env python -"""Go image recognition""" +"""Go image recognition. + +This is the main UI module of Imago. +""" import sys import os @@ -18,10 +21,9 @@ import linef import manual import intrsc import gridf +import output -def main(): - """Main function of the program.""" - +def argument_parser(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('files', metavar='file', nargs='+', help="image to analyse") @@ -37,8 +39,18 @@ def main(): help="save images instead of displaying them") parser.add_argument('-c', '--cache', dest='l_cache', action='store_true', help="use cached lines") + parser.add_argument('-S', '--sgf', dest='sgf_output', action='store_true', + help="output in SGF") parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="report progress") + return parser + + +# TODO factor this into smaller functions +def main(): + """Main function of the program.""" + + parser = argument_parser() args = parser.parse_args() show_all = args.show_all @@ -55,11 +67,24 @@ def main(): if image.size[0] > args.w: image = image.resize((args.w, int((float(args.w)/image.size[0]) * image.size[1])), Image.ANTIALIAS) - do_something = im_debug.show - if args.saving: + + if not show_all: + def nothing(a, b): + pass + do_something = nothing + elif args.saving: do_something = Imsave("saved/" + args.files[0][:-4] + "_" + str(image.size[0]) + "/").save + else: + do_something = im_debug.show + if verbose: + def logger(m): + print >> sys.stderr, m + else: + def logger(m): + pass + if args.manual_mode: try: lines = manual.find_lines(image) @@ -74,19 +99,18 @@ def main(): if os.path.exists(filename): lines, l1, l2, bounds, hough = pickle.load(open(filename)) print >> sys.stderr, "using cached results" - im_h = None else: - lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose) + lines, l1, l2, bounds, hough = linef.find_lines(image, do_something, logger) if not os.path.isdir(cache_dir): os.makedirs(cache_dir) d_file = open(filename, 'wb') pickle.dump((lines, l1, l2, bounds, hough), d_file) d_file.close() else: - lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose) + lines, l1, l2, bounds, hough = linef.find_lines(image, do_something, logger) grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough, - show_all, do_something) + show_all, do_something, logger) if show_all: im_g = image.copy() draw = ImageDraw.Draw(im_g) @@ -96,22 +120,33 @@ def main(): board = intrsc.board(image, lines, show_all, do_something) - #simple ASCII output: - for line in board: - print ' '.join(line) + if len(args.files) == 1: + + if args.sgf_output: + print board.asSGFsetPos() + else: + print board - if len(args.files) > 1: + else: + game = output.Game(19, board) #TODO size parameter for f in args.files[1:]: try: image = Image.open(f) except IOError, msg: print >> sys.stderr, msg continue + if verbose: + print >> sys.stderr, "Opening", f if image.mode == 'P': image = image.convert('RGB') board = intrsc.board(image, lines, show_all, do_something) - for line in board: - print ' '.join(line) + if args.sgf_output: + game.addMove(board) + else: + print board + + if args.sgf_output: + print game.asSGF() return 0 @@ -133,6 +168,6 @@ class Imsave(): if __name__ == '__main__': try: sys.exit(main()) - except KeyboardInterrupt: - print "Interrupted." - sys.exit() + except KeyboardInterrupt: #TODO does this work? + print >> sys.stderr, "Interrupted." + sys.exit(1)