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
51c867af
Commit
51c867af
authored
11 months ago
by
zalonzo2
Browse files
Options
Downloads
Patches
Plain Diff
Trying to implement camera. Saving small changes
parent
af94d6f7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
board_detector.py
+3
-3
3 additions, 3 deletions
board_detector.py
game.py
+27
-1
27 additions, 1 deletion
game.py
with
30 additions
and
4 deletions
board_detector.py
+
3
−
3
View file @
51c867af
...
...
@@ -85,12 +85,12 @@ def filter_lines(lines, min_distance):
def
find_board
(
img
):
vertical_lines
,
horizontal_lines
=
find_longest_lines
(
img
)
print
(
"
# of Vertical:
"
,
len
(
vertical_lines
))
print
(
"
# of Horizontal:
"
,
len
(
horizontal_lines
))
if
(
len
(
vertical_lines
)
!=
9
or
len
(
horizontal_lines
)
!=
9
):
print
(
"
Error: Grid does not match expected 9x9
"
)
return
print
(
"
# of Vertical:
"
,
len
(
vertical_lines
))
print
(
"
# of Horizontal:
"
,
len
(
horizontal_lines
))
return
None
,
None
# create bitmasks for vert and horiz so we can get lines and intersections
height
,
width
,
_
=
img
.
shape
...
...
This diff is collapsed.
Click to expand it.
game.py
+
27
−
1
View file @
51c867af
...
...
@@ -9,12 +9,17 @@ import move_translator
import
cv2
import
os
import
time
from
picamera2
import
Picamera2
,
Preview
class
ChessGame
:
def
__init__
(
self
,
difficulty
,
show_cv
,
test_img
=
None
):
self
.
board
=
chess
.
Board
()
self
.
difficulty
=
difficulty
self
.
show_cv
=
show_cv
self
.
test_img
=
test_img
self
.
img_idx
=
0
self
.
picam2
=
Picamera2
()
def
start_game
(
self
):
print
(
f
"
Starting chess game (difficulty:
{
self
.
difficulty
}
)
"
)
...
...
@@ -24,11 +29,17 @@ class ChessGame:
init_show_cv
(
self
.
show_cv
)
preview_config
=
self
.
picam2
.
create_preview_configuration
(
main
=
{
"
size
"
:
(
512
,
512
)})
self
.
picam2
.
configure
(
preview_config
)
# self.get_img()
if
(
self
.
test_img
):
img_path
=
os
.
path
.
join
(
'
test_images
'
,
self
.
test_img
)
orig_img
=
cv2
.
imread
(
img_path
)
else
:
orig_img
=
cv2
.
imread
(
'
test_images/board1.jpg
'
)
# TODO - CHANGE TO MAKE IT RECEIVE INPUT FROM CAMERA
# orig_img = cv2.imread('test_images/board1.jpg') # TODO - CHANGE TO MAKE IT RECEIVE INPUT FROM CAMERA
orig_img
=
self
.
get_img
()
h
,
w
,
c
=
orig_img
.
shape
cropped_img
=
orig_img
[
0
:
h
,
int
(
w
/
2
-
h
/
2
-
60
):
int
(
w
/
2
+
h
/
2
+
100
)]
img
=
cv2
.
resize
(
cropped_img
,
(
512
,
512
))
...
...
@@ -39,6 +50,10 @@ class ChessGame:
cv2
.
destroyAllWindows
()
warped_img
,
sorted_warped_points
=
find_board
(
img
)
if
(
warped_img
is
None
):
return
color_grid
=
find_pieces
(
warped_img
,
sorted_warped_points
)
while
(
1
):
# game loop
...
...
@@ -67,6 +82,17 @@ class ChessGame:
# 4. call move_translator with argument of best move
pass
def
get_img
(
self
):
self
.
img_idx
+=
1
self
.
picam2
.
start_preview
(
Preview
.
QTGL
)
self
.
picam2
.
start
()
time
.
sleep
(
2
)
img_txt
=
'
board
'
+
str
(
self
.
img_idx
)
+
'
.jpg
'
print
(
img_txt
)
img_path
=
os
.
path
.
join
(
'
game_images
'
,
img_txt
)
metadata
=
self
.
picam2
.
capture_file
(
img_path
)
return
cv2
.
imread
(
img_path
)
if
__name__
==
"
__main__
"
:
parser
=
argparse
.
ArgumentParser
(
description
=
"
AI Chess Robot with Computer Vision
"
)
parser
.
add_argument
(
"
--difficulty
"
,
choices
=
[
"
easy
"
,
"
medium
"
,
"
hard
"
],
...
...
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