What up we can find all empties, we best
This commit is contained in:
parent
5165609ac8
commit
6393b12e82
124
main.py
124
main.py
|
@ -3,11 +3,15 @@ import sys
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
import runner
|
import runner
|
||||||
from util import load_classifier, PIECE, COLOR, POSITION, Board, Squares, PieceAndColor
|
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)
|
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()
|
sift = cv2.xfeatures2d.SIFT_create()
|
||||||
if mystery_image is None:
|
if mystery_image is None:
|
||||||
mystery_image = cv2.imread("training_images/rook/white/rook_training_D4_2.png")
|
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
|
return probs
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,36 +75,36 @@ def test_entire_board() -> None:
|
||||||
|
|
||||||
|
|
||||||
def predict(square: np.ndarray, position: POSITION) -> PIECE:
|
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:
|
left, right = x[:-1], x[1:]
|
||||||
empty_classifier = load_classifier(f"classifiers/classifier_empty/white_piece_on_{color}_square.pkl")
|
X = np.array([left, right]).T.flatten()
|
||||||
prob = empty_classifier.predict_proba(np.array(y).reshape(1, -1))
|
Y = np.array([y, y]).T.flatten()
|
||||||
print(f"{file}{rank}, {color}: {prob[0, 1]}")
|
area = sum(np.diff(x) * y)
|
||||||
if prob[0, 1] > 0.5:
|
plt.plot(X, Y)
|
||||||
return PIECE.EMPTY
|
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
|
return None
|
||||||
|
|
||||||
|
def remove_most_empties(warped):
|
||||||
if __name__ == '__main__':
|
|
||||||
board = cv2.imread("whole_boards/boards_for_empty/board_1554286488.605142_rank_3.png")
|
|
||||||
warped = runner.warp_board(board)
|
|
||||||
|
|
||||||
empty = 0
|
empty = 0
|
||||||
|
|
||||||
files = "ABCDEFGH"
|
|
||||||
ranks = [1, 2, 3, 4, 5, 6, 7, 8]
|
|
||||||
|
|
||||||
|
|
||||||
non_empties = []
|
non_empties = []
|
||||||
|
|
||||||
for position in POSITION:
|
for position in POSITION:
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
src = runner.get_square(warped, position)
|
src = runner.get_square(warped, position)
|
||||||
width, height, _ = src.shape
|
width, height, _ = src.shape
|
||||||
src = src[width//25:, height//25:]
|
src = src[width // 25:, height // 25:]
|
||||||
# src = src[:-width//200, :-height//200]
|
# src = src[:-width//200, :-height//200]
|
||||||
segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.8, k=150, min_size=700)
|
segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.8, k=150, min_size=700)
|
||||||
segment = segmentator.processImage(src)
|
segment = segmentator.processImage(src)
|
||||||
|
@ -112,44 +116,32 @@ if __name__ == '__main__':
|
||||||
masked.mask = mask != i
|
masked.mask = mask != i
|
||||||
|
|
||||||
y, x = np.where(segment == i)
|
y, x = np.where(segment == i)
|
||||||
|
pls.append(len(y))
|
||||||
top, bottom, left, right = min(y), max(y), min(x), max(x)
|
top, bottom, left, right = min(y), max(y), min(x), max(x)
|
||||||
|
|
||||||
dst = masked.filled()[top: bottom + 1, left: right + 1]
|
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)
|
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")
|
print(f"{position} is nonempty")
|
||||||
non_empties.append([f"{position}", src])
|
non_empties.append([position, src])
|
||||||
print(counter)
|
|
||||||
print(np.max(segment))
|
|
||||||
empty += 1
|
empty += 1
|
||||||
print("++"*20)
|
|
||||||
print(counter)
|
|
||||||
print(64-empty)
|
|
||||||
|
|
||||||
for non_empty in non_empties:
|
print(64 - empty)
|
||||||
cv2.imshow(non_empty[0], non_empty[1])
|
|
||||||
cv2.waitKey(0)
|
return non_empties
|
||||||
exit()
|
|
||||||
|
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")
|
#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])
|
#print(empty_classifier.predict_proba(np.array([0]*16).reshape(1, -1))[0, 1])
|
||||||
|
@ -157,21 +149,33 @@ if __name__ == '__main__':
|
||||||
#exit()
|
#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
|
counter = 0
|
||||||
|
completely_non_empties = []
|
||||||
|
for position, square in non_empties:
|
||||||
|
#predict(square, position)
|
||||||
|
|
||||||
for file in files:
|
#y, x = np.histogram(square.ravel(), bins=32, range=[0, 256])
|
||||||
for rank in ranks:
|
#left, right = x[:-1], x[1:]
|
||||||
square_img = runner.get_square(warped, file, rank)
|
#X = np.array([left, right]).T.flatten()
|
||||||
if predict(square_img, file, rank) == 'empty':
|
#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
|
counter += 1
|
||||||
|
else:
|
||||||
|
completely_non_empties.append([position, square])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print(counter)
|
print(counter)
|
||||||
|
for position, square in completely_non_empties:
|
||||||
|
cv2.imshow(f"{position}", square)
|
||||||
|
cv2.waitKey(0)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ def train_empty_or_piece_hist() -> None:
|
||||||
for piece in (PIECE.EMPTY, PIECE.ROOK, PIECE.KNIGHT):
|
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")):
|
for filename in glob.glob(os.path.join("training_images", f"{piece}", f"{square_color}_square", "*.png")):
|
||||||
img = cv2.imread(filename)
|
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)
|
X.append(y)
|
||||||
Y.append(piece == PIECE.EMPTY)
|
Y.append(piece == PIECE.EMPTY)
|
||||||
|
|
||||||
|
@ -176,9 +176,10 @@ def get_square(warped_board: np.ndarray, position: POSITION) -> np.ndarray:
|
||||||
square_size = size // 8
|
square_size = size // 8
|
||||||
padding = 0
|
padding = 0
|
||||||
|
|
||||||
x1 = side + (square_size * position.file)
|
x1 = side + (square_size * (position.file - 1))
|
||||||
x2 = x1 + square_size
|
x2 = x1 + square_size
|
||||||
y1 = max(0, side + (square_size * (8 - position.rank)) - padding) # 8 - rank because chessboard is from 8 to 1
|
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)
|
y2 = min(width, y1 + square_size + padding)
|
||||||
|
|
||||||
square = warped_board[y1:y2, x1:x2]
|
square = warped_board[y1:y2, x1:x2]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user