-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdetectionScript.py
73 lines (64 loc) · 2.81 KB
/
detectionScript.py
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
from chessboard import Chessboard
import cv2
from boardDetection import ChessboardDetector
from chessboard import Chessboard
import glob
import numpy as np
class chess_digitizer:
def visualize(self, detect, board, corners):
board_img = board.curr_board_to_img()
# draw board outline
for i in range(len(corners)):
cv2.line(detect.img_rgb, tuple(corners[i]), tuple(
corners[(i+1) % len(corners)]), 255, 1, cv2.LINE_AA)
# draw score
try:
if board.score > 0:
cv2.rectangle(detect.img_rgb, (256, 10), (256 -
min(int(156/12*board.score), 150), 30), (255, 255, 255), -1)
else:
cv2.rectangle(detect.img_rgb, (256, 10), (256 -
max(int(156/12*board.score), -150), 30), (0, 0, 0), -1)
except:
sc = int(board.score[1:])
if sc > 0:
cv2.rectangle(detect.img_rgb, (256, 10), (100, 30), (255, 255, 255), -1)
elif sc == 0:
pass
else:
cv2.rectangle(detect.img_rgb, (256, 10), (412, 30), (0, 0, 0), -1)
cv2.rectangle(detect.img_rgb, (100, 0), (412, 40), (200, 200, 200), 2)
cv2.line(detect.img_rgb, (256, 0), (256, 40), (200, 200, 200), 3)
cv2.putText(detect.img_rgb, str(board.score), (225, 67),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# draw imgs
tp_img = np.hstack((cv2.cvtColor(detect.img_rgb, cv2.COLOR_BGR2RGB),board_img[0:384,0:384,0:3]))
cv2.imshow("Board", tp_img)
cv2.waitKey(0)
def main(self):
detect = ChessboardDetector(
"models/detection", "models/classification.h5")
board = Chessboard()
# cap = cv2.VideoCapture('input_imgs/20201209_111609.mp4')
filenames = glob.glob("input_imgs/*")
filenames = sorted(filenames)
for file in filenames:
img = cv2.imread(file)
corners = detect.predict_board_corners(img)
if len(corners) == 4:
predictions = detect.predictBoard(img, corners)
board.predictions_to_move(predictions)
self.visualize(detect, board, corners)
# while True:
# ret, img = cap.read()
# # ret, img = cap.read()
# img = img[:,240:1680,:]
# corners = detect.predict_board_corners(img)
# if len(corners) == 4:
# predictions = detect.predictBoard(img, corners)
# # if board.predictions_to_move(predictions):
# board.predictions_to_move(predictions)
# self.visualize(detect, board, corners)
if __name__ == "__main__":
cd = chess_digitizer()
cd.main()