Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AI Chess Robot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jhur22
AI Chess Robot
Commits
8666f472
Commit
8666f472
authored
10 months ago
by
zalonzo2
Browse files
Options
Downloads
Patches
Plain Diff
Played through ocr_alt_game1 correctly
parent
6f0e5f2f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
board_detector.py
+11
-11
11 additions, 11 deletions
board_detector.py
chess_ai.py
+12
-3
12 additions, 3 deletions
chess_ai.py
game.py
+22
-2
22 additions, 2 deletions
game.py
with
45 additions
and
16 deletions
board_detector.py
+
11
−
11
View file @
8666f472
...
...
@@ -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:
...
...
This diff is collapsed.
Click to expand it.
chess_ai.py
+
12
−
3
View file @
8666f472
...
...
@@ -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
)
...
...
This diff is collapsed.
Click to expand it.
game.py
+
22
−
2
View file @
8666f472
...
...
@@ -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
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment