def main(which, debug=True): with(open('settings.yml')) as f: settings = yaml.load(f) clf = pickle.load(open('classifier.pickle')) img = capture_image() prediction, binarized_img, chars = predict(clf, img=img, boundaries=settings['crop'][which]) if debug: pp.gray() pp.subplot(2,1,1) pp.title('Prediction: %s' % prediction, fontsize='xx-large') pp.imshow(binarized_img) for i in range(6): pp.subplot(2,6,7+i) pp.imshow(chars[i]) pp.show() print prediction
settings = yaml.load(f) class ValidationError(Exception): pass def validate_user(username, password): if settings["users"][username] != password: raise ValidationError() @app.route("/<int:token_id>", methods=["POST"]) def index(token_id): try: username = request.values["username"] password = request.values["password"] validate_user(username, password) boundaries = settings["crop"][token_id] except KeyError, ValidationError: abort(401) img = capture_image() prediction, binarized_img, chars = predict(clf, img=img, boundaries=boundaries) return jsonify({"prediction": prediction}) if __name__ == "__main__": app.run(debug=True)
def interactive_benchmark(clf, raw_images_glob, save_to): with(open('settings.yml')) as f: settings = yaml.load(f) # interactive plotting pp.ion() pp.figure() pp.gray() if not os.path.exists(save_to): print "CREATING DIRECTORY", save_to os.makedirs(save_to) instructions = '[Enter] to accept, [q] to quit, or key the correct answer' print instructions lines = [] raw_images_fn = glob.glob(raw_images_glob) training_images = set(yaml.load(open('training/training_set.yml')).keys()) for fn in raw_images_fn: if os.path.basename(fn) in training_images: print 'Already trained on %s. Skipping' % fn continue prediction, binarized_img, chars = predict(clf, img=fn, boundaries=settings['crop'][0]) # plot the binarize_img across the top panel pp.clf() pp.subplot(2,1,1) pp.imshow(binarized_img) # plot the 6 chars (separated) across the bottom panel for i in range(6): pp.subplot(2,6,7+i) pp.imshow(chars[i]) title = ' '.join(prediction[:3]) + ' ' + ' '.join(prediction[3:]) pp.suptitle(title, fontsize='xx-large') pp.draw() ask_again = True while ask_again: response = raw_input('[Enter/q/key]: ') ask_again = False # default if response == '': line = '%s: "%s"' % (os.path.basename(fn), prediction) print line lines.append(line) io.imsave(os.path.join(save_to, os.path.basename(fn)), 1.0*binarized_img) elif re.match('\d{6}', response): line = '%s: "%s"' % (os.path.basename(fn), response) print 'CORRECTED', line lines.append(line) io.imsave(os.path.join(save_to, os.path.basename(fn)), 1.0*binarized_img) elif response == 'q': print 'Quitting...\n' print ('Instructions: Move the files from %s into the training ' 'directory, and add the lines below to the file' 'training_set.yml:\n' % save_to) print os.linesep.join(lines) return else: print instructions ask_again = True