3 """Go image recognition."""
11 import Image, ImageDraw
12 except ImportError, msg:
13 print >> sys.stderr, msg
23 def argument_parser():
24 parser = argparse.ArgumentParser(description=__doc__)
25 parser.add_argument('files', metavar='file', nargs='+',
26 help="image to analyse")
27 parser.add_argument('-w', type=int, default=640,
28 help="scale image to the specified width before analysis")
29 parser.add_argument('-m', '--manual', dest='manual_mode',
31 help="manual grid selection")
32 parser.add_argument('-d', '--debug', dest='show_all',
34 help="show every step of the computation")
35 parser.add_argument('-s', '--save', dest='saving', action='store_true',
36 help="save images instead of displaying them")
37 parser.add_argument('-c', '--cache', dest='l_cache', action='store_true',
38 help="use cached lines")
39 parser.add_argument('-S', '--sgf', dest='sgf_output', action='store_true',
41 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
42 help="report progress")
47 """Main function of the program."""
49 parser = argument_parser()
50 args = parser.parse_args()
52 show_all = args.show_all
53 verbose = args.verbose
56 image = Image.open(args.files[0])
58 print >> sys.stderr, msg
61 image = image.convert('RGB')
63 if image.size[0] > args.w:
64 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
65 image.size[1])), Image.ANTIALIAS)
66 do_something = im_debug.show
68 do_something = Imsave("saved/" + args.files[0][:-4] + "_" +
69 str(image.size[0]) + "/").save
73 lines = manual.find_lines(image)
74 except manual.UserQuitError:
75 #TODO ask user to try again
79 filename = ("saved/cache/" + args.files[0][:-4] + "_" +
81 cache_dir = "/".join(filename.split('/')[:-1])
82 if os.path.exists(filename):
83 lines, l1, l2, bounds, hough = pickle.load(open(filename))
84 print >> sys.stderr, "using cached results"
87 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
88 if not os.path.isdir(cache_dir):
89 os.makedirs(cache_dir)
90 d_file = open(filename, 'wb')
91 pickle.dump((lines, l1, l2, bounds, hough), d_file)
94 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
96 grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
97 show_all, do_something)
100 draw = ImageDraw.Draw(im_g)
101 for l in grid[0] + grid[1]:
102 draw.line(l, fill=(64, 255, 64), width=1)
103 do_something(im_g, "grid", name="grid")
105 board = intrsc.board(image, lines, show_all, do_something)
107 if len(args.files) == 1:
110 print board.asSGFsetPos()
115 game = output.Game(19, board) #TODO size parameter
116 for f in args.files[1:]:
118 image = Image.open(f)
120 print >> sys.stderr, msg
123 print >> sys.stderr, "Opening", f
124 if image.mode == 'P':
125 image = image.convert('RGB')
126 board = intrsc.board(image, lines, show_all, do_something)
138 def __init__(self, saving_dir):
139 self.saving_dir = saving_dir
142 def save(self, image, title='', name=None):
144 filename = self.saving_dir + name + '.jpg'
146 filename = self.saving_dir + "{0:0>2}".format(self.saving_num) + '.jpg'
148 if not os.path.isdir(self.saving_dir):
149 os.makedirs(self.saving_dir)
150 image.save(filename, 'JPEG')
152 if __name__ == '__main__':
155 except KeyboardInterrupt: #TODO does this work?
156 print >> sys.stderr, "Interrupted."