3 """Go image recognition."""
11 import Image, ImageDraw
12 except ImportError, msg:
13 print >> sys.stderr, msg
22 def argument_parser():
23 parser = argparse.ArgumentParser(description=__doc__)
24 parser.add_argument('files', metavar='file', nargs='+',
25 help="image to analyse")
26 parser.add_argument('-w', type=int, default=640,
27 help="scale image to the specified width before analysis")
28 parser.add_argument('-m', '--manual', dest='manual_mode',
30 help="manual grid selection")
31 parser.add_argument('-d', '--debug', dest='show_all',
33 help="show every step of the computation")
34 parser.add_argument('-s', '--save', dest='saving', action='store_true',
35 help="save images instead of displaying them")
36 parser.add_argument('-c', '--cache', dest='l_cache', action='store_true',
37 help="use cached lines")
38 parser.add_argument('-S', '--sgf', dest='sgf_output', action='store_true',
40 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
41 help="report progress")
46 """Main function of the program."""
48 parser = argument_parser()
49 args = parser.parse_args()
51 show_all = args.show_all
52 verbose = args.verbose
55 image = Image.open(args.files[0])
57 print >> sys.stderr, msg
60 image = image.convert('RGB')
62 if image.size[0] > args.w:
63 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
64 image.size[1])), Image.ANTIALIAS)
65 do_something = im_debug.show
67 do_something = Imsave("saved/" + args.files[0][:-4] + "_" +
68 str(image.size[0]) + "/").save
72 lines = manual.find_lines(image)
73 except manual.UserQuitError:
74 #TODO ask user to try again
78 filename = ("saved/cache/" + args.files[0][:-4] + "_" +
80 cache_dir = "/".join(filename.split('/')[:-1])
81 if os.path.exists(filename):
82 lines, l1, l2, bounds, hough = pickle.load(open(filename))
83 print >> sys.stderr, "using cached results"
86 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
87 if not os.path.isdir(cache_dir):
88 os.makedirs(cache_dir)
89 d_file = open(filename, 'wb')
90 pickle.dump((lines, l1, l2, bounds, hough), d_file)
93 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
95 grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
96 show_all, do_something)
99 draw = ImageDraw.Draw(im_g)
100 for l in grid[0] + grid[1]:
101 draw.line(l, fill=(64, 255, 64), width=1)
102 do_something(im_g, "grid", name="grid")
104 board = intrsc.board(image, lines, show_all, do_something)
106 if len(args.files) == 1:
109 print board.asSGFsetPos()
114 for f in args.files[1:]:
116 image = Image.open(f)
118 print >> sys.stderr, msg
120 if image.mode == 'P':
121 image = image.convert('RGB')
122 board = intrsc.board(image, lines, show_all, do_something)
129 def __init__(self, saving_dir):
130 self.saving_dir = saving_dir
133 def save(self, image, title='', name=None):
135 filename = self.saving_dir + name + '.jpg'
137 filename = self.saving_dir + "{0:0>2}".format(self.saving_num) + '.jpg'
139 if not os.path.isdir(self.saving_dir):
140 os.makedirs(self.saving_dir)
141 image.save(filename, 'JPEG')
143 if __name__ == '__main__':
146 except KeyboardInterrupt: #TODO does this work?