Skip to content
Snippets Groups Projects
Commit 8666f472 authored by zalonzo2's avatar zalonzo2
Browse files

Played through ocr_alt_game1 correctly

parent 6f0e5f2f
No related branches found
No related tags found
No related merge requests found
......@@ -290,7 +290,7 @@ def find_pieces(warped_img, sorted_warped_points):
warped_img_pil = cv2_to_pil(warped_img)
warped_img_draw = ImageDraw.Draw(warped_img_pil)
reader = easyocr.Reader(['en'])
reader = easyocr.Reader(['en'], gpu=False, verbose=False)
filled_contour_mask = np.zeros_like(hsv_after)
pixel_thresh = 40
......@@ -474,17 +474,17 @@ def find_pieces(warped_img, sorted_warped_points):
# img_to_draw.text((y_min - 10, x_min), letter, fill=(255, 0, 0))
# print color_grid. only print when the color is found a lot in the square (> pixel_thresh times)
# if show_cv:
if show_cv:
# print("|avg_hue, num_pixels, letter|")
for row in color_grid:
print("||", end="")
for color, avg_hue, num_pixels, letter in row:
if num_pixels > pixel_thresh:
print(f"{int(avg_hue)}, {letter}\t|", end="")
# print(f"{color}, {letter}\t|", end="")
else:
print("\t\t|", end="")
print("|")
for row in color_grid:
print("||", end="")
for color, avg_hue, num_pixels, letter in row:
if num_pixels > pixel_thresh:
print(f"{int(avg_hue)}, {letter}\t|", end="")
# print(f"{color}, {letter}\t|", end="")
else:
print("\t\t|", end="")
print("|")
# img_to_draw._image.save('game_images/ocr_results.jpg')
# if show_cv:
......
......@@ -4,6 +4,11 @@ import io
# run command "pip install stockfish"
from stockfish import Stockfish
skip_camera = None
def init_stockfish(bool1):
global skip_camera
skip_camera = bool1
def read_from(file_name):
with open(r"%s" %file_name, 'r') as File_object: # 'with' autocloses file
file_data = File_object.read()
......@@ -68,9 +73,13 @@ def Stockfish_eval(FEN_string):
'''
# setting up stockfish
stockfish = Stockfish( path = "C:\ece445\stockfish\stockfish-windows-x86-64-avx2.exe", # install Stockfish and input path to .exe here
depth = 15, # 20 is good but slower, 15 is fine
parameters={"Skill Level":19}) # range: 0-19? 20 might exist or rounds to 19, https://github.com/vondele/Stockfish/blob/master/src/search.cpp
if skip_camera:
stockfish = Stockfish( path = "C:\ece445\stockfish\stockfish-windows-x86-64-avx2.exe", # install Stockfish and input path to .exe here
depth = 15, # 20 is good but slower, 15 is fine
parameters={"Skill Level":19}) # range: 0-19? 20 might exist or rounds to 19, https://github.com/vondele/Stockfish/blob/master/src/search.cpp
else:
stockfish = None # FIX FOR RASPI
# set state of board based off FEN
stockfish.set_fen_position(FEN_string)
......
......@@ -58,6 +58,7 @@ class ChessGame:
# then loop until checkmate. also handle illegal moves (writing to screen if we end up doing that or just LEDs)
init_global(self.show_cv, skip_camera, self.img_size)
init_stockfish(skip_camera)
if self.save_img_as:
self.loop = True
......@@ -74,9 +75,15 @@ class ChessGame:
# initial setup of board
self.do_cv()
self.board.turn = chess.WHITE
write_to("saved_FEN", self.board.fen())
self.player_turn()
self.ai_turn()
while(1): # game loop
self.board.fullmove_number += 1
self.player_turn()
......@@ -90,25 +97,36 @@ class ChessGame:
# game is over
def switch_turn(self):
if self.board.turn == chess.WHITE:
print("Next move: Black")
self.board.turn = chess.BLACK
else:
print("Next move: White")
self.board.turn = chess.WHITE
def player_turn(self):
# TODO - wait for user button, then check for valid move. loop until a valid move has been made
input("[Waiting to submit move. Replace with physical button]")
self.switch_turn()
self.do_cv()
# handle cheating
while cheat_check(self.board.fen()):
print("CHECKING: ", self.board.fen())
self.board = self.prev_board.copy()
print("Player is cheating. Return to this state and try again:")
print("Player is cheating. Return to this state and perform a valid move:")
print(self.board)
input("[Waiting to submit move. Replace with physical button]")
self.do_cv()
def ai_turn(self):
def ai_turn(self):
# print("Board before chess_AI:", self.board.fen())
info = chess_AI(self.board.fen(), 0)
# convert best move to coordinates to send
input("[SEND BEST MOVE TO ESP32 AND WAIT]")
self.switch_turn()
self.do_cv()
def do_cv(self):
......@@ -194,6 +212,8 @@ class ChessGame:
return cv2.imread(img_path)
def color_grid_to_fen(self, color_grid, color1, color2):
print(color1, "(top) is black. ", color2, "(bottom) is white.")
self.prev_board = self.board.copy()
for i, row in enumerate(color_grid):
for j, (color, _, _, letter) in enumerate(row):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment