-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_modules.py
217 lines (160 loc) · 6.61 KB
/
test_modules.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
212
213
214
215
216
217
import cv2
import numpy as np
from threshold import preprocess
from processing import find_contours, warp_image, create_grid_mask, split_squares, clean_square, recognize_digits, draw_digits_on_warped, unwarp_image
from utils import grid_line_helper, clean_square_helper, resize_square, classify_one_digit, normalize, convert_str_to_board
import matplotlib.pyplot as plt
import torch
from sudoku_solve import Sudoku_solver
classifier = torch.load('digit_model.h5',map_location ='cpu')
classifier.eval()
img = "testimg\sudoku_real_4.jpeg"
img = cv2.imread(img)
thresholded = preprocess(img)
corners_img, corners, org_img = find_contours(thresholded, img)
warped, matrix = warp_image(corner_list=corners, original= corners_img)
warped_processed = preprocess(warped)
horizontal = grid_line_helper(warped_processed, shape_location = 0)
vertical = grid_line_helper(warped_processed, shape_location=1)
def test_wrap_image(thresholded):
corners_img, corners,_ = find_contours(thresholded, thresholded)
res_img, matrix = warp_image(corners, corners_img)
res_img = cv2.resize(res_img, (600,600),interpolation = cv2.INTER_AREA)
cv2.imshow("Wraped image", res_img)
cv2.waitKey(0)
print(matrix)
print("Test wrap image success")
def test_threshold_img(original):
thresholded = preprocess(original)
cv2.imshow("img", thresholded)
cv2.waitKey(0)
print("Test threshold image success")
return thresholded
def test_find_contours(thresholded, original):
original, corner_list = find_contours(thresholded, thresholded)
cv2.imshow("img", original)
print(corner_list)
print("Test find contours success")
cv2.waitKey(0)
def test_draw_contour(thresholded, original):
contour, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(original, contour, -1, (0,255,255), 3)
original = cv2.resize(original, (600,600), interpolation= cv2.INTER_AREA)
cv2.imshow("Draw Contour", original)
cv2.waitKey(0)
def test_get_gridline(original, length = 10):
horizontal = grid_line_helper(original, shape_location =0)
vertical = grid_line_helper(original, shape_location=1)
horizontal = cv2.resize(horizontal, (600,600), interpolation= cv2.INTER_AREA)
cv2.imshow("Original", original)
cv2.waitKey(0)
cv2.imshow("Find horizontal grid line", horizontal)
cv2.waitKey(0)
vertical = cv2.resize(vertical, (600,600), interpolation= cv2.INTER_AREA)
cv2.imshow("Find vertical grid line", vertical)
cv2.waitKey(0)
print("Test get gridline success")
def test_create_grid_mask(horizontal, vertical):
get = create_grid_mask(horizontal, vertical)
get = cv2.resize(get, (600,600), interpolation= cv2.INTER_AREA)
cv2.imshow("Mask", get)
cv2.waitKey(0)
print("Test get gridline success")
return get
def test_split_square(number_img):
square = split_squares(number_img)
figure = plt.figure(figsize=(10, 10))
cols, rows = 9, 9
plt.title("Split into 81 squares")
plt.axis("off")
#Visualize result
for i in range(0, cols * rows):
figure.add_subplot(rows, cols, i+1)
plt.axis("off")
plt.imshow(square[i], cmap="gray")
plt.show()
print("Test split square success")
return square
#Test clean output image
def test_clean_square_visualize(square_list):
square_cleaned_list = []
for i in square_list:
clean_square, _ = clean_square_helper(i)
square_cleaned_list.append(clean_square)
figure = plt.figure(figsize=(10, 10))
plt.axis("off")
plt.title("Clean noises in square images")
cols, rows = 9, 9
#Visualize result
for i in range(0, cols * rows):
figure.add_subplot(rows, cols, i+1)
plt.axis("off")
plt.imshow(square_cleaned_list[i], cmap="gray")
plt.show()
print("Test clean square success")
return square_cleaned_list
def test_clean_square_count(square_list):
cleaned_list, count = clean_square(square_list)
print(count)
print("Test clean quare count success")
def test_resize_clean_square(clean_square_list):
resized_list = resize_square(clean_square_list)
for i in range(0,5):
print(resized_list[i].shape)
print("Test resize clean square success")
return resized_list
def test_classify_one_digit(model, resize_list):
digit = classify_one_digit(model, clean_square, threshold=60)
print("Test classify one digit success")
return digit
def test_recognize_digits(model, resize_list, org_image):
res_str = recognize_digits(model, resize_list, org_image)
return res_str
def test_convert_str_to_board(string):
board = convert_str_to_board(string)
# print(board)
# print(type(board))
print("Test convert str to board success")
return board
def test_sudoku_solver(board):
unsolved_board = board.copy()
sudoku = Sudoku_solver(board, 9)
# sudoku.print_board()
sudoku.solve()
# sudoku.print_board()
res_board = sudoku.board
print("Test sudoku solver success")
return res_board, unsolved_board
def test_draw_digits_warped(warped_img, solved_board, unsolved_board):
img_text, warped_img= draw_digits_on_warped(warped_img, solved_board, unsolved_board)
# img_text = cv2.resize(img_text, (600,600), interpolation=cv2.INTER_AREA)
return img_text, warped_img
def test_unwarp_image(img_src, img_dst, corner_list):
dst_img = unwarp_image(img_src, img_dst, corner_list, 0.115)
# cv2.imshow("Res", dst_img)
# cv2.waitKey(0)
return dst_img
if __name__ == "__main__":
cv2.imshow("Original image", cv2.resize(img, (600,600)))
cv2.waitKey(0)
cv2.imshow("Find corners", cv2.resize(corners_img, (600,600), cv2.INTER_AREA))
cv2.waitKey(0)
get = test_create_grid_mask(horizontal, vertical)
number = cv2.bitwise_and(cv2.resize(warped_processed, (600,600), cv2.INTER_AREA), get)
square = test_split_square(number)
square_cleaned_list = test_clean_square_visualize(square)
resized = test_resize_clean_square(square_cleaned_list)
resize_norm = normalize(resized)
res_str = test_recognize_digits(classifier, resize_norm, img)
print(res_str)
board = test_convert_str_to_board(res_str)
res_board, unsolved_board = test_sudoku_solver(board)
print(res_board)
print(unsolved_board)
_, warp_with_nums = test_draw_digits_warped(warped, res_board, unsolved_board)
cv2.imshow("Warped with numbers", cv2.resize(warp_with_nums, (600,600), cv2.INTER_AREA))
cv2.waitKey(0)
print(warp_with_nums.shape)
dst_img = test_unwarp_image(warp_with_nums, img, corners)
cv2.imshow("Final result", cv2.resize(dst_img, (800,800), cv2.INTER_AREA))
cv2.waitKey(0)