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
def main(chess): ran_stockfish = False while True: image = cv2.imread(img_filename) if image is None: continue img_binary, img_r = ur.unrotate(image) if img_r is None or np.max(img_binary) < 1e-5: continue h, w, d = img_r.shape edges = get_vert_grad(img_r) row_counts = get_rows(edges) max_row_indexes = sorted(range(len(row_counts)), key = lambda k: row_counts[k], reverse=True) rows_changed = get_rows_changed(max_row_indexes) if rows_changed is None or len(rows_changed) != 9: continue edges = np.rot90(img_r) edges = get_vert_grad(edges) col_counts = get_rows(edges) max_col_indexes = sorted(range(len(col_counts)), key = lambda k: col_counts[k], reverse=True) cols_changed = get_rows_changed(max_col_indexes, True) if cols_changed is None or len(cols_changed) != 9: continue edges = np.rot90(edges, k=3) low_y, high_y, low_x, high_x = rows_changed[7], rows_changed[8], cols_changed[7], cols_changed[8] square = img_binary[low_x+10:high_x-10, low_y+10:high_y-10] if np.average(square) < 100: print "Could not properly detect bottom right corner: %d with threshold >=100."%np.average(square) continue low_y, high_y, low_x, high_x = rows_changed[7], rows_changed[8], cols_changed[0], cols_changed[1] square = img_r[low_x+10:high_x-10, low_y+10:high_y-10] if np.average(square) > 80: print "Could not properly detect top right corner: %d with threshold <=50."%np.average(square) continue board = determine_board_configuration(img_r, img_binary, rows_changed, cols_changed) if GRAPH: for row_idx in cols_changed: cv2.line(img_r, (0, row_idx), (w, row_idx), (0, 0, 255), 1) for row_idx in rows_changed: cv2.line(img_r, (row_idx, 0), (row_idx, h), (0, 255, 0), 1) cv2.imwrite("../images/temp.png", img_r) board = board[:, ::-1].T if not ran_stockfish: best_move, score, mate = chess.assisted_human_turn() if best_move is not None and score is None: print "\n\nSTOCKFISH RECOMMENDS: %s with mate in %s\n\n"%(best_move, mate) elif best_move is not None: print "\n\nSTOCKFISH RECOMMENDS: %s with a score of %f\n\n"%(best_move, score / 100.0) ran_stockfish = True move = obtain_moves(board, chess.board) if move is None: continue try: chess._receive_move(move) except IllegalMoveException as e: print "Illegal move detected!" continue print "Move %s successful!"%move ran_stockfish = False