2019-04-04 10:59:37 +00:00
|
|
|
import cv2
|
2019-04-04 20:25:15 +00:00
|
|
|
import sys
|
2019-04-05 01:31:30 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from datetime import datetime
|
2019-04-04 20:25:15 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
import matplotlib.pyplot as plt
|
2019-04-05 01:31:30 +00:00
|
|
|
import numpy as np
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
import runner
|
|
|
|
from util import load_classifier, PIECE, COLOR, POSITION, Board, Squares, PieceAndColor
|
2019-04-08 21:59:06 +00:00
|
|
|
from sklearn.exceptions import DataConversionWarning
|
|
|
|
import warnings
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
warnings.filterwarnings(action='ignore', category=DataConversionWarning)
|
2019-04-05 01:31:30 +00:00
|
|
|
np.set_printoptions(threshold=sys.maxsize)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def identify_piece(image: np.ndarray, sift : cv2.xfeatures2d_SIFT, empty_bias=False) -> PieceAndColor:
|
2019-04-04 10:59:37 +00:00
|
|
|
centers = np.load("training_data/centers.npy")
|
2019-04-05 01:31:30 +00:00
|
|
|
probs = defaultdict(lambda: defaultdict(float))
|
|
|
|
best = 0
|
|
|
|
best_piece = best_color = None
|
|
|
|
for piece in PIECE:
|
|
|
|
for color in COLOR:
|
2019-04-04 10:59:37 +00:00
|
|
|
#color = runner.compute_color(file, rank)
|
2019-04-05 01:31:30 +00:00
|
|
|
classifier = load_classifier(f"classifiers/classifier_{piece}/{color}.pkl")
|
2019-04-04 10:59:37 +00:00
|
|
|
features = runner.generate_bag_of_words(image, centers, sift)
|
|
|
|
prob = classifier.predict_proba(features)
|
|
|
|
probs[piece][color] = prob[0, 1]
|
2019-04-05 01:31:30 +00:00
|
|
|
if prob[0, 1] > best:
|
|
|
|
best_piece, best_color = piece, color
|
|
|
|
print(probs)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
if empty_bias:
|
2019-04-05 01:31:30 +00:00
|
|
|
probs[PIECE.EMPTY] *= 1.2
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
return best_piece, best_color
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def pred_test(position: POSITION, mystery_image=None, empty_bias=False):
|
2019-04-04 10:59:37 +00:00
|
|
|
sift = cv2.xfeatures2d.SIFT_create()
|
|
|
|
if mystery_image is None:
|
|
|
|
mystery_image = cv2.imread("training_images/rook/white/rook_training_D4_2.png")
|
2019-04-08 21:59:06 +00:00
|
|
|
probs = identify_piece(mystery_image, sift, empty_bias=empty_bias)
|
2019-04-04 10:59:37 +00:00
|
|
|
return probs
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def pre_process_and_train() -> None:
|
2019-04-04 10:59:37 +00:00
|
|
|
runner.do_pre_processing()
|
|
|
|
runner.train_pieces_svm()
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def build_board_from_squares(squares: Squares) -> Board:
|
2019-04-04 10:59:37 +00:00
|
|
|
sift = cv2.xfeatures2d.SIFT_create()
|
2019-04-05 01:31:30 +00:00
|
|
|
board = Board()
|
2019-04-04 10:59:37 +00:00
|
|
|
counter = 0
|
2019-04-05 01:31:30 +00:00
|
|
|
for position, square in squares.values():
|
|
|
|
likely_piece = identify_piece(square, sift)
|
|
|
|
board[position] = likely_piece
|
|
|
|
if likely_piece != PIECE.EMPTY:
|
2019-04-04 10:59:37 +00:00
|
|
|
counter += 1
|
|
|
|
|
|
|
|
print(counter)
|
|
|
|
print(64/(counter-1))
|
|
|
|
return board
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def test_entire_board() -> None:
|
|
|
|
board_img = cv2.imread("homo_pls_fuck.jpg")
|
|
|
|
warped = runner.warp_board(board_img)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
squares = runner.get_squares(warped)
|
|
|
|
board = build_board_from_squares(squares)
|
2019-04-04 10:59:37 +00:00
|
|
|
print(board)
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
def predict(square: np.ndarray, position: POSITION) -> PIECE:
|
2019-04-08 21:59:06 +00:00
|
|
|
y, x = np.histogram(square.ravel(), bins=32, range=[0, 256])
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
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()
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
#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
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
return None
|
2019-04-04 16:27:19 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
def remove_most_empties(warped):
|
2019-04-04 16:27:19 +00:00
|
|
|
empty = 0
|
|
|
|
non_empties = []
|
2019-04-05 01:31:30 +00:00
|
|
|
for position in POSITION:
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
src = runner.get_square(warped, position)
|
|
|
|
width, height, _ = src.shape
|
2019-04-08 21:59:06 +00:00
|
|
|
src = src[width // 25:, height // 25:]
|
2019-04-05 01:31:30 +00:00
|
|
|
# src = src[:-width//200, :-height//200]
|
|
|
|
segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.8, k=150, min_size=700)
|
|
|
|
segment = segmentator.processImage(src)
|
|
|
|
|
|
|
|
mask = segment.reshape(list(segment.shape) + [1]).repeat(3, axis=2)
|
|
|
|
masked = np.ma.masked_array(src, fill_value=0)
|
|
|
|
pls = []
|
|
|
|
for i in range(np.max(segment)):
|
|
|
|
masked.mask = mask != i
|
|
|
|
|
|
|
|
y, x = np.where(segment == i)
|
2019-04-08 21:59:06 +00:00
|
|
|
pls.append(len(y))
|
2019-04-05 01:31:30 +00:00
|
|
|
top, bottom, left, right = min(y), max(y), min(x), max(x)
|
|
|
|
|
|
|
|
dst = masked.filled()[top: bottom + 1, left: right + 1]
|
|
|
|
cv2.imwrite(f"segment_test/segment_{datetime.utcnow().timestamp()}_{position}.png", dst)
|
2019-04-04 20:25:15 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
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])):
|
2019-04-05 01:31:30 +00:00
|
|
|
print(f"{position} is nonempty")
|
2019-04-08 21:59:06 +00:00
|
|
|
non_empties.append([position, src])
|
2019-04-05 01:31:30 +00:00
|
|
|
empty += 1
|
2019-04-04 16:27:19 +00:00
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
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)
|
|
|
|
|
2019-04-04 16:27:19 +00:00
|
|
|
|
|
|
|
#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])
|
|
|
|
|
|
|
|
#exit()
|
|
|
|
|
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
counter = 0
|
|
|
|
completely_non_empties = []
|
|
|
|
for position, square in non_empties:
|
|
|
|
#predict(square, position)
|
|
|
|
|
|
|
|
#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()
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-08 21:59:06 +00:00
|
|
|
if predict(square,position) == PIECE.EMPTY:
|
2019-04-04 10:59:37 +00:00
|
|
|
counter += 1
|
2019-04-08 21:59:06 +00:00
|
|
|
else:
|
|
|
|
completely_non_empties.append([position, square])
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
print(counter)
|
2019-04-08 21:59:06 +00:00
|
|
|
for position, square in completely_non_empties:
|
|
|
|
cv2.imshow(f"{position}", square)
|
|
|
|
cv2.waitKey(0)
|
2019-04-04 10:59:37 +00:00
|
|
|
exit()
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
square_img = runner.get_square(warped, "D", 2)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
gray_square_img = cv2.cvtColor(square_img, cv2.COLOR_BGR2GRAY)
|
|
|
|
print(cv2.meanStdDev(gray_square_img)[1])
|
|
|
|
print(cv2.meanStdDev(square_img)[1])
|
|
|
|
cv2.imshow("square", square_img)
|
2019-04-04 10:59:37 +00:00
|
|
|
cv2.waitKey(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
print(pred_test("C", 2, square_img))
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
sift: cv2.xfeatures2d_SIFT = cv2.xfeatures2d.SIFT_create()
|
2019-04-05 01:31:30 +00:00
|
|
|
gray = cv2.cvtColor(square_img, cv2.COLOR_BGR2GRAY)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
|
|
|
kp, desc = sift.detectAndCompute(gray, None)
|
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
cv2.drawKeypoints(square_img, kp, square_img)
|
2019-04-04 10:59:37 +00:00
|
|
|
|
2019-04-05 01:31:30 +00:00
|
|
|
cv2.imshow("kp", square_img)
|
2019-04-04 10:59:37 +00:00
|
|
|
cv2.waitKey(0)
|
|
|
|
|
|
|
|
|