Skip to content

Commit

Permalink
relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kochsebastian committed May 23, 2020
1 parent c61c3b9 commit c4b6219
Show file tree
Hide file tree
Showing 8 changed files with 29,454 additions and 29 deletions.
1 change: 0 additions & 1 deletion code/chessboard_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,3 @@ def find_chessboard_from_image(img):
maxY = y + h if y + h > maxY else maxY

return True, img, minX, minY, maxX, maxY, img

37 changes: 20 additions & 17 deletions code/game_state_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self):
self.expected_move_to_detect = "" #This variable stores the move we should see next, if we don't see the right one in the next iteration, we wait and try again. This solves the slow transition problem: for instance, starting with e2e4, the screenshot can happen when the pawn is on e3, that is a possible position. We always have to double check that the move is done.
self.previous_chessboard_image = [] #Storing the chessboard image from previous iteration
self.executed_moves = [] #Store the move detected on san format
self.engine = chess.engine.SimpleEngine.popen_uci("/Users/sebastiankoch/OnlineChessBot/engine/stockfish-10-64")
self.engine = chess.engine.SimpleEngine.popen_uci("engine/stockfish-11-64")
self.board = chess.Board() #This object comes from the "chess" package, the moves are stored inside it (and it has other cool features such as showing all the "legal moves")
self.board_position_on_screen = []
self.sct = mss.mss()
Expand Down Expand Up @@ -152,12 +152,16 @@ def register_move_if_needed(self):
diff = cv2.absdiff(new_board, old_board)
if diff.mean() == 0:
return False, ([], []), (old_board, new_board)
cv2.waitKey(50)
cv2.waitKey(100)
new_board2 = chessboard_detection.get_chessboard(self)
while cv2.absdiff(new_board,new_board2).mean()>0:
# cv2.waitKey(10)
# new_board3 = chessboard_detection.get_chessboard(self)
while cv2.absdiff(new_board,new_board2).mean()>0:# or cv2.absdiff(new_board,new_board3).mean()>0:
new_board = chessboard_detection.get_chessboard(self)
cv2.waitKey(50)
cv2.waitKey(100)
new_board2 = chessboard_detection.get_chessboard(self)
# cv2.waitKey(10)
# new_board3 = chessboard_detection.get_chessboard(self)



Expand Down Expand Up @@ -236,13 +240,13 @@ def play_next_move(self,factor,strength,variance):
else:
print('depth_mode')
move_time = time.time()
engine_process = self.engine.play(self.board, chess.engine.Limit(depth=20))
engine_process = self.engine.play(self.board, chess.engine.Limit(depth=26))
move_time = time.time() - move_time
# engine_process = self.engine.go(depth=25)#
except EngineTerminatedError:
print('restart')
# self.engine = chess.uci.popen_engine("/Users/sebastiankoch/OnlineChessBot/engine/stockfish-10-64")
self.engine = chess.engine.SimpleEngine.popen_uci("/Users/sebastiankoch/OnlineChessBot/engine/stockfish-10-64")
# self.engine = chess.uci.popen_engine("engine/stockfish-11-64")
self.engine = chess.engine.SimpleEngine.popen_uci("engine/stockfish-11-64")
return 0,0

postthink = time.time()
Expand All @@ -269,8 +273,7 @@ def play_next_move(self,factor,strength,variance):
# Having the positions we can drag the piece:
pyautogui.moveTo(int(centerXOrigin), int(centerYOrigin), 0.0001)
# pyautogui.click(clicks=2,button='left')
pyautogui.dragTo(int(centerXOrigin), int(centerYOrigin) + 1, button='left',
duration=0.0001) # This small click is used to get the focus back on the browser window
pyautogui.dragTo(int(centerXOrigin), int(centerYOrigin) + 1, button='left', duration=0.2) # This small click is used to get the focus back on the browser window

pyautogui.dragTo(int(centerXDest), int(centerYDest), button='left', duration=0.11)
# print(f"mousetime: {time.time()-mousetime}")
Expand Down Expand Up @@ -326,7 +329,7 @@ def build_fen(self,we_are_white,rochade = 'KQkq' ):

self.moves_to_detect_before_use_engine = 0 # if v.get() else 1

pieces = sorted(os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces'))
pieces = sorted(os.listdir('pieces'))

vis_glob = np.array([])
piece_notation = ['b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R']
Expand All @@ -342,7 +345,7 @@ def build_fen(self,we_are_white,rochade = 'KQkq' ):
image_list = [get_square_image(i, j, position_detection) for j in (range(8) if we_are_white else reversed(range(8)))]
answers = piece_on_square_list(image_list)
for answer in answers:
im = cv2.imread(os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces', pieces[answer]))
im = cv2.imread(os.path.join('pieces', pieces[answer]))
if vis.size == 0:
vis = im
fen_str += piece_notation[answer]
Expand Down Expand Up @@ -430,7 +433,7 @@ def our_side(self):
# position_detection = chessboard_detection.get_chessboard(self, (800, 800))
# piece_notation = ['b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R']
#
# pieces = sorted(os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces'))
# pieces = sorted(os.listdir('pieces'))
#
# vis_glob = np.array([])
#
Expand All @@ -457,7 +460,7 @@ def our_side(self):
# if (mod_i)%8==0 and mod_i!=0:
# fen_str += '/'
#
# im = cv2.imread(os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces', pieces[answer]))
# im = cv2.imread(os.path.join('pieces', pieces[answer]))
# im_list.append(im)
# fen_str += piece_notation[answer]
#
Expand Down Expand Up @@ -507,10 +510,10 @@ def build_fen_guess_side(self):
self.moves_to_detect_before_use_engine = 0 # if v.get() else 1

fen_str = ''
position_detection = chessboard_detection.get_chessboard(self, (800, 800))
position_detection = chessboard_detection.get_chessboard(self, (1024, 1024))
piece_notation = ['b', 'k', 'n', 'p', 'q', 'r', '*', 'B', 'K', 'N', 'P', 'Q', 'R']

pieces = sorted(os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces'))
pieces = sorted(os.listdir('pieces'))

vis_glob = np.array([])

Expand All @@ -533,7 +536,7 @@ def build_fen_guess_side(self):
if piece_notation.index('K') == answer:
white_king_position = i*8+j

im = cv2.imread(os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces', pieces[answer]))
im = cv2.imread(os.path.join('pieces', pieces[answer]))
im_list.append(im)
fen_str += piece_notation[answer]

Expand All @@ -543,7 +546,7 @@ def build_fen_guess_side(self):

print(f"-->Prediction: {prediction_time}")

if white_king_position ==-1or black_king_position==-1:
if white_king_position ==-1 or black_king_position==-1:
raise NoValidPosition
if black_king_position < white_king_position:

Expand Down
2 changes: 1 addition & 1 deletion code/generate_trainset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
for i in range(8):
for j in range(8):
image =board_basics.get_square_image(i,j,resized_chessboard)
cv2.imwrite(f"/Users/sebastiankoch/OnlineChessBot/pieces/{i}{j}.png", image)
cv2.imwrite(f"pieces/{i}{j}.png", image)

12 changes: 6 additions & 6 deletions code/ml_model.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@

from tensorflow.keras.models import Sequential, load_model
def init_binary():
model_path = '/Users/sebastiankoch/OnlineChessBot/model_binary/binarymodel.h5'
model_weights_path = '/Users/sebastiankoch/OnlineChessBot/model_binary/binaryweights.hdf5'
# model_path = '/Users/sebastiankoch/OnlineChessBot/model_binary/modelmnis.h5'
# model_weights_path = '/Users/sebastiankoch/OnlineChessBot/model_binary/weightsmnist.hdf5'
model_path = 'model_binary/binarymodel.h5'
model_weights_path = 'model_binary/binaryweights.hdf5'
# model_path = 'model_binary/modelmnis.h5'
# model_weights_path = 'model_binary/weightsmnist.hdf5'
global binary_model
binary_model = load_model(model_path)
binary_model.load_weights(model_weights_path)
# from tensorflow.keras.utils import plot_model
# plot_model(model, to_file='model.png')

def init_class():
model_path = '/Users/sebastiankoch/OnlineChessBot/model_class/model128.h5'
model_weights_path = '/Users/sebastiankoch/OnlineChessBot/model_class/weights128.hdf5'
model_path = 'model_class/model128.h5'
model_weights_path = 'model_class/weights128.hdf5'
global class_model
class_model = load_model(model_path)
class_model.load_weights(model_weights_path)
Expand Down
4 changes: 2 additions & 2 deletions code/rebuildBoard.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
resized_chessboard = chessboard_detection.get_chessboard(game_state,resolution=(800,800))
# cv2.imshow('test',resized_chessboard)
# cv2.waitKey(0)
pieces = sorted(os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces'))
pieces = sorted(os.listdir('pieces'))
vis = np.array([])
vis_glob = np.array([])
piece_notation =['b','k','n','p','q','r','*','B','K','N','P','Q','R']
Expand All @@ -44,7 +44,7 @@
for j in order2:
image = board_basics.get_square_image(i,j,resized_chessboard)
answer = board_basics.piece_on_square(image)
im = cv2.imread(os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces',pieces[answer]))
im = cv2.imread(os.path.join('pieces',pieces[answer]))
if vis.size==0:
vis=im
fen_str+=piece_notation[answer]
Expand Down
4 changes: 2 additions & 2 deletions code/test_mlmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
if __name__ == '__main__':

ml_model.init_binary()
squares=os.listdir('/Users/sebastiankoch/OnlineChessBot/pieces')
squares=os.listdir('pieces')
for name in squares:
square= cv2.imread(os.path.join('/Users/sebastiankoch/OnlineChessBot/pieces',name),0)
square= cv2.imread(os.path.join('pieces',name),0)
# square = cv2.resize(square,(32,32))
square = cv2.resize(square,(32,32))

Expand Down
Binary file added model_class/model128.pb
Binary file not shown.
Loading

0 comments on commit c4b6219

Please sign in to comment.