-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard_detection.py
More file actions
273 lines (218 loc) · 7.44 KB
/
board_detection.py
File metadata and controls
273 lines (218 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from chess import Board, svg, engine, parse_square
from argparse import Namespace, ArgumentParser
from json import load as load_json
from cairosvg import svg2png
from io import BytesIO
from PIL import Image
import numpy as np
import cv2 as cv
from helper_functions import segment_lines, find_intersection, cluster_points, \
get_squares_coords_dict, detections_to_fen, get_fen_piece_name
from board_selector import roi_selector
from yolo import Yolo
def parse_args() -> Namespace:
"""
Parses command line arguments for the board detection script.
Returns:
argparse.Namespace: Parsed command line arguments.
"""
parser = ArgumentParser(
description='Identifies a chess position from an image.'
)
parser.add_argument(
'board_image', type=str,
help='Path to the image file to be processed.'
)
parser.add_argument(
'--config-path', type=str,
default='~/repos/chesspy/yolo/configs/yolov4_tiny_chess.cfg',
help='Path to the yolo configuration file.'
)
parser.add_argument(
'--weights-path', type=str,
default='~/repos/chesspy/yolo/weights/yolov4_tiny_chess_last.weights',
help='Path to the yolo weights file.'
)
parser.add_argument(
'--meta-path', type=str,
default='~/repos/chesspy/yolo/data/chess_obj.data',
help='Path to the yolo .data file.'
)
parser.add_argument(
'--select-board', action='store_true',
help='Interface for selecting the board outline in the video.'
)
parser.add_argument(
'--show-best-move', action='store_true',
help='Highlights best moves in the 2D board.'
)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
board = cv.imread(args.board_image)
board_height, board_width, _ = board.shape
if args.select_board:
roi_selector(board)
try:
board_coords = np.array(
load_json(open('./assets/board_pos.json'))['board_coords'],
dtype='float32'
)
except FileNotFoundError:
raise Exception('Board selection configuration not found! Run the script with the `--select-board` flag.')
width = 400
height = 400
dest_coords = np.array([
[0, 0],
[width - 1, 0],
[width - 1, height - 1],
[0, height - 1]
], dtype='float32')
perspective_matrix = cv.getPerspectiveTransform(board_coords, dest_coords)
mask = np.zeros(board.shape, np.uint8)
points = np.array(board_coords, np.int32).reshape((-1, 1, 2))
mask = cv.polylines(mask, [points], True, (255, 255, 255), 2)
mask2 = cv.fillPoly(mask.copy(), [points], (255, 255, 255))
roi = cv.bitwise_and(mask2, board)
roi_warped = cv.warpPerspective(roi, perspective_matrix, (width, height))
roi_warped_gray = cv.cvtColor(roi_warped, cv.COLOR_BGR2GRAY)
canny_edges = cv.Canny(roi_warped_gray, 100, 150)
dilated = cv.dilate(canny_edges, np.ones((1, 1), dtype=np.uint8))
lines = cv.HoughLinesP(
dilated,
rho=1, theta=np.pi/180,
threshold=70,
minLineLength=40, maxLineGap=70
)
# Segment the lines
delta = 10
h_lines, v_lines = segment_lines(lines, delta)
# draw the segmented lines
hough_img = roi_warped.copy()
for line in h_lines:
for x1, y1, x2, y2 in line:
# Horizontal lines are red
cv.line(
hough_img,
(x1, y1), (x2, y2),
color=(0, 0, 255), thickness=1
)
for line in v_lines:
for x1, y1, x2, y2 in line:
# Vertical lines are blue
cv.line(
hough_img,
(x1, y1), (x2, y2),
color=(255, 0, 0), thickness=1
)
# find the line intersection points
Px = []
Py = []
for h_line in h_lines:
for v_line in v_lines:
px, py = find_intersection(h_line, v_line)
Px.append(px)
Py.append(py)
# Use clustering to find the centers of the data clusters
P = np.float32(np.column_stack((Px, Py)))
centers = cluster_points(P, 81)
centers = centers[np.lexsort((centers[:, 0], centers[:, 1]))]
# Draw the center of the clusters
for idx, (cx, cy) in enumerate(centers):
cx = np.round(cx).astype(int)
cy = np.round(cy).astype(int)
cv.circle(roi_warped, (cx, cy), radius=3, color=(255, 0, 255), thickness=-1)
squares = get_squares_coords_dict(centers)
# Draw squares and square names
for square_name, (pt1, pt2) in squares.items():
x1, y1 = pt1
x2, y2 = pt2
cv.circle(roi_warped, pt1, 2, (255, 255, 255), -1)
cv.rectangle(
roi_warped,
(x1 + 6, y1 + 6),
(x2 - 6, y2 - 6),
(107, 104, 255), 1
)
x = round((x1 + x2) / 2)
y = round((y1 + y2) / 2)
cv.putText(
roi_warped,
square_name,
(x - 10, y),
cv.FONT_HERSHEY_SIMPLEX,
0.5, (255, 255, 255)
)
detector = Yolo(args)
detections = detector.detect(board)
# Draw detections
for detection in detections:
x, y, _, _ = detection['coords']
# Projection the points
x_p, y_p = cv.perspectiveTransform(
np.array([[[x, y]]], dtype=np.float64),
perspective_matrix
).reshape(2,).astype(int)
cv.circle(
roi_warped,
(x_p, y_p),
5,
detector.class_colors[detection['class_name']],
-1
)
cv.putText(
roi_warped,
get_fen_piece_name(detection['class_name']),
(x_p, y_p - 3),
cv.FONT_HERSHEY_SIMPLEX,
0.7,
(30, 189, 133),
2
)
fen = detections_to_fen(detections, perspective_matrix, squares)
if args.show_best_move:
stockfish = engine.SimpleEngine.popen_uci(r'../assets/stockfish_16.1')
result = str(stockfish.play(Board(fen), engine.Limit(time=0.1)).move)
stockfish.quit()
board_2d_data = Image.open(
BytesIO(svg2png(
bytestring=svg.board(
Board(fen),
arrows=[svg.Arrow(
parse_square(result[:2]),
parse_square(result[-2:])
)]
),
output_width=board_height, output_height=board_height
))
).convert('RGBA')
else:
board_2d_data = Image.open(
BytesIO(svg2png(
bytestring=svg.board(Board(fen)),
output_width=board_height, output_height=board_height
))
).convert('RGBA')
board_2d = cv.cvtColor(
np.array(board_2d_data),
cv.COLOR_RGBA2BGR
)
detections_img = np.concatenate(
(
detector.draw_detection_boxes(board, detections),
board_2d
),
axis=1
)
result_win = 'Result_1'
cv.namedWindow(result_win, cv.WINDOW_KEEPRATIO)
cv.moveWindow(result_win, 80, 60)
cv.resizeWindow(result_win, 1351, 524)
result_win_2 = 'Result_2'
cv.namedWindow(result_win_2, cv.WINDOW_KEEPRATIO)
cv.moveWindow(result_win_2, 1435, 60)
cv.resizeWindow(result_win_2, 636, 629)
cv.imshow(result_win, detections_img)
cv.imshow(result_win_2, roi_warped)
cv.waitKey(0)
cv.destroyAllWindows()