def main(): import sys classifier = cv2.SVM() classifier.load(sys.argv[1]) im = cv2.imread(sys.argv[2]) im = cv2.GaussianBlur(im, (3, 3), 0) imshow_large(__file__, im) cv2.waitKey() unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() binarized = binarize(unrotated) rois = get_rois(binarized) results = {} grayscale = cv2.cvtColor(unrotated, cv.CV_BGR2GRAY) for (x, y, width, height) in rois: roi = grayscale[y:y + height, x:x + width] vec = extract_features(roi) label = classifier.predict(vec) results[(x, y, width, height)] = "01234567890X"[int(label)] scale = SCREEN_HEIGHT / unrotated.shape[0] unrotated = cv2.cvtColor(grayscale, cv.CV_GRAY2BGR) scaled = cv2.resize(unrotated, (0, 0), fx=scale, fy=scale) for roi in rois: x = int(roi[0] * scale) y = int(roi[1] * scale) width = int(roi[2] * scale) height = int(roi[3] * scale) cv2.rectangle(scaled, (x, y), (x + width, y + height), (0, 255, 0, 0), 1) if results[roi] == "X": continue cv2.putText(scaled, results[roi], (x, y), cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 0)) cv2.imshow(__file__, scaled) cv2.waitKey()
def main(): import sys classifier = cv2.SVM() classifier.load(sys.argv[1]) im = cv2.imread(sys.argv[2]) im = cv2.GaussianBlur(im, (3,3), 0) imshow_large(__file__, im) cv2.waitKey() unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() binarized = binarize(unrotated) rois = get_rois(binarized) results = {} grayscale = cv2.cvtColor(unrotated, cv.CV_BGR2GRAY) for (x,y,width,height) in rois: roi = grayscale[y:y+height,x:x+width] vec = extract_features(roi) label = classifier.predict(vec) results[(x,y,width,height)] = "01234567890X"[int(label)] scale = SCREEN_HEIGHT/unrotated.shape[0] unrotated = cv2.cvtColor(grayscale, cv.CV_GRAY2BGR) scaled = cv2.resize(unrotated, (0,0), fx=scale, fy=scale) for roi in rois: x = int(roi[0]*scale) y = int(roi[1]*scale) width = int(roi[2]*scale) height = int(roi[3]*scale) cv2.rectangle(scaled, (x,y), (x+width, y+height), (0,255,0,0), 1) if results[roi] == "X": continue cv2.putText(scaled, results[roi], (x, y), cv.CV_FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 0)) cv2.imshow(__file__, scaled) cv2.waitKey()
def main(): import sys in_dir = sys.argv[1] images = map(lambda x: P.join(in_dir, x), os.listdir(in_dir)) out_dir = sys.argv[2] counters = {} for subdir in "0123456789X": path = P.join(out_dir, subdir) if not P.isdir(path): os.makedirs(path) files = os.listdir(path) indices = map(lambda x: int(P.splitext(x)[0]), files) try: counters[subdir] = max(indices) + 1 except ValueError: counters[subdir] = 0 for fpath in images: print fpath im = cv2.imread(fpath) im = cv2.GaussianBlur(im, (3, 3), 0) imshow_large(__file__, im) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): continue unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() elif key & 0xff == ord("n"): continue binarized = binarize(unrotated) scale = SCREEN_HEIGHT / unrotated.shape[0] colorbin = copy.deepcopy(unrotated) colorbin = cv2.resize(colorbin, (0, 0), fx=scale, fy=scale) rois = get_rois(binarized) for (x, y, width, height) in rois: x = int(x * scale) y = int(y * scale) width = int(width * scale) height = int(height * scale) cv2.rectangle(colorbin, (x, y), (x + width, y + height), (255, 0, 0, 0), 1) cv2.imshow(__file__, colorbin) key = cv2.waitKey() if key & 0xff == ord("q"): break elif key & 0xff == ord("n"): continue for (x, y, width, height) in rois: colorbin2 = copy.deepcopy(colorbin) x_ = int(x * scale) y_ = int(y * scale) width_ = int(width * scale) height_ = int(height * scale) cv2.rectangle(colorbin2, (x_, y_), (x_ + width_, y_ + height_), (0, 0, 255, 0), 1) sub = unrotated[y:y + height, x:x + width] supers = cv2.resize(sub, (192, 192), interpolation=cv.CV_INTER_NN) cv2.imshow(__file__, colorbin2) cv2.imshow("supers", supers) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): # move on to the next image break elif (key & 0xff) in map(ord, "0123456789"): digit = chr(key & 0xff) subdir = P.join(out_dir, digit) out_file = P.join(subdir, str(counters[digit]) + ".png") cv2.imwrite(out_file, sub) counters[digit] += 1 else: subdir = P.join(out_dir, "X") out_file = P.join(subdir, str(counters["X"]) + ".png") cv2.imwrite(out_file, sub) counters["X"] += 1
def main(): import sys in_dir = sys.argv[1] images = map(lambda x: P.join(in_dir, x), os.listdir(in_dir)) out_dir = sys.argv[2] counters = {} for subdir in "0123456789X": path = P.join(out_dir, subdir) if not P.isdir(path): os.makedirs(path) files = os.listdir(path) indices = map(lambda x: int(P.splitext(x)[0]), files) try: counters[subdir] = max(indices) + 1 except ValueError: counters[subdir] = 0 for fpath in images: print fpath im = cv2.imread(fpath) im = cv2.GaussianBlur(im, (3,3), 0) imshow_large(__file__, im) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): continue unrotated = unrotate(im) # # Check if image is right way up, correct otherwise # imshow_large(__file__, unrotated) key = cv2.waitKey() if key & 0xff == ord("r"): unrotated = cv2.flip(cv2.flip(unrotated, 0), 1) imshow_large(__file__, unrotated) cv2.waitKey() elif key & 0xff == ord("n"): continue binarized = binarize(unrotated) scale = SCREEN_HEIGHT/unrotated.shape[0] colorbin = copy.deepcopy(unrotated) colorbin = cv2.resize(colorbin, (0,0), fx=scale, fy=scale) rois = get_rois(binarized) for (x,y,width,height) in rois: x = int(x*scale) y = int(y*scale) width = int(width*scale) height = int(height*scale) cv2.rectangle(colorbin, (x,y), (x+width, y+height), (255,0,0,0), 1) cv2.imshow(__file__, colorbin) key = cv2.waitKey() if key & 0xff == ord("q"): break elif key & 0xff == ord("n"): continue for (x,y,width,height) in rois: colorbin2 = copy.deepcopy(colorbin) x_ = int(x*scale) y_ = int(y*scale) width_ = int(width*scale) height_ = int(height*scale) cv2.rectangle(colorbin2, (x_,y_), (x_+width_, y_+height_), (0,0,255,0), 1) sub = unrotated[y:y+height, x:x+width] supers = cv2.resize(sub, (192, 192), interpolation=cv.CV_INTER_NN) cv2.imshow(__file__, colorbin2) cv2.imshow("supers", supers) key = cv2.waitKey() if key & 0xff == ord("q"): sys.exit(0) elif key & 0xff == ord("n"): # move on to the next image break elif (key & 0xff) in map(ord, "0123456789"): digit = chr(key & 0xff) subdir = P.join(out_dir, digit) out_file = P.join(subdir, str(counters[digit]) + ".png") cv2.imwrite(out_file, sub) counters[digit] += 1 else: subdir = P.join(out_dir, "X") out_file = P.join(subdir, str(counters["X"]) + ".png") cv2.imwrite(out_file, sub) counters["X"] += 1