-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolver.py
executable file
·105 lines (85 loc) · 2.73 KB
/
solver.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
import pandas as pd
from random import randint
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import os
import random
import sys
import cv2
f1 = open('testingdata.txt','w')
outfile = open('sudoku_input.txt', 'w')
filename = sys.argv[-1]
img = cv2.imread(filename,0) #load in grascale
img = cv2.GaussianBlur(img, (5, 5), 0)
crop = cv2.resize(img, (252, 252))
(thresh, finalimg) = cv2.threshold(crop, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) #convert it in binary image
height, width = finalimg.shape[:2]
for i in range(0,height):
for j in range(0,width):
finalimg[i,j] = 255 - finalimg.item(i,j)
valid = np.zeros((28,28))
for row_number in range(0,9):
for col_number in range(0,9):
cellimg = np.zeros((28,28)) #to store the 28 * 28 image for debugging purpose
#running the loop to take in the pixel values for each cells. 2 pixels padding is done to ignore the cell borders.
count_pixel=0
for i in range(2,26):
for j in range(2,26):
pixel_value = finalimg.item(row_number*28+i,col_number*28+j)
if (pixel_value == 255):
pixel_value = 1
count_pixel = count_pixel+1
cellimg[i,j] = pixel_value
if (count_pixel != 0):
valid[row_number,col_number]=1
for i in range(0,28):
for j in range(0,28):
f1.write(str(int(cellimg.item(i,j)))+" ");
f1.write('\n')
f1.close()
with open("trainingdata.txt") as textFile:
features = [line.split() for line in textFile]
with open("traininglable.txt") as textFile:
tagg = [line.split() for line in textFile]
tagi=np.array(tagg)
tag=np.ravel(tagi)
with open("testingdata.txt") as textFile:
test = [line.split() for line in textFile]
clf = KNeighborsClassifier(n_neighbors=2,weights='distance')
clf.fit(features, tag)
preds = clf.predict(test)
k = 0
for i in range(0,9):
for j in range(0,9):
if (valid[i][j] == 1):
outfile.write(str(preds[k])+" ")
else:
outfile.write("0 ")
k = k+1
outfile.write('\n')
outfile.close()
os.system("./sudoku_solver")
with open("sudoku_output.txt") as ansFile:
ans = [line.split() for line in ansFile]
check = int(ans[0][0])
if(check == -1):
print "****************Sudoku could not be read. Try for another Image.****************\n";
os.system("rm testingdata.txt")
os.system("rm sudoku_input.txt")
os.system("rm sudoku_output.txt")
exit()
imgout = cv2.imread(filename)
font = cv2.FONT_HERSHEY_SCRIPT_SIMPLEX
for i in range(0,9):
for j in range(0,9):
if (valid[i][j] == 1):
vy=1
else:
cv2.putText(imgout,str(ans[i][j]),((j)*40+13,(i+1)*40-13), font, 0.7,(0,0,0),2)
#cv2.putText(imgout,'0',(28,28), font, 1,(0,0,0),2)
cv2.imshow("Result",imgout)
cv2.waitKey(0)
cv2.destroyAllWindows()
os.system("rm testingdata.txt")
os.system("rm sudoku_input.txt")
os.system("rm sudoku_output.txt")