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
059830cd
Commit
059830cd
authored
11 months ago
by
zalonzo2
Browse files
Options
Downloads
Patches
Plain Diff
Code from glare images
parent
a096efdd
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
+15
-2
15 additions, 2 deletions
board_detector.py
game.py
+21
-7
21 additions, 7 deletions
game.py
with
36 additions
and
9 deletions
board_detector.py
+
15
−
2
View file @
059830cd
...
...
@@ -29,6 +29,16 @@ def find_longest_lines(img):
gray_img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
edges
=
cv2
.
Canny
(
gray_img
,
50
,
100
,
apertureSize
=
3
)
h
,
w
=
edges
.
shape
# don't detect any lines at the edges (make the edges black)
cutoff
=
5
for
y
in
range
(
0
,
h
):
for
x
in
range
(
0
,
w
):
if
x
<
cutoff
or
x
>
w
-
cutoff
or
y
<
cutoff
or
y
>
h
-
cutoff
:
# print(edges[y, x])
edges
[
y
,
x
]
=
0
if
(
show_cv
):
# cv2.imshow('Canny Filter', edges)
# cv2.waitKey(0)
...
...
@@ -47,8 +57,8 @@ def find_longest_lines(img):
filtered_horizontal
=
filter_lines
(
horizontal_line_points
,
50
)
# get the 9 largest lines
sorted_vertical
=
sorted
(
filtered_vertical
,
key
=
lambda
line
:
min
(
line
[
0
][
1
],
line
[
0
][
3
]))[:
9
]
sorted_horizontal
=
sorted
(
filtered_horizontal
,
key
=
lambda
line
:
min
(
line
[
0
][
0
],
line
[
0
][
2
]))[:
9
]
sorted_vertical
=
sorted
(
filtered_vertical
,
key
=
lambda
line
:
min
(
line
[
0
][
1
],
line
[
0
][
3
]))[:
12
]
sorted_horizontal
=
sorted
(
filtered_horizontal
,
key
=
lambda
line
:
min
(
line
[
0
][
0
],
line
[
0
][
2
]))[:
12
]
return
sorted_vertical
,
sorted_horizontal
...
...
@@ -86,6 +96,9 @@ def filter_lines(lines, min_distance):
keep_line
=
True
for
line2
in
filtered_lines
:
x3
,
y3
,
x4
,
y4
=
line2
[
0
]
line2_x_avg
=
(
x3
+
x4
)
/
2
line2_y_avg
=
(
y3
+
y4
)
/
2
...
...
This diff is collapsed.
Click to expand it.
game.py
+
21
−
7
View file @
059830cd
...
...
@@ -14,16 +14,28 @@ import time
from
picamera2
import
Picamera2
,
Preview
class
ChessGame
:
def
__init__
(
self
,
difficulty
,
show_cv
,
show_cam
,
test_img
=
None
):
def
__init__
(
self
,
difficulty
,
show_cv
,
show_cam
,
calibrate
,
test_img
=
None
):
self
.
board
=
chess
.
Board
()
self
.
difficulty
=
difficulty
self
.
show_cv
=
show_cv
self
.
show_cam
=
show_cam
self
.
test_img
=
test_img
self
.
calibrate
=
calibrate
self
.
img_idx
=
0
self
.
left_cut
=
30
self
.
right_cut
=
290
self
.
top_cut
=
0
self
.
bottom_cut
=
290
self
.
picam2
=
Picamera2
()
def
calibrate_cam
():
pass
def
start_game
(
self
):
if
(
self
.
calibrate
):
self
.
calibrate_cam
()
print
(
f
"
Starting chess game (difficulty:
{
self
.
difficulty
}
)
"
)
# TODO - call initialize board in board_detector, initialize colors for color analysis,
...
...
@@ -32,7 +44,7 @@ class ChessGame:
init_show_cv
(
self
.
show_cv
)
preview_config
=
self
.
picam2
.
create_preview_configuration
(
main
=
{
"
size
"
:
(
2
000
,
2000
)})
preview_config
=
self
.
picam2
.
create_preview_configuration
(
main
=
{
"
size
"
:
(
2
464
,
2464
)})
self
.
picam2
.
configure
(
preview_config
)
if
(
self
.
show_cam
):
self
.
picam2
.
start_preview
(
Preview
.
QTGL
)
...
...
@@ -50,15 +62,16 @@ class ChessGame:
# orig_img = cv2.imread('test_images/board1.jpg') # TODO - CHANGE TO MAKE IT RECEIVE INPUT FROM CAMERA
orig_img
=
self
.
take_pic
()
h
,
w
,
c
=
orig_img
.
shape
cropped_img
=
orig_img
[
0
:
h
-
100
,
0
:
w
-
100
]
# w_cut = 30
# h_cut = 100
cropped_img
=
orig_img
[
self
.
top_cut
:
h
-
self
.
bottom_cut
,
self
.
left_cut
:
w
-
self
.
right_cut
]
# cropped_img = orig_img
img
=
cv2
.
resize
(
cropped_img
,
(
512
,
512
))
# img = orig_img
if
(
self
.
show_cv
):
# cv2.imshow('Original Image Before Processing', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
display_img
([
orig_img
,
img
])
# display_img([img])
warped_img
,
sorted_warped_points
=
find_board
(
img
)
...
...
@@ -108,8 +121,9 @@ if __name__ == "__main__":
default
=
"
medium
"
,
help
=
"
Chess AI difficulty (how far it looks ahead)
"
)
parser
.
add_argument
(
"
--show_cv
"
,
action
=
"
store_true
"
,
help
=
"
Show opencv images as processing occurs during game
"
)
parser
.
add_argument
(
"
--show_cam
"
,
action
=
"
store_true
"
,
help
=
"
Show persistent camera view
"
)
parser
.
add_argument
(
"
--calibrate
"
,
action
=
"
store_true
"
,
help
=
"
Loop showing camera with cutoff lines first, then start game
"
)
parser
.
add_argument
(
"
--test_img
"
,
help
=
"
If specified, will use said image in test_images folder rather than camera input
"
)
args
=
parser
.
parse_args
()
game
=
ChessGame
(
args
.
difficulty
,
args
.
show_cv
,
args
.
show_cam
,
args
.
test_img
)
game
=
ChessGame
(
args
.
difficulty
,
args
.
show_cv
,
args
.
show_cam
,
args
.
calibrate
,
args
.
test_img
)
game
.
start_game
()
\ No newline at end of file
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