-
Notifications
You must be signed in to change notification settings - Fork 5
/
read_predict1.py
289 lines (225 loc) · 7.3 KB
/
read_predict1.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
# coding: utf-8
# In[19]:
import os
import cv2
import numpy as np
os.chdir('/home/mehak/Documents/HandwritingRecognitionANN/')
import Neural_Networks as nn
import Load_MNIST as lm
import math
import scipy.misc
import sys
# In[20]:
#This function extracts the opposite points of a rectangle.
def getp(rect):
x = []; y=[];
for i in range(0,4):
x.append(rect[i][0][0])
y.append(rect[i][0][1])
x.sort()
y.sort()
return[[x[1],x[2]],[y[1],y[2]]]
# In[21]:
def show_image(im):
cv2.imshow('new',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
# In[22]:
#All the images are have dimensions greater than 50x50
def crop(i):
os.chdir('/home/mehak/Documents/HandwritingRecognitionANN/outputs/images/')
try:
im = cv2.imread('outfile' +str(i)+ '.jpg')
h,w = im.shape[:2]
roi = im[10:h-10, 10:w-15]
saveimage(roi,i)
except:
print "ok"
# In[23]:
#This function arranges the images in order from left to right and also gets the diagonal points
def arrange(rects):
ar = [[[1001000]]]
for r in rects:
tr = getp(r)
print tr
for i in range(0, len(ar)):
if(tr[0][0] < ar[i][0][0]):
ar.insert(i,tr)
break
return ar[:-1]
# In[24]:
def saveimages(crops):
for i in range(1, len(crops)+1):
print i
scipy.misc.imsave('outfile'+str(i)+'.jpg', crops[i-1])
# In[25]:
def savedata(crops):
os.chdir( cwd + '/outputs/data')
for i in range(0,len(crops)):
crops[i].dump("data" + str(i+1) +".data")
os.chdir(cwd)
# In[26]:
#This function provides us with six nearly equal rectangles
def filter(rects):
tps = [[0]]
for r in rects:
area = cv2.contourArea(r)
print area
x = True
for tp in tps:
if(area > 0.8*tp[0] and area < 1.2*tp[0]):
tp.append(r)
x = False
if(x):
tps.append([area,r])
for tp in tps:
if (len(tp) == 7 and tp[0] > 28*28):
return tp[1:]
# In[27]:
#Setting to 650 by default change as required, lower than this won't be scaled.
def initialize_image(file_name):
im = cv2.imread(file_name)
height, width = im.shape[:2]
print height
print width
rwidth = 400
sfactor = float(width)/rwidth
rheight = int(math.ceil(float(height)/sfactor ))
if(sfactor > 1):
return cv2.resize(im, (rwidth,rheight))
else:
return im
# In[28]:
def show_contours(rects):
for i in range(0,len(rects)) :
area = cv2.contourArea(rects[i])
cv2.drawContours(im,rects,i,(((i%2)+1)*255,(i%2)*255,0),2)
cv2.imshow('before',im)
cv2.waitKey(0)
cv2.destroyAllWindows()
# In[29]:
#Reading all files from the folder and returning a list
# get_list(1)
def get_list(dir_no):
os.chdir('/home/mehak/Documents/HandwritingRecognitionANN/DATASET/' )
list = os.listdir(str(dir_no))
return list
# In[30]:
def vectorize(i):
a= np.zeros(10)
a[i] = 1
a = np.reshape(a,(10,1))
return a
# In[31]:
def make_train_set():
training_set = []
for dir_no in range (0,10):
lis = get_list(dir_no)
t_data = process(dir_no, lis)
training_set += t_data
print len(lis)
return training_set
# In[32]:
#Process Images from a particular folder.
#assuming present path to be at Handwriting Recoginition
def process(dir_no, im_list):
os.chdir('/home/mehak/Documents/HandwritingRecognitionANN/DATASET/' + str(dir_no) + '/')
print os.getcwd()
x = []
for im in im_list:
img = cv2.imread(im)
height,width = img.shape[:2]
rimg = cv2.resize(img, (28,28))
ret,rimg = cv2.threshold(rimg,127,255,cv2.THRESH_BINARY)
rimg = cv2.GaussianBlur(rimg,(1,1),0)
rimg = cv2.cvtColor( rimg, cv2.COLOR_RGB2GRAY )
dip = 255 - rimg
dip = cv2.dilate(dip, np.ones((2,2)))
dip = dip.astype(np.float32, copy=False)
dip = dip/255
dip = np.reshape(dip, (784,1))
x.append((dip,vectorize(dir_no)))
return x
# In[33]:
#Saves the dimens of image, I wish to resize the image proportional to its original dimensions.
#The scaling factor is such that width will be 1000 else lesser for low pixel image.
#file_name = sys.argv[0]
file_name = 'test.jpg'
im = initialize_image(file_name)
height,width = im.shape[:2]
print height
print width
# In[34]:
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
imgray = cv2.fastNlMeansDenoising(imgray,None,10,7,21)
#Thresholding requires a grayscale image 2nd param : threshvalue and 3rd param : maxValue of a pixel
ret,thresh = cv2.threshold(imgray,125,255,0)
ret,thresh = cv2.threshold(imgray,125,255,0)
thresh = (255-thresh)
#Closing is dialation followed by erosion helps to fill out the gaps left out by creases in paper or disconnected components.
#Size of kernel is area of sliding window, I think it should be proportional to the size of image/boxes we will be using.
ki = int(math.ceil(float(width)/100))
kernel = np.ones((ki,ki), np.uint8)
print ki
# kernel = np.ones((4,4), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
#Countours are curves joining all the continuous points having same colour or intensity.
#http://opencvpython.blogspot.in/2012/06/hi-this-article-is-tutorial-which-try.html
#The result "contours" is a Python list, where it contains all objects boundary points as separate lists.
#Whichever element of contoeur is to be drawn set the 2nd param accordingly on an index of zero, -1 to show all the contours
#Last arguement draws the boundary in pixels pass -1 for a filled image.
thresh2=thresh.copy()
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#Contour Approximation to detect shapes.
approx = []
for i in range(0,len(contours)) :
epsilon = 0.025*cv2.arcLength (contours[i],True)
approx.append(cv2.approxPolyDP(contours[i],epsilon,True))
#Separate the ones which are rectangles. :)
rects = []
for i in range(0,len(approx)):
if(len(approx[i]) == 4):
rects.append(approx[i])
show_contours(rects)
if(len(rects) > 6):
rects = filter(rects)
im = initialize_image(file_name)
#Arrange in ascending order of x and put in opposite points
frects = arrange(rects)
#Ahead of this show_contours won't work as only opposite points are returned.
im = initialize_image(file_name)
crops = []
for r in frects:
crops.append( im[ r[1][0]:r[1][1], r[0][0]:r[0][1] ] )
# saveimages(crops)
# In[35]:
show_contours(rects)
# In[39]:
train_set = make_train_set()
net = nn.Network([784,30,10])
net.stochastic_gradient_descent(train_set,30,30,3.0)
# In[42]:
# np.shape(mnist.test.images) Gives (10000, 784)
# np.shape(mnist.test.labels) Gives (10000, 10)
# print(sess.run(tf.argmax(y,1), feed_dict={x: mnist.test.images})) Gives [7 2 1 ..., 4 8 6]
"""Give a proper Thresholding now."""
os.chdir('/home/mehak/Documents/HandwritingRecognitionANN/')
def predict():
x = []
for crop in crops:
rimg = cv2.resize(crop, (28,28))
ret,rimg = cv2.threshold(rimg,127,255,cv2.THRESH_BINARY)
rimg = cv2.GaussianBlur(rimg,(1,1),0)
rimg = cv2.cvtColor( rimg, cv2.COLOR_RGB2GRAY )
dip = 255 - rimg
dip = cv2.dilate(dip, np.ones((1,1)))
dip = dip.astype(np.float32, copy=False)
dip = dip/255
dip = np.reshape(dip, (784,1))
a=np.argmax(net.feedforward(dip))
print a
x.append(a)
return x
# In[43]:
pin = predict()
print pin