-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku.py
More file actions
131 lines (113 loc) · 4.76 KB
/
sudoku.py
File metadata and controls
131 lines (113 loc) · 4.76 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
import cv2
import numpy as np
import operator
from keras.models import load_model
from keras.models import model_from_json
import sudoku_solver as sol
classifier = load_model(r"Sudoku-Solver-AI-master\digit_model.h5")
marge = 4
case = 28 + 2 * marge
taille_grille = 9 * case
cap = cv2.VideoCapture(2)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
flag = 0
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (1080, 620))
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
thresh = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 9, 2)
contours, hierarchy = cv2.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contour_grille = None
maxArea = 0
for c in contours:
area = cv2.contourArea(c)
if area > 25000:
peri = cv2.arcLength(c, True)
polygone = cv2.approxPolyDP(c, 0.01 * peri, True)
if area > maxArea and len(polygone) == 4:
contour_grille = polygone
maxArea = area
if contour_grille is not None:
cv2.drawContours(frame, [contour_grille], 0, (0, 255, 0), 2)
points = np.vstack(contour_grille).squeeze()
points = sorted(points, key=operator.itemgetter(1))
if points[0][0] < points[1][0]:
if points[3][0] < points[2][0]:
pts1 = np.float32([points[0], points[1], points[3], points[2]])
else:
pts1 = np.float32([points[0], points[1], points[2], points[3]])
else:
if points[3][0] < points[2][0]:
pts1 = np.float32([points[1], points[0], points[3], points[2]])
else:
pts1 = np.float32([points[1], points[0], points[2], points[3]])
pts2 = np.float32([[0, 0], [taille_grille, 0], [0, taille_grille], [
taille_grille, taille_grille]])
M = cv2.getPerspectiveTransform(pts1, pts2)
grille = cv2.warpPerspective(frame, M, (taille_grille, taille_grille))
grille = cv2.cvtColor(grille, cv2.COLOR_BGR2GRAY)
grille = cv2.adaptiveThreshold(
grille, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 7, 3)
cv2.imshow("grille", grille)
if flag == 0:
grille_txt = []
for y in range(9):
ligne = ""
for x in range(9):
y2min = y * case + marge
y2max = (y + 1) * case - marge
x2min = x * case + marge
x2max = (x + 1) * case - marge
cv2.imwrite("mat" + str(y) + str(x) + ".png",
grille[y2min:y2max, x2min:x2max])
img = grille[y2min:y2max, x2min:x2max]
x = img.reshape(1, 28, 28, 1)
if x.sum() > 10000:
prediction = classifier.predict_classes(x)
ligne += "{:d}".format(prediction[0])
else:
ligne += "{:d}".format(0)
grille_txt.append(ligne)
print(grille_txt)
result = sol.sudoku(grille_txt)
print("Resultat:", result)
if result is not None:
flag = 1
fond = np.zeros(
shape=(taille_grille, taille_grille, 3), dtype=np.float32)
for y in range(len(result)):
for x in range(len(result[y])):
if grille_txt[y][x] == "0":
cv2.putText(fond, "{:d}".format(result[y][x]), ((
x) * case + marge + 3, (y + 1) * case - marge - 3), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 0.9, (0, 0, 255), 1)
M = cv2.getPerspectiveTransform(pts2, pts1)
h, w, c = frame.shape
fondP = cv2.warpPerspective(fond, M, (w, h))
img2gray = cv2.cvtColor(fondP, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask = mask.astype('uint8')
mask_inv = cv2.bitwise_not(mask)
img1_bg = cv2.bitwise_and(frame, frame, mask=mask_inv)
img2_fg = cv2.bitwise_and(fondP, fondP, mask=mask).astype('uint8')
dst = cv2.add(img1_bg, img2_fg)
dst = cv2.resize(dst, (1080, 620))
cv2.imshow("frame", dst)
out.write(dst)
else:
frame = cv2.resize(frame, (1080, 620))
cv2.imshow("frame", frame)
out.write(frame)
else:
flag = 0
frame = cv2.resize(frame, (1080, 620))
cv2.imshow("frame", frame)
out.write(frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
out.release()
cap.release()
cv2.destroyAllWindows()