-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigit.py
314 lines (237 loc) · 10.7 KB
/
digit.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import numpy as np
from scipy.misc.pilutil import imresize
import cv2
from skimage.feature import hog
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
from shutil import copyfile
import warnings
warnings.filterwarnings("ignore")
DIGIT_WIDTH = 10
DIGIT_HEIGHT = 20
IMG_HEIGHT = 28
IMG_WIDTH = 28
CLASS_N = 10 # 0-9
def split2d(img, cell_size, flatten=True):
h, w = img.shape[:2]
sx, sy = cell_size
cells = [np.hsplit(row, w // sx) for row in np.vsplit(img, h // sy)]
cells = np.array(cells)
if flatten:
cells = cells.reshape(-1, sy, sx)
return cells
def load_digits(fn):
print('loading "%s for training" ...' % fn)
digits_img = cv2.imread(fn, 0) # 0 for cv2.IMREAD_GRAYSCALE
digits = split2d(digits_img, (DIGIT_WIDTH, DIGIT_HEIGHT))
resized_digits = []
for digit in digits:
resized_digits.append(imresize(digit, (IMG_WIDTH, IMG_HEIGHT)))
labels = np.repeat(np.arange(CLASS_N), len(digits) / CLASS_N)
return np.array(resized_digits), labels
def pixels_to_hog_20(img_array):
hog_featuresData = []
for img in img_array:
fd = hog(img, orientations=10, pixels_per_cell=(5, 5), cells_per_block=(1, 1), visualise=False)
hog_featuresData.append(fd)
hog_features = np.array(hog_featuresData, 'float64')
return np.float32(hog_features)
# define a custom model in a similar class wrapper with train and predict methods
class KNN_MODEL:
def __init__(self, k=3):
self.k = k
self.model = cv2.ml.KNearest_create()
def train(self, samples, responses):
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
def predict(self, samples):
retval, results, neigh_resp, dists = self.model.findNearest(samples, self.k)
return results.ravel()
class SVM_MODEL:
def __init__(self, num_feats, C=1, gamma=0.1):
self.model = cv2.ml.SVM_create()
self.model.setType(cv2.ml.SVM_C_SVC)
self.model.setKernel(cv2.ml.SVM_RBF)
self.model.setC(C)
self.model.setGamma(gamma)
self.features = num_feats
def train(self, samples, responses):
self.model.train(samples, cv2.ml.ROW_SAMPLE, responses)
def predict(self, samples):
results = self.model .predict(samples.reshape(-1, self.features))
return results[1].ravel()
def get_digits(contours, hierarchy):
hierarchy = hierarchy[0]
bounding_rectangles = [cv2.boundingRect(ctr) for ctr in contours]
final_bounding_rectangles = []
u, indices = np.unique(hierarchy[:, -1], return_inverse=True)
most_common_hierarchy = u[np.argmax(np.bincount(indices))]
for r, hr in zip(bounding_rectangles, hierarchy):
x, y, w, h = r
if ((w * h) > 250) and (10 <= w <= 200) and (10 <= h <= 200) and hr[3] == most_common_hierarchy:
final_bounding_rectangles.append(r)
return final_bounding_rectangles
def proc_user_img(img_file, model, image=None):
print('loading "%s for digit recognition" ...' % img_file)
im = 0
if image is None:
im = cv2.imread(img_file)
blank_image = np.zeros((im.shape[0], im.shape[1], 3), np.uint8)
blank_image.fill(255)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
plt.imshow(imgray)
kernel = np.ones((5, 5), np.uint8)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
thresh = cv2.erode(thresh, kernel, iterations=1)
thresh = cv2.dilate(thresh, kernel, iterations=1)
thresh = cv2.erode(thresh, kernel, iterations=1)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
digits_rectangles = get_digits(contours, hierarchy) # rectangles of bounding the digits in user image
pred_arr = []
for rect in digits_rectangles:
x, y, w, h = rect
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
im_digit = imgray[y:y + h, x:x + w]
im_digit = (255 - im_digit)
im_digit = imresize(im_digit, (IMG_WIDTH, IMG_HEIGHT))
hog_img_data = pixels_to_hog_20([im_digit])
pred = model.predict(hog_img_data)
cv2.putText(im, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)
cv2.putText(blank_image, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 5)
pred_arr.append(pred[0])
# plt.imshow(im)
cv2.imwrite("output1X.png", im)
copyfile("output1X.png", "static/output1.png")
# cv2.imwrite("final_digits.png",blank_image)
# cv2.destroyAllWindows()
# retval, buffer = cv2.imencode('.png', im)
# png_as_text = base64.b64encode(buffer)
# response = make_response(png_as_text)
return None
def runs(model, image):
im = image
blank_image = np.zeros((im.shape[0], im.shape[1], 3), np.uint8)
blank_image.fill(255)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
plt.imshow(imgray)
kernel = np.ones((5, 5), np.uint8)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
thresh = cv2.erode(thresh, kernel, iterations=1)
thresh = cv2.dilate(thresh, kernel, iterations=1)
thresh = cv2.erode(thresh, kernel, iterations=1)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
digits_rectangles = get_digits(contours, hierarchy) # rectangles of bounding the digits in user image
pred_arr = []
for rect in digits_rectangles:
x, y, w, h = rect
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
im_digit = imgray[y:y + h, x:x + w]
im_digit = (255 - im_digit)
im_digit = imresize(im_digit, (IMG_WIDTH, IMG_HEIGHT))
hog_img_data = pixels_to_hog_20([im_digit])
pred = model.predict(hog_img_data)
cv2.putText(im, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)
cv2.putText(blank_image, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 5)
pred_arr.append(pred[0])
cv2.imwrite("output1X.png", im)
copyfile("output1X.png", "static/output1.png")
return im
def store_img_runs(model, image):
im = image
blank_image = np.zeros((im.shape[0], im.shape[1], 3), np.uint8)
blank_image.fill(255)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
plt.imshow(imgray)
kernel = np.ones((5, 5), np.uint8)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
thresh = cv2.erode(thresh, kernel, iterations=1)
thresh = cv2.dilate(thresh, kernel, iterations=1)
thresh = cv2.erode(thresh, kernel, iterations=1)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
digits_rectangles = get_digits(contours, hierarchy) # rectangles of bounding the digits in user image
pred_arr = []
img_arr = []
for rect in digits_rectangles:
x, y, w, h = rect
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
im_digit = imgray[y:y + h, x:x + w]
im_digit = (255 - im_digit)
im_digit = imresize(im_digit, (IMG_WIDTH, IMG_HEIGHT))
img_arr.append(im_digit)
hog_img_data = pixels_to_hog_20([im_digit])
pred = model.predict(hog_img_data)
cv2.putText(im, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 3)
cv2.putText(blank_image, str(int(pred[0])), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 3, (255, 0, 0), 5)
pred_arr.append(pred[0])
cv2.imwrite("output1X.png", im)
copyfile("output1X.png", "static/output1.png")
return img_arr, pred_arr
def get_contour_precedence(contour, cols):
return contour[1] * cols + contour[0] # row-wise ordering
def load_digits_custom(img_file):
train_data = []
train_target = []
start_class = 1
im = cv2.imread(img_file)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
plt.imshow(imgray)
kernel = np.ones((5, 5), np.uint8)
ret, thresh = cv2.threshold(imgray, 127, 255, 0)
thresh = cv2.erode(thresh, kernel, iterations=1)
thresh = cv2.dilate(thresh, kernel, iterations=1)
thresh = cv2.erode(thresh, kernel, iterations=1)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
digits_rectangles = get_digits(contours, hierarchy)
digits_rectangles.sort(key=lambda x: get_contour_precedence(x, im.shape[1]))
for index, rect in enumerate(digits_rectangles):
x, y, w, h = rect
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)
im_digit = imgray[y:y + h, x:x + w]
im_digit = (255 - im_digit)
im_digit = imresize(im_digit, (IMG_WIDTH, IMG_HEIGHT))
train_data.append(im_digit)
train_target.append(start_class % 10)
if index > 0 and (index + 1) % 10 == 0:
start_class += 1
cv2.imwrite("training_box_overlay.png", im)
return np.array(train_data), np.array(train_target)
# ------------------data preparation--------------------------------------------
TRAIN_MNIST_IMG = 'training_images/digits.png'
TRAIN_USER_IMG = 'training_images/custom_train_digits.jpg'
TEST_USER_IMG = 'testing_images/testing_image_1.jpeg' # 'test_image.png'
# digits, labels = load_digits(TRAIN_MNIST_IMG) # original MNIST data
digits, labels = load_digits_custom(TRAIN_USER_IMG) # custom handwritten dataset - Better results obtained
digits, labels = shuffle(digits, labels, random_state=256)
train_digits_data = pixels_to_hog_20(digits)
X_train, X_test, y_train, y_test = train_test_split(train_digits_data, labels, test_size=0.33, random_state=42)
# ------------------training and testing----------------------------------------
# model = KNN_MODEL(k = 3)
# model.train(X_train, y_train)
# preds = model.predict(X_test)
# print('Accuracy: ',accuracy_score(y_test, preds))
#
# model = KNN_MODEL(k = 4)
# model.train(train_digits_data, labels)
# proc_user_img(TEST_USER_IMG, model)
model = SVM_MODEL(num_feats=train_digits_data.shape[1])
model.train(X_train, y_train)
preds = model.predict(X_test)
print('Accuracy: ', accuracy_score(y_test, preds))
model = SVM_MODEL(num_feats=train_digits_data.shape[1])
model.train(train_digits_data, labels)
proc_user_img(TEST_USER_IMG, model)
# ------------------------------------------------------------------------------
def deploy(filename):
return proc_user_img(filename, model)
def get_np_array_from_file(tar_extractfile):
return np.asarray(bytearray(tar_extractfile.read()), dtype=np.uint8)
def deployImg(file):
im = cv2.imdecode(get_np_array_from_file(file), 1)
return runs(model, im)
def deployImgCheck(file):
im = cv2.imdecode(get_np_array_from_file(file), 1)
return store_img_runs(model, im)
# def store_img(img_file):
# cv2.imwrite("output2X.png", img_file)
# copyfile("output2X.png", "static/outputdb1.png")