-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictMnist.py
194 lines (149 loc) · 6.44 KB
/
predictMnist.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
# Importing the Keras libraries and packages
import logging, os
logging.disable(logging.WARNING)
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
from keras.models import load_model
model = load_model('mnist_keras_cnn_model.h5')
from PIL import Image
import numpy as np
import cv2
def x_cord_contour(contour):
# This function take a contour from findContours
# it then outputs the x centroid coordinates
if cv2.contourArea(contour) > 10:
M = cv2.moments(contour)
return (int(M['m10'] / M['m00']))
else:
return -1
def makeSquare(not_square):
# This functions taken an image and makes the different square
# It adds black pixels as the padding where needed
BLACK = [0, 0, 0]
img_dim = not_square.shape
height = img_dim[0]
width = img_dim[1]
# print("Height = ", height, "Width = ", width)
if(height == width):
square = not_square
return square
else:
doublesize = cv2.resize(not_square, (2*width, 2*height), interpolation = cv2.INTER_CUBIC)
height = height * 2
width = width * 2
# print("New Height = ", height, width = ", width)
if(height > width):
pad = (height - width) // 2
# print("Padding = ", pad)
doublesize_square = cv2.copyMakeBorder(doublesize, 0, 0, pad, pad, cv2.BORDER_CONSTANT, value=BLACK)
else:
pad = ( width - height ) // 2
# print("Padding = ", pad)
doublesize_square = cv2.copyMakeBorder(doublesize, pad, pad, 0, 0, cv2.BORDER_CONSTANT, value = BLACK)
doublesize_square_dim = doublesize_square.shape
# print("Sq Height = ", doublesize_square_dim[0], "sq Width = ", doublesize_square_dim[1])
return doublesize_square
def verifyOverlap(l1, r1, l2, r2):
# If one rectangle is on left side of other
if(l1[0] > r2[0] or l2[0] > r1[0]):
return False
# If one rectangle is above other
if(l1[1] < r2[1] or l2[1] < r1[1]):
return False
return True
def get_contour_precedence(contour, cols):
tolerance_factor = 100
origin = cv2.boundingRect(contour)
return ((origin[1] // tolerance_factor) * tolerance_factor) * cols + origin[0]
#-----------------------------------------------------------------------------------------
image = cv2.imread('./input/004.jpeg')
#image = cv2.imread('/mnt/e/miniproject/myPro/input/test_img.png')
#image = cv2.imread('E:/miniproject/myPro/input/numbers.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#print(gray.shape)
if(gray.shape == (28, 28)):
y_pred = model.predict(gray.reshape(1, 28, 28, 1))
number = str(int(float(np.where(y_pred == np.amax(y_pred))[1][0])))
print(number)
__import__('sys').exit(1)
ret, gray1 = cv2.threshold(gray, 135, 255, cv2.THRESH_BINARY_INV)
#gray1 = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
#cv2.imshow("Threshhold", gray1)
# Blur image then find edges using Canny
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
blurred1 = cv2.GaussianBlur(gray1, (5, 5), 0)
blur1 = cv2.GaussianBlur(gray1, (5, 5), 0)
edged = cv2.Canny(blurred1, 30, 150)
#cv2.imshow("Edges", edged)
# Find Contours
contours, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#print(hierarchy)
# Sort out contours left to right by using thier x coordinate
#contours = sorted(contours, key = cv2.contourArea, reverse = True)
#contours = sorted(contours, key =lambda x:get_contour_precedence(x, blur1.shape[1]), reverse = False)
contours = sorted(contours, key = x_cord_contour, reverse = False)
w1 = [cv2.boundingRect(c)[2] for c in contours]
w_avg = (( sum(w1)/len(w1) ) + (max(w1)-min(w1))/2 ) /1.5
h1 = [cv2.boundingRect(c)[3] for c in contours]
h_avg = (( sum(h1)/len(h1) ) + (max(h1)-min(h1))/2 ) /1.5
# Create empty array to store entire number
full_number = []
elements = []
# loop over the contours
for c in contours:
# compute the bounding box for the rectangle
(x, y, w, h) = cv2.boundingRect(c)
#print(elements)
if(not elements):
elements.append((x, y, x+w, y+h))
elif(any([1 for i in elements if(verifyOverlap( (i[0], i[1]), (i[2], i[3]), (x, y), (x+w, y+h) ))])):
continue
else:
elements.append((x, y, x+w, y+h))
if(w >=w_avg and h >=h_avg):
roi = blurred1[y:y+h, x:x+w]
#ret, roi = cv2.threshold(roi, 127, 255, cv2.THRESH_BINARY_INV)
cv2.rectangle(blur1, (x, y), (x+w, y+h), (255, 255, 255), 2)
#blur1 = cv2.putText(blur1, number, cv2.boundingRect(contours[i])[:2], cv2.FONT_HERSHEY_COMPLEX, 1, [125])
#roi = cv2.adaptiveThreshold(roi,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
while (True):
cv2.imshow("Image", roi)
k = cv2.waitKey(50) & 0xFF
if k == ord('q'):
break
squared = makeSquare(roi)
#print(squared.shape)
final = cv2.resize(squared, (28, 28), interpolation = cv2.INTER_AREA)
#print(final.shape)
y_pred = model.predict(final.reshape(1, 28, 28, 1))
print(y_pred)
number = str(int(float(np.where(y_pred == np.amax(y_pred))[1][0])))
print(number)
full_number.append(number)
else:
roi = blurred1[y:y+h, x:x+w]
#ret, roi = cv2.threshold(roi, 127, 255, cv2.THRESH_BINARY_INV)
#roi = cv2.adaptiveThreshold(roi,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
squared = makeSquare(roi)
#print(squared.shape)
final = cv2.resize(squared, (28, 28), interpolation = cv2.INTER_AREA)
#print(final.shape)
y_pred = model.predict(final.reshape(1, 28, 28, 1))
number = str(int(float(np.where(y_pred == np.amax(y_pred))[1][0])))
#print(number)
if(number==7 or number ==1):
cv2.rectangle(blur, (x, y), (x+w, y+h), (255, 255, 255), 2)
print(y_pred)
while (True):
cv2.imshow("Image_ding_dong", roi)
k = cv2.waitKey(50) & 0xFF
if k == ord('q'):
break
full_number.append(number)
print("The number is : " + ''.join(full_number))
while (True):
cv2.imshow("Image", blur1)
k = cv2.waitKey(50) & 0xFF
if k == ord('q'):
break
cv2.destroyAllWindows()