forked from murtazahassan/OpenCV-Sudoku-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utlis.py
135 lines (117 loc) · 4.48 KB
/
utlis.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
import cv2
import numpy as np
from tensorflow.keras.models import load_model
#### READ THE MODEL WEIGHTS
def intializePredectionModel():
model = load_model('Resources/myModel.h5')
return model
#### 1 - Preprocessing Image
def preProcess(img):
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # CONVERT IMAGE TO GRAY SCALE
imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # ADD GAUSSIAN BLUR
imgThreshold = cv2.adaptiveThreshold(imgBlur, 255, 1, 1, 11, 2) # APPLY ADAPTIVE THRESHOLD
return imgThreshold
#### 3 - Reorder points for Warp Perspective
def reorder(myPoints):
myPoints = myPoints.reshape((4, 2))
myPointsNew = np.zeros((4, 1, 2), dtype=np.int32)
add = myPoints.sum(1)
myPointsNew[0] = myPoints[np.argmin(add)]
myPointsNew[3] =myPoints[np.argmax(add)]
diff = np.diff(myPoints, axis=1)
myPointsNew[1] =myPoints[np.argmin(diff)]
myPointsNew[2] = myPoints[np.argmax(diff)]
return myPointsNew
#### 3 - FINDING THE BIGGEST COUNTOUR ASSUING THAT IS THE SUDUKO PUZZLE
def biggestContour(contours):
biggest = np.array([])
max_area = 0
for i in contours:
area = cv2.contourArea(i)
if area > 50:
peri = cv2.arcLength(i, True)
approx = cv2.approxPolyDP(i, 0.02 * peri, True)
if area > max_area and len(approx) == 4:
biggest = approx
max_area = area
return biggest,max_area
#### 4 - TO SPLIT THE IMAGE INTO 81 DIFFRENT IMAGES
def splitBoxes(img):
rows = np.vsplit(img,9)
boxes=[]
for r in rows:
cols= np.hsplit(r,9)
for box in cols:
boxes.append(box)
return boxes
#### 4 - GET PREDECTIONS ON ALL IMAGES
def getPredection(boxes,model):
result = []
for image in boxes:
## PREPARE IMAGE
img = np.asarray(image)
img = img[4:img.shape[0] - 4, 4:img.shape[1] -4]
img = cv2.resize(img, (28, 28))
img = img / 255
img = img.reshape(1, 28, 28, 1)
## GET PREDICTION
predictions = model.predict(img)
classIndex = model.predict_classes(img)
probabilityValue = np.amax(predictions)
## SAVE TO RESULT
if probabilityValue > 0.8:
result.append(classIndex[0])
else:
result.append(0)
return result
#### 6 - TO DISPLAY THE SOLUTION ON THE IMAGE
def displayNumbers(img,numbers,color = (0,255,0)):
secW = int(img.shape[1]/9)
secH = int(img.shape[0]/9)
for x in range (0,9):
for y in range (0,9):
if numbers[(y*9)+x] != 0 :
cv2.putText(img, str(numbers[(y*9)+x]),
(x*secW+int(secW/2)-10, int((y+0.8)*secH)), cv2.FONT_HERSHEY_COMPLEX_SMALL,
2, color, 2, cv2.LINE_AA)
return img
#### 6 - DRAW GRID TO SEE THE WARP PRESPECTIVE EFFICENCY (OPTIONAL)
def drawGrid(img):
secW = int(img.shape[1]/9)
secH = int(img.shape[0]/9)
for i in range (0,9):
pt1 = (0,secH*i)
pt2 = (img.shape[1],secH*i)
pt3 = (secW * i, 0)
pt4 = (secW*i,img.shape[0])
cv2.line(img, pt1, pt2, (255, 255, 0),2)
cv2.line(img, pt3, pt4, (255, 255, 0),2)
return img
#### 6 - TO STACK ALL THE IMAGES IN ONE WINDOW
def stackImages(imgArray,scale):
rows = len(imgArray)
cols = len(imgArray[0])
rowsAvailable = isinstance(imgArray[0], list)
width = imgArray[0][0].shape[1]
height = imgArray[0][0].shape[0]
if rowsAvailable:
for x in range ( 0, rows):
for y in range(0, cols):
imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
imageBlank = np.zeros((height, width, 3), np.uint8)
hor = [imageBlank]*rows
hor_con = [imageBlank]*rows
for x in range(0, rows):
hor[x] = np.hstack(imgArray[x])
hor_con[x] = np.concatenate(imgArray[x])
ver = np.vstack(hor)
ver_con = np.concatenate(hor)
else:
for x in range(0, rows):
imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
hor= np.hstack(imgArray)
hor_con= np.concatenate(imgArray)
ver = hor
return ver