segment finding is nice
67
main.py
|
@ -5,11 +5,12 @@ import runner
|
|||
from sklearn.externals import joblib
|
||||
import numpy as np
|
||||
import operator
|
||||
from matplotlib import pyplot as plt
|
||||
import glob
|
||||
import os
|
||||
import heapq
|
||||
import math
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
pieces = ['rook', 'knight']
|
||||
#pieces = ['rook', 'knight']
|
||||
|
@ -381,16 +382,18 @@ def selective_search(image, use_fast=False, use_slow=False):
|
|||
# close image show window
|
||||
cv2.destroyAllWindows()
|
||||
|
||||
|
||||
def predict(square, file, rank):
|
||||
color = runner.compute_color(file, rank)
|
||||
empty_var_classifier = load_classifier(f"classifiers/classifier_empty_var/{color}.pkl")
|
||||
square_color = runner.compute_color(file, rank)
|
||||
|
||||
magnitude_of_var = np.linalg.norm(cv2.meanStdDev(square)[1])
|
||||
y, x = np.histogram(square.ravel(), bins=256, range=[0, 256]) # TODO: Maybe img.ravel() ?
|
||||
|
||||
prob = empty_var_classifier.predict_proba(np.array(magnitude_of_var).reshape(-1, 1))
|
||||
print(prob[0, 1])
|
||||
if (prob[0, 1]) > 0.5:
|
||||
return 'empty'
|
||||
for color in ['black', 'white']:
|
||||
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 'empty'
|
||||
|
||||
return None
|
||||
|
||||
|
@ -398,6 +401,7 @@ def predict(square, file, rank):
|
|||
|
||||
@lru_cache()
|
||||
def load_classifier(filename):
|
||||
print(f"loading {filename}")
|
||||
return joblib.load(filename)
|
||||
|
||||
|
||||
|
@ -405,11 +409,56 @@ def load_classifier(filename):
|
|||
|
||||
if __name__ == '__main__':
|
||||
|
||||
board = cv2.imread("whole_boards/boards_for_empty/board_1554288951.972197_.png")
|
||||
warped = runner.warp_board(board)
|
||||
|
||||
empty = 0
|
||||
|
||||
|
||||
files = "ABCDEFGH"
|
||||
ranks = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
|
||||
|
||||
non_empties = []
|
||||
|
||||
for file in files:
|
||||
for rank in ranks:
|
||||
src = runner.get_square(warped, file, rank)
|
||||
#src = cv2.GaussianBlur(src, (5, 5), 0)
|
||||
segmentator = cv2.ximgproc.segmentation.createGraphSegmentation(sigma=0.75, k=175, min_size=750)
|
||||
segment = segmentator.processImage(src)
|
||||
mask = segment.reshape(list(segment.shape) + [1]).repeat(3, axis=2)
|
||||
masked = np.ma.masked_array(src, fill_value=0)
|
||||
for i in range(np.max(segment)):
|
||||
masked.mask = mask != i
|
||||
y, x = np.where(segment == i)
|
||||
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()}_{file}{rank}.jpg", dst)
|
||||
|
||||
if np.max(segment) >= 2:
|
||||
print(f"{file}{rank} is nonempty")
|
||||
non_empties.append([f"{file}{rank}", src])
|
||||
print(np.max(segment))
|
||||
empty += 1
|
||||
print(64-empty)
|
||||
|
||||
for non_empty in non_empties:
|
||||
cv2.imshow(non_empty[0], non_empty[1])
|
||||
cv2.waitKey(0)
|
||||
exit()
|
||||
|
||||
#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()
|
||||
|
||||
|
||||
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]
|
||||
ranks = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
|
||||
counter = 0
|
||||
|
||||
|
|
47
runner.py
|
@ -1,3 +1,5 @@
|
|||
from functools import lru_cache
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import glob
|
||||
|
@ -9,9 +11,13 @@ from sklearn.externals import joblib
|
|||
from sklearn import neural_network
|
||||
import heapq
|
||||
from datetime import datetime
|
||||
|
||||
from sklearn.pipeline import make_pipeline
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
|
||||
import utils
|
||||
|
||||
|
||||
import random
|
||||
|
||||
pieces = ["rook", "knight"]
|
||||
colors = ['black', 'white']
|
||||
|
@ -214,37 +220,28 @@ def load_training_data(spec_piece, color):
|
|||
|
||||
|
||||
def train_empty_or_piece_var():
|
||||
pieces = ['empty', 'knight', 'rook']
|
||||
for color in colors:
|
||||
|
||||
for square_color in ["black", "white"]:
|
||||
X = None
|
||||
Y = None
|
||||
for piece in ['empty', 'rook', 'knight']:
|
||||
|
||||
total_weight = 0
|
||||
for piece in pieces:
|
||||
total_weight += len(glob.glob(os.path.join("training_images", f"{piece}", f"{color}_square", "*.png")))
|
||||
|
||||
current_weight = len(glob.glob(os.path.join("training_images", 'empty', f"{color}_square", "*.png")))
|
||||
|
||||
for piece in pieces:
|
||||
piece_class = int('empty' == piece)
|
||||
for filename in glob.glob(os.path.join("training_images", piece, f"{color}_square", "*.png")):
|
||||
for filename in glob.glob(os.path.join("training_images", f"{piece}", f"{square_color}_square", "*.png")):
|
||||
piece_class = 'empty' == piece
|
||||
img = cv2.imread(filename)
|
||||
|
||||
magnitude_of_var = np.linalg.norm(cv2.meanStdDev(img)[1])
|
||||
y, x = np.histogram(img.ravel(), bins=256, range=[0, 256]) # TODO: Maybe img.ravel() ?
|
||||
|
||||
if X is None:
|
||||
X = np.array(magnitude_of_var)
|
||||
X = np.array(y)
|
||||
Y = np.array([piece_class])
|
||||
else:
|
||||
X = np.vstack((X, magnitude_of_var))
|
||||
X = np.vstack((X, y))
|
||||
Y = np.vstack((Y, [piece_class]))
|
||||
|
||||
classifier = svm.SVC(class_weight={0: current_weight, 1: total_weight - current_weight}, probability=True)
|
||||
classifier = make_pipeline(StandardScaler(),
|
||||
svm.SVC(C=10.0, gamma=0.01, probability=True))
|
||||
classifier.fit(X, Y)
|
||||
joblib.dump(classifier, f"classifiers/classifier_empty_var/{color}.pkl")
|
||||
|
||||
|
||||
joblib.dump(classifier, f"classifiers/classifier_empty/white_piece_on_{square_color}_square.pkl")
|
||||
|
||||
|
||||
def train_pieces_svm():
|
||||
|
@ -349,11 +346,11 @@ def get_square(warped_board, file, rank):
|
|||
file = files.index(file)
|
||||
rank = 8 - rank
|
||||
width, _, _ = warped_board.shape # board is square anyway
|
||||
|
||||
side = int(width * 0.04)
|
||||
side = int(width * 0.045)
|
||||
size = width - 2 * side
|
||||
square_size = size // 8
|
||||
|
||||
|
||||
padding = 0
|
||||
|
||||
x1 = side + (square_size * file)
|
||||
|
@ -413,6 +410,8 @@ def letter_to_int(letter):
|
|||
alphabet = list('ABCDEFGH')
|
||||
return alphabet.index(letter) + 1
|
||||
|
||||
|
||||
@lru_cache(maxsize=64)
|
||||
def compute_color(file, rank):
|
||||
if ((letter_to_int(file)+rank) % 2):
|
||||
return 'white'
|
||||
|
@ -435,4 +434,8 @@ def save_empty_fields(warped, skip_rank=None):
|
|||
utils.imwrite(f"training_images/empty/{color}_square/training_{file}{rank}_{datetime.utcnow().timestamp()}.png", square)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
train_empty_or_piece_var()
|
||||
|
||||
|
||||
|
||||
|
|
BIN
whole_boards/board_100_1554110446.049265_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_100_1554110868.509844_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_101_1554110456.565206_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_101_1554110872.145373_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_102_1554110461.608167_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_102_1554110875.760085_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_103_1554110464.448667_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_103_1554110879.22117_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_104_1554110468.218827_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_104_1554110883.02862_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_105_1554110471.419924_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_105_1554110888.644281_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_106_1554110474.697261_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_106_1554110891.434002_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_107_1554110480.810585_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_107_1554110894.478171_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_108_1554110483.561383_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_108_1554110897.507369_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_109_1554110486.656643_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_109_1554110901.973739_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_110_1554110490.183463_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_110_1554110905.427295_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_111_1554110495.059992_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_111_1554110908.484825_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_112_1554110499.357666_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_112_1554110915.119086_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_113_1554110502.186687_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_113_1554110918.898841_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_114_1554110506.330011_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_114_1554110922.817289_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_115_1554110509.155347_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_115_1554110926.333664_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_116_1554110512.214301_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_116_1554110931.919971_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_117_1554110519.52992_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_117_1554110936.746528_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_118_1554110522.620303_.png
Normal file
After Width: | Height: | Size: 2.5 MiB |
BIN
whole_boards/board_118_1554110940.892729_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_119_1554110525.738887_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_119_1554110945.263698_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_120_1554110529.408658_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_120_1554110948.841754_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_121_1554110532.664855_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_121_1554110952.591841_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_122_1554110536.505578_.png
Normal file
After Width: | Height: | Size: 1.8 MiB |
BIN
whole_boards/board_122_1554110955.720307_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_123_1554110539.703553_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_123_1554110962.783508_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_124_1554110546.340293_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_124_1554110965.792607_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_125_1554110553.985252_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_125_1554110970.968836_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_126_1554110564.364159_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_126_1554110975.82463_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_127_1554110567.554228_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_127_1554110981.022816_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_128_1554110570.75272_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_128_1554110984.471614_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_129_1554110574.623462_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_129_1554110987.984516_.png
Normal file
After Width: | Height: | Size: 1.7 MiB |
BIN
whole_boards/board_130_1554110610.646927_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_130_1554110991.103533_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_131_1554110614.765141_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_131_1554110994.478325_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_132_1554110618.537395_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_132_1554110997.3701_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_133_1554110623.3588_.png
Normal file
After Width: | Height: | Size: 2.5 MiB |
BIN
whole_boards/board_133_1554111000.25686_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_134_1554110628.706586_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_134_1554111003.624519_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_135_1554110643.250391_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_135_1554111043.447214_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_136_1554110648.187479_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_136_1554111047.405803_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_137_1554110652.747311_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_137_1554111050.529226_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_138_1554110655.564099_.png
Normal file
After Width: | Height: | Size: 1.7 MiB |
BIN
whole_boards/board_138_1554111054.135153_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_139_1554110660.29755_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_139_1554111060.880644_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_140_1554110664.249539_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_140_1554111063.835343_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_141_1554110673.752214_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_141_1554111066.869056_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_142_1554110678.056988_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_142_1554111069.716135_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_143_1554110681.439862_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_143_1554111072.681554_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_144_1554110685.775782_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_144_1554111076.102722_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_145_1554110689.952279_.png
Normal file
After Width: | Height: | Size: 2.0 MiB |
BIN
whole_boards/board_145_1554111079.171349_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_146_1554110693.922325_.png
Normal file
After Width: | Height: | Size: 2.4 MiB |
BIN
whole_boards/board_146_1554111083.029799_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_147_1554110697.751021_.png
Normal file
After Width: | Height: | Size: 2.3 MiB |
BIN
whole_boards/board_147_1554111086.513472_.png
Normal file
After Width: | Height: | Size: 2.1 MiB |
BIN
whole_boards/board_148_1554110702.055849_.png
Normal file
After Width: | Height: | Size: 2.2 MiB |
BIN
whole_boards/board_148_1554111089.55404_.png
Normal file
After Width: | Height: | Size: 2.5 MiB |