-
Notifications
You must be signed in to change notification settings - Fork 9
/
functions.py
170 lines (140 loc) · 5.94 KB
/
functions.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
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
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 6 21:00:17 2015
@author: croeer
"""
import cv2
import numpy as np
from packages import sunfish
import imutils
import config
def getPositionsByTemplateMatching( filename, img, threshold ):
if (config.verbosity):
print("matching", filename)
ret=[]
template = cv2.imread(filename,0)
w, h = template.shape[::-1]
# templates are taken for 134x134 field size so image must be scaled accordingly
img_scaled = imutils.resize(img, width = 8*134)
#cv2.imwrite(filename + 'scaled.png', img_scaled)
scalefactor = img.shape[1] / float(img_scaled.shape[1])
boximg = np.copy(img_scaled)
res = cv2.matchTemplate(img_scaled,template,cv2.TM_CCOEFF_NORMED)
#threshold = 0.4
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
addPt = True
# check for similar points in close range
aktPt = (pt[0] + w/2, pt[1] + h/2)
for chkPt in ret:
#print 'Checking point ' , aktPt[0], aktPt[1], ' against ', chkPt[0], chkPt[1], ': ', (chkPt[0]-aktPt[0])**2+(chkPt[1]-aktPt[1])**2
if (chkPt[0]-aktPt[0])**2+(chkPt[1]-aktPt[1])**2 < 100:
addPt = False
break
if addPt:
ret.append( aktPt )
cv2.rectangle(boximg, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
if (config.verbosity):
cv2.imwrite(filename + '.png', boximg)
return scalefactor, ret
def getBoard( img ):
ret,thresh = cv2.threshold(img,127,255,0)
if (config.verbosity):
cv2.imwrite('thresh.png', thresh)
contours, _ = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#print 'Found %d contours' % (len(contours))
# find board
max_area = 0
best_cnt = None
for cnt in contours:
area = cv2.contourArea(cnt)
if area > 1000:
if area > max_area:
max_area = area
best_cnt = cnt
mask = np.zeros((img.shape),np.uint8)
cv2.drawContours(mask,[best_cnt],0,255,-1)
cv2.drawContours(mask,[best_cnt],0,0,2)
res = cv2.bitwise_and(img,mask)
if (config.verbosity):
cv2.imwrite('detected.png',res)
return res, cv2.boundingRect(best_cnt)
def transformCoordinatesToField( board, scalefactor, points ):
#print "transforming", points, scalefactor
binSizeX = board[2]//8
binSizeY = board[3]//8
#print binSizeX, binSizeY
ret = []
for point in points:
#print "point", point
pointInBoardCoordinates = [(int)(x*scalefactor) for x in point] #(point[0]-board[0],point[1]-board[1])
#print "maps to", pointInBoardCoordinates
binX = -(-pointInBoardCoordinates[0]//binSizeX)
binY = -(-pointInBoardCoordinates[1]//binSizeY)
#print binX, binY
ret.append((binX,9-binY))
#return (chr(ord('a')+(binX-1)),chr(ord('1')+(9-binY-1)))
#print chr(ord('a')+(binX-1)) + chr(ord('1')+(9-binY-1)) # human readable
return ret
def pointInGlobalCoordinates( board, field ):
p0,p1 = (board[0],board[1])
rank, fil = divmod(field - sunfish.A1, 10)
binSizeX = board[2]//8
binSizeY = board[3]//8
p0 = p0 + binSizeX*fil + binSizeX/2
p1 = p1 + binSizeY*(8+rank-1) + binSizeY/2
if (config.verbosity):
print(f"{field}: {p0},{p1}")
return (int(p0),int(p1))
def replaceFig( key, pieces, boardL ):
for p in pieces:
pos = 63+p[0]-8*p[1]
#print key, p, pos
boardL[pos]=key
return
def setupBoard( board, img ):
f, wp = getPositionsByTemplateMatching('templates/white_pawn.png', img, 0.4)
whitePawns = transformCoordinatesToField(board, f, wp)
f, wr = getPositionsByTemplateMatching('templates/white_rook.png', img, 0.5)
whiteRooks = transformCoordinatesToField(board, f, wr)
f, wk = getPositionsByTemplateMatching('templates/white_knight.png', img, 0.6)
whiteKnights = transformCoordinatesToField(board, f, wk)
f, wb = getPositionsByTemplateMatching('templates/white_bishop.png', img, 0.5)
whiteBishops = transformCoordinatesToField(board, f, wb)
f, wkg = getPositionsByTemplateMatching('templates/white_king.png', img, 0.6)
whiteKing = transformCoordinatesToField(board, f, wkg)
f, wq = getPositionsByTemplateMatching('templates/white_queen.png', img, 0.5)
whiteQueens = transformCoordinatesToField(board, f, wq)
f, bp = getPositionsByTemplateMatching('templates/black_pawn.png', img, 0.8)
blackPawns = transformCoordinatesToField(board, f, bp)
f, br = getPositionsByTemplateMatching('templates/black_rook.png', img, 0.6)
blackRooks = transformCoordinatesToField(board, f, br)
f, bk = getPositionsByTemplateMatching('templates/black_knight.png', img, 0.6)
blackKnights = transformCoordinatesToField(board, f, bk)
f, bb = getPositionsByTemplateMatching('templates/black_bishop.png', img, 0.6)
blackBishops = transformCoordinatesToField(board, f, bb)
f, bkg = getPositionsByTemplateMatching('templates/black_king.png', img, 0.6)
blackKing = transformCoordinatesToField(board, f, bkg)
f, bq = getPositionsByTemplateMatching('templates/black_queen.png', img, 0.6)
blackQueens = transformCoordinatesToField(board, f, bq)
boardL = ['o']*64
for k,v in {'P':whitePawns,'R':whiteRooks,'N':whiteKnights,'B':whiteBishops,'K':whiteKing,'Q':whiteQueens}.items():
replaceFig(k,v,boardL)
for k,v in {'p':blackPawns,'r':blackRooks,'n':blackKnights,'b':blackBishops,'k':blackKing,'q':blackQueens}.items():
replaceFig(k,v,boardL)
fen = ''
for k in range(64):
fen = fen + boardL[k]
if (k+1) % 8 == 0 and k < 63:
fen = fen + '/'
for k in range(8,0,-1):
fen = fen.replace('o'*k, str(k))
return fen, boardL
def reverse_fen(fen):
arr = fen.split('/')
reversedFen = ''
for i in reversed(arr):
if reversedFen != '':
reversedFen += '/'
reversedFen += i[::-1].swapcase()
return reversedFen