3 """Go image recognition.
5 This is the main UI module of Imago.
14 from PIL import Image, ImageDraw
15 except ImportError, msg:
16 print >> sys.stderr, msg
22 import gridf3 as gridf
25 def argument_parser():
26 parser = argparse.ArgumentParser(description=__doc__)
27 parser.add_argument('files', metavar='file', nargs='+',
28 help="image to analyse")
29 parser.add_argument('-w', type=int, default=640,
30 help="scale image to the specified width before analysis")
31 parser.add_argument('-m', '--manual', dest='manual_mode',
33 help="manual grid selection")
34 parser.add_argument('-d', '--debug', dest='show_all',
36 help="show every step of the computation")
37 parser.add_argument('-s', '--save', dest='saving', action='store_true',
38 help="save images instead of displaying them")
39 parser.add_argument('-c', '--cache', dest='l_cache', action='store_true',
40 help="use cached lines")
41 parser.add_argument('-S', '--sgf', dest='sgf_output', action='store_true',
43 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
44 help="report progress")
48 # TODO factor this into smaller functions
50 """Main function of the program."""
52 parser = argument_parser()
53 args = parser.parse_args()
55 show_all = args.show_all
56 verbose = args.verbose
59 image = Image.open(args.files[0])
61 print >> sys.stderr, msg
64 image = image.convert('RGB')
66 if image.size[0] > args.w:
67 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
68 image.size[1])), Image.ANTIALIAS)
73 do_something = nothing
75 do_something = Imsave("saved/" + args.files[0][:-4] + "_" +
76 str(image.size[0]) + "/").save
79 do_something = im_debug.show
83 print >> sys.stderr, m
90 lines = manual.find_lines(image)
91 except manual.UserQuitError:
92 #TODO ask user to try again
96 filename = ("saved/cache/" + args.files[0][:-4] + "_" +
98 cache_dir = "/".join(filename.split('/')[:-1])
99 if os.path.exists(filename):
100 lines, l1, l2, bounds, hough = pickle.load(open(filename))
101 print >> sys.stderr, "using cached results"
103 lines, l1, l2, bounds, hough = linef.find_lines(image, do_something, logger)
104 if not os.path.isdir(cache_dir):
105 os.makedirs(cache_dir)
106 d_file = open(filename, 'wb')
107 pickle.dump((lines, l1, l2, bounds, hough), d_file)
110 lines, l1, l2, bounds, hough = linef.find_lines(image, do_something, logger)
111 #d_file = open('lines09.pickle', 'wb')
112 #pickle.dump(lines, d_file)
113 #d_file.close() #TODO delete this
116 grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
117 show_all, do_something, logger)
120 draw = ImageDraw.Draw(im_g)
121 for l in grid[0] + grid[1]:
122 draw.line(l, fill=(64, 255, 64), width=1)
123 do_something(im_g, "grid", name="grid")
125 board = intrsc.board(image, lines, show_all, do_something)
127 if len(args.files) == 1:
130 print board.asSGFsetPos()
135 game = output.Game(19, board) #TODO size parameter
136 for f in args.files[1:]:
138 image = Image.open(f)
140 print >> sys.stderr, msg
143 print >> sys.stderr, "Opening", f
144 if image.mode == 'P':
145 image = image.convert('RGB')
146 board = intrsc.board(image, lines, show_all, do_something)
158 def __init__(self, saving_dir):
159 self.saving_dir = saving_dir
162 def save(self, image, title='', name=None):
164 filename = self.saving_dir + name + '.jpg'
166 filename = self.saving_dir + "{0:0>2}".format(self.saving_num) + '.jpg'
168 if not os.path.isdir(self.saving_dir):
169 os.makedirs(self.saving_dir)
170 image.save(filename, 'JPEG')
172 if __name__ == '__main__':
175 except KeyboardInterrupt: #TODO does this work?
176 print >> sys.stderr, "Interrupted."