3 """Go image recognition"""
11 import Image, ImageDraw
12 except ImportError, msg:
13 print >> sys.stderr, msg
23 """Main function of the program."""
25 parser = argparse.ArgumentParser(description=__doc__)
26 parser.add_argument('files', metavar='file', nargs='+',
27 help="image to analyse")
28 parser.add_argument('-w', type=int, default=640,
29 help="scale image to the specified width before analysis")
30 parser.add_argument('-m', '--manual', dest='manual_mode',
32 help="manual grid selection")
33 parser.add_argument('-d', '--debug', dest='show_all',
35 help="show every step of the computation")
36 parser.add_argument('-s', '--save', dest='saving', action='store_true',
37 help="save images instead of displaying them")
38 parser.add_argument('-c', '--cache', dest='l_cache', action='store_true',
39 help="use cached lines")
40 parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
41 help="report progress")
42 args = parser.parse_args()
44 show_all = args.show_all
45 verbose = args.verbose
48 image = Image.open(args.files[0])
50 print >> sys.stderr, msg
53 image = image.convert('RGB')
55 if image.size[0] > args.w:
56 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
57 image.size[1])), Image.ANTIALIAS)
58 do_something = im_debug.show
60 do_something = Imsave("saved/" + args.files[0][:-4] + "_" +
61 str(image.size[0]) + "/").save
65 lines = manual.find_lines(image)
66 except manual.UserQuitError:
67 #TODO ask user to try again
71 filename = ("saved/cache/" + args.files[0][:-4] + "_" +
73 if os.path.exists(filename):
74 lines, l1, l2, bounds, hough = pickle.load(open(filename))
75 print >> sys.stderr, "using cached results"
78 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
79 if not os.path.isdir("saved/cache"):
80 os.makedirs("saved/cache")
81 d_file = open(filename, 'wb')
82 pickle.dump((lines, l1, l2, bounds, hough), d_file)
85 lines, l1, l2, bounds, hough, im_h = linef.find_lines(image, show_all, do_something, verbose)
87 grid, lines = gridf.find(lines, image.size, l1, l2, bounds, hough,
91 draw = ImageDraw.Draw(im_g)
92 for l in grid[0] + grid[1]:
93 draw.line(l, fill=(64, 255, 64), width=1)
96 board = intrsc.board(image, lines, show_all, do_something)
102 if len(args.files) > 1:
103 for f in args.files[1:]:
105 image = Image.open(f)
107 print >> sys.stderr, msg
109 if image.mode == 'P':
110 image = image.convert('RGB')
111 board = intrsc.board(image, lines, show_all, do_something)
118 def __init__(self, saving_dir):
119 self.saving_dir = saving_dir
122 def save(self, image, title=''):
123 filename = self.saving_dir + "{0:0>2}".format(self.saving_num) + '.jpg'
124 if not os.path.isdir(self.saving_dir):
125 os.makedirs(self.saving_dir)
126 image.save(filename, 'JPEG')
129 if __name__ == '__main__':
132 except KeyboardInterrupt: