-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
211 lines (143 loc) · 4.8 KB
/
main.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
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
import cv2
import numpy as np
from ocr.ocr import *
from astar import *
from collections import Counter
capture = cv2.VideoCapture(2)
# define the list of boundaries
boundaries = [
([17, 15, 100], [50, 56, 200]),
([86, 31, 10], [225, 100, 60]),
([25, 146, 190], [62, 174, 250]),
([103, 86, 65], [145, 133, 128])
]
kernel = np.ones((3,3), np.uint8)
def four_point_transform(image, pts,rect):
(tl, tr, br, bl) = pts
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
def paintCorners(image,corners):
for i in corners:
x = i[0]
y = i[1]
cv2.circle(image,(x,y),10,(0, 0, 255),2)
def sortCorners(corners):
corners = [ i[0].tolist() for i in sorted(corners, key=cv2.contourArea, reverse=True)]
corners = np.asarray(corners)
rect = np.zeros((4, 2), dtype = "float32")
s = corners.sum(axis = 1)
rect[0] = corners[np.argmin(s)]
rect[2] = corners[np.argmax(s)]
diff = np.diff(corners, axis = 1)
rect[1] = corners[np.argmin(diff)]
rect[3] = corners[np.argmax(diff)]
return rect
def splitBoard(imageWraped):
print(imageWraped.shape)
w=imageWraped.shape[0]
h=imageWraped.shape[1]
d_w = int(w/3)
d_h = int(h/3)
fig_1 = imageWraped[0:d_h, 0:d_w]
fig_2 = imageWraped[0:d_h, d_w:d_w*2]
fig_3 = imageWraped[0:d_h, d_w*2:w]
fig_4 = imageWraped[d_h:d_h*2, 0:d_w]
fig_5 = imageWraped[d_h:d_h*2, d_w:d_w*2]
fig_6 = imageWraped[d_h:d_h*2, d_w*2:w]
fig_7 = imageWraped[h-d_h:h, 0:d_w]
fig_8 = imageWraped[h-d_h:h, d_w:d_w*2]
fig_9 = imageWraped[h-d_h:h, d_w*2:w]
return [fig_1,fig_2,fig_3,fig_4,fig_5,fig_6,fig_7,fig_8,fig_9]
def most_frequent(List):
occurence_count = Counter(List)
return occurence_count.most_common(1)[0][0]
def checkIsCorrect(array):
array=array.sort()
for i in range(0,9):
print(i,array[i])
if array[i]!=i:
return False
return True
def compare(A,B):
for i in range(len(A)):
if A[i] != B[i]:
print( A[i] ,B[i])
return False
return True
count_tolerance = 0
threshold_tolerance = 1
cells_buffer = [[],[],[],[],[],[],[],[],[]]
best_numbers = [0,0,0,0,0,0,0,0,0]
temp_best = [0,0,0,0,0,0,0,0,0]
while(True):
ret, image = capture.read()
lower,upper=boundaries[1]
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
mask = cv2.inRange(image, lower, upper)
output = cv2.bitwise_and(image, image, mask = mask)
#cv2.imshow("mask", mask)
#cv2.imshow("bitwise_and", output)
output = cv2.cvtColor(output, cv2.COLOR_HSV2BGR)
#cv2.imshow("hsv",output )
gray = cv2.cvtColor(output,cv2.COLOR_BGR2GRAY)
bi = cv2.bilateralFilter(mask, 5, 75, 75)
img_dilation = cv2.dilate(bi, kernel, iterations=15)
#cv2.imshow("dilatation",img_dilation )
img_erosion = cv2.erode(img_dilation, kernel, iterations=15)
cv2.imshow("erosion", img_erosion )
wrapedImage = image
try:
corners = cv2.goodFeaturesToTrack(img_erosion,50,0.3,10)
corners = np.float32(corners)
if len(corners) == 4:
corners=sortCorners(corners)
wrapedImage=four_point_transform(image,corners,corners)
pieces = splitBoard(wrapedImage)
numbers=reconocer(pieces)
"""
if count_tolerance < threshold_tolerance :
for it2 in range(0,9):
cells_buffer[it2].append(numbers[it2])
count_tolerance+=1
else:
for it3 in range(0,9):
best_numbers[it3]=most_frequent(cells_buffer[it3])
cells_buffer = [[],[],[],[],[],[],[],[],[]]
count_tolerance = 0
"""
print(numbers,"Reconocimiento ")
if len(numbers) == len(set(numbers)):
if not compare(temp_best,numbers) :
print("cambio : ")
temp_best = numbers
inputAstar = ''.join([str(elem) for elem in numbers])
print(inputAstar)
resultado = astar(inputAstar)
print("res ",resultado)
if resultado == []:
print("No hay solución")
else :
print("resultado")
print(resultado[0])
paintCorners(image,corners)
cv2.imshow("wraperd", wrapedImage)
cv2.imshow("images", image)
except:
cv2.imshow("images", image)
if cv2.waitKey(1) == 27:
break
capture.release()
cv2.destroyAllWindows()