From 6393b12e821d6e742e66ebbc4cb40688e405d8b0 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 8 Apr 2019 23:59:06 +0200 Subject: [PATCH] What up we can find all empties, we best --- main.py | 124 ++++++++++++++++++++++++++++-------------------------- runner.py | 5 ++- 2 files changed, 67 insertions(+), 62 deletions(-) diff --git a/main.py b/main.py index c62167b2..f5d51cc0 100644 --- a/main.py +++ b/main.py @@ -3,11 +3,15 @@ import sys from collections import defaultdict from datetime import datetime +import matplotlib.pyplot as plt import numpy as np import runner from util import load_classifier, PIECE, COLOR, POSITION, Board, Squares, PieceAndColor +from sklearn.exceptions import DataConversionWarning +import warnings +warnings.filterwarnings(action='ignore', category=DataConversionWarning) np.set_printoptions(threshold=sys.maxsize) @@ -37,7 +41,7 @@ def pred_test(position: POSITION, mystery_image=None, empty_bias=False): sift = cv2.xfeatures2d.SIFT_create() if mystery_image is None: mystery_image = cv2.imread("training_images/rook/white/rook_training_D4_2.png") - probs = classify(mystery_image, sift, empty_bias=empty_bias) + probs = identify_piece(mystery_image, sift, empty_bias=empty_bias) return probs @@ -71,36 +75,36 @@ def test_entire_board() -> None: def predict(square: np.ndarray, position: POSITION) -> PIECE: - y, x = np.histogram(square.ravel(), bins=256, range=[0, 256]) + y, x = np.histogram(square.ravel(), bins=32, range=[0, 256]) - for color in COLOR: - empty_classifier = load_classifier(f"classifiers/classifier_empty/white_piece_on_{color}_square.pkl") - prob = empty_classifier.predict_proba(np.array(y).reshape(1, -1)) - print(f"{file}{rank}, {color}: {prob[0, 1]}") - if prob[0, 1] > 0.5: - return PIECE.EMPTY + left, right = x[:-1], x[1:] + X = np.array([left, right]).T.flatten() + Y = np.array([y, y]).T.flatten() + area = sum(np.diff(x) * y) + plt.plot(X, Y) + plt.xlabel(f"{position}") + #plt.show() + + + #for color in COLOR: + empty_classifier = load_classifier(f"classifiers/classifier_empty/white_piece_on_{position.color}_square.pkl") + prob = empty_classifier.predict_proba(np.array(y).reshape(1, -1)) + print(f"{position}, {position.color}: {prob[0, 1]}") + if prob[0, 1] > 0.95: + print(f"{position} is empty") + return PIECE.EMPTY return None - -if __name__ == '__main__': - board = cv2.imread("whole_boards/boards_for_empty/board_1554286488.605142_rank_3.png") - warped = runner.warp_board(board) - +def remove_most_empties(warped): empty = 0 - - files = "ABCDEFGH" - ranks = [1, 2, 3, 4, 5, 6, 7, 8] - - non_empties = [] - for position in POSITION: counter = 0 src = runner.get_square(warped, position) width, height, _ = src.shape - src = src[width//25:, height//25:] + src = src[width // 25:, height // 25:] # src = src[:-width//200, :-height//200] segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.8, k=150, min_size=700) segment = segmentator.processImage(src) @@ -112,44 +116,32 @@ if __name__ == '__main__': masked.mask = mask != i y, x = np.where(segment == i) - + pls.append(len(y)) top, bottom, left, right = min(y), max(y), min(x), max(x) dst = masked.filled()[top: bottom + 1, left: right + 1] - lel = (bottom - top) * (right - left) - #print(f"this is lel: {lel} ") - #print(f"this is meh: {np.sum(mask[:,:,0])} ") - if position == POSITION.H7: - print("--"*20) - print("H7") - print(lel) - print(len(y)) - print(np.max(segment)) -# print(lel) -# print(np.sum(mask[:, :, 0])) - print("--"*20) - - - pls.append(len(y)) - if len(y) < (164**2)*0.65: - counter += 1 - cv2.imwrite(f"segment_test/segment_{datetime.utcnow().timestamp()}_{position}.png", dst) - if np.max(segment) > 0 and not np.all([x < (164**2)*0.2 for x in pls]) and (np.max(segment) >= 3 or np.all([x < (164**2)*0.942 for x in pls])): + if np.max(segment) > 0 and not np.all([x < (164 ** 2) * 0.2 for x in pls]) and ( + np.max(segment) >= 3 or np.all([x < (164 ** 2) * 0.942 for x in pls])): print(f"{position} is nonempty") - non_empties.append([f"{position}", src]) - print(counter) - print(np.max(segment)) + non_empties.append([position, src]) empty += 1 - print("++"*20) - print(counter) - print(64-empty) - for non_empty in non_empties: - cv2.imshow(non_empty[0], non_empty[1]) - cv2.waitKey(0) - exit() + print(64 - empty) + + return non_empties + +if __name__ == '__main__': + + + #board = cv2.imread("whole_boards/boards_for_empty/board_1554286488.605142_rank_3.png") + board = cv2.imread("whole_boards/boards_for_empty/board_1554288606.075646_rank_1.png") + + warped = runner.warp_board(board) + + non_empties = remove_most_empties(warped) + #empty_classifier = load_classifier(f"classifiers/classifier_empty/white_piece_on_white_square.pkl") #print(empty_classifier.predict_proba(np.array([0]*16).reshape(1, -1))[0, 1]) @@ -157,21 +149,33 @@ if __name__ == '__main__': #exit() - board = cv2.imread("whole_boards/board_102_1554110461.608167_.png") - warped = runner.warp_board(board) - - files = "ABCDEFGH" - ranks = [1, 2, 3, 4, 5, 6, 7, 8] - counter = 0 + completely_non_empties = [] + for position, square in non_empties: + #predict(square, position) - for file in files: - for rank in ranks: - square_img = runner.get_square(warped, file, rank) - if predict(square_img, file, rank) == 'empty': + #y, x = np.histogram(square.ravel(), bins=32, range=[0, 256]) + #left, right = x[:-1], x[1:] + #X = np.array([left, right]).T.flatten() + #Y = np.array([y, y]).T.flatten() + #plt.plot(X, Y) + #plt.xlabel(f"{position}") + #plt.show() + + + + if predict(square,position) == PIECE.EMPTY: counter += 1 + else: + completely_non_empties.append([position, square]) + + + print(counter) + for position, square in completely_non_empties: + cv2.imshow(f"{position}", square) + cv2.waitKey(0) exit() diff --git a/runner.py b/runner.py index efe2a8f4..831cd287 100644 --- a/runner.py +++ b/runner.py @@ -82,7 +82,7 @@ def train_empty_or_piece_hist() -> None: for piece in (PIECE.EMPTY, PIECE.ROOK, PIECE.KNIGHT): for filename in glob.glob(os.path.join("training_images", f"{piece}", f"{square_color}_square", "*.png")): img = cv2.imread(filename) - y, x = np.histogram(img.ravel(), bins=256, range=[0, 256]) + y, x = np.histogram(img.ravel(), bins=32, range=[0, 256]) X.append(y) Y.append(piece == PIECE.EMPTY) @@ -176,9 +176,10 @@ def get_square(warped_board: np.ndarray, position: POSITION) -> np.ndarray: square_size = size // 8 padding = 0 - x1 = side + (square_size * position.file) + x1 = side + (square_size * (position.file - 1)) x2 = x1 + square_size y1 = max(0, side + (square_size * (8 - position.rank)) - padding) # 8 - rank because chessboard is from 8 to 1 + y2 = min(width, y1 + square_size + padding) square = warped_board[y1:y2, x1:x2]