forked from space-kerala/lekha_OCR_1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
executable file
·333 lines (305 loc) · 8.61 KB
/
training.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 17 10:26:15 2015
@author: james
"""
#feature extraction and Recognition
from __future__ import division
import cv2
import preprocess as pp
import numpy as np
import os
import glob
import shutil
import itertools
from random import shuffle
from path import *
classifier = cv2.SVM()
classifier.load(PATH_TO_MAIN+'svm_class.xml')
def find_feature(char):
# htow_ratio(char)
# q=pixel_intensity(char)
# print q
# return zonewise_hu5(char)+feature_hu2(char)
# return feature_hu2(char)
# print len(hog(char))
# im=preprocess(char)
# return zonewise_hu5(char)+[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]+hog(char)
# return hog(char)
# return zonewise_hu5(char)+zonewise_hu3(char)
# find_vlines(char.copy())
# print len(zonewise_hu5(char)+htow_ratio(char)+q)
return zonewise_hu5(char)+htow_ratio(char)+find_blobs(char)
# return zonewise_hu5(char)+zonewise_hu3(char)
def find_vlines(img):
edges=cv2.Canny(img,50,150,apertureSize=3)
minLineLength=10
maxLineGap=15
lines=cv2.HoughLinesP(edges,1,np.pi,10,minLineLength,maxLineGap)
try:
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),0,2)
print len(lines[0])
except:
print 0
cv2.imshow('ske',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def preprocess(img):
cv2.imwrite('before_pp_thresholding.png',img)
img = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,243,50)
cv2.imwrite('after_pp_thresholding.png',img)
return img
def htow_ratio(im):
h,w=im.shape
q=0
for i in range(h):
for j in range(w):
if im.item(i,j)==255:
q+=1
# print [h/w,(q/(h*w))]
return [h/w,(q/(h*w))]
# def pixel_intensity(im):
def find_blobs(im):
params=cv2.SimpleBlobDetector_Params()
params.filterByArea=True
params.minArea=10
params.filterByConvexity=True
params.minConvexity=0.87
detector=cv2.SimpleBlobDetector(params)
keypoints=detector.detect(im)
# print len(keypoints)
return [len(keypoints)]
def recognize(feature):
a = classifier.predict(feature)
# print (label_uni[int(a)]=='ഠ')
# if(pp.previous_char==None):
# print 'none in prev'
if (label_uni[int(a)]=='0' or label_uni[int(a)]=='ഠ'):
# print pp.previous_char
if(pp.previous_char==None):
return label_uni.index('0')
if(label_uni[int(pp.previous_char.label)].isdigit()):
return label_uni.index('0')
# print pp.previous_char.height,pp.cur_char.height
if(pp.cur_char.height<=(pp.previous_char.height*3/4)):
return label_uni.index('ം')
# print '0'
return label_uni.index('ഠ')
# ===========================================my additions
# if (label_uni[int(a)]=='\'' or label_uni[int(a)]==','):
# # print pp.previous_char
# if(pp.previous_char==None):
# return label_uni.index('\'')
# if(pp.cur_char.height<=(pp.previous_char.height*3/4)):
# return label_uni.index(',')
# # print '0'
# return label_uni.index('\'')
# =====================================
# if (pp.previous_char!= None):
# if (label_uni[int(pp.previous_char.label)]=='ം'):
# if(pp.cur_char.height*3/4<pp.previous_char.height):
# pp.previous_char.label= label_uni.index('ഠ')
# print 'ഠ'
return a
label_uni = []
f = open(PATH_TO_MAIN+'/label','r')
for l in f:
label_uni.append(l[:-1])
# label_uni.append('0')
def label_unicode():
# url='../samples/train_images/'
# for i in range(101,208):
# file=open(url+str(i)+'/utf8',"r")
# i_uni=file.read()
# i_uni=i_uni[:-1]
# label_uni.append(i_uni)
return label_uni
# label_unicode()
label_uni.append('ം')
def hog(img):
bin_n =16
gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
mag, ang = cv2.cartToPolar(gx, gy)
# print len(mag)
# quantizing binvalues in (0...16)
bins = np.int32(bin_n*ang/(2*np.pi))
cut= len(bins)/2
# Divide to 4 sub-squares
bin_cells = bins[:cut,:cut], bins[cut:,:cut], bins[:cut,cut:], bins[cut:,cut:]
mag_cells = mag[:cut,:cut], mag[cut:,:cut], mag[:cut,cut:], mag[cut:,cut:]
hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
hist = np.hstack(hists)
return hist
def feature_hu2(img):
contours, hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
moments = [0,0,0,0,0,0,0,0,0,0,0,0]
if(len(contours)==0):
return moments
X = [cv2.contourArea(C) for C in contours]
t=[i for i in range (0,len(contours))]
X,t = zip(*sorted(zip(X,t),reverse=True))
list = []
for i in range (0,2):
try:
# print (i)
cnt = contours[i]
if(cv2.contourArea(cnt)<4):
[list.append(0.0) for j in range(0,6)]
continue
mom = cv2.HuMoments(cv2.moments(cnt))
moments=mom[:-1]
[list.append(m[0]) for m in moments]
except IndexError:
[list.append(0.0) for j in range(0,6)]
return list
#jithin-additions
def zonewise_hu2(img):
# global ter
contours, hierarchy = cv2.findContours(img.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
X = [cv2.contourArea(C) for C in contours]
t=[i for i in range (0,len(contours))]
X,t = zip(*sorted(zip(X,t),reverse=True))
cnt = contours[t[0]]
x,y,w,h=cv2.boundingRect(cnt)
im = img[y-1:y+h+1,x-1:x+w+1]
# cv2.imwrite('zq2nd'+str(ter)+'.png',im)
height,width=im.shape
box = img[0:1,0:1]
box[0,0]=0
box = cv2.resize(box,(width,height))
img4=[]
[img4.append(box.copy())for i in range(0,4)]
i=0
for i in range (0,height):
j=(int)(i*width/height)
for k in range(0,width):
if(k<j):
img4[0][i,k]=im[i,k]
img4[0][height-i-1,k]=im[height-i-1,k]
elif(k>width-j):
img4[2][i,k]=im[i,k]
img4[2][height-i-1,k]=im[height-i-1,k]
else:
img4[1][i,k]=im[i,k]
img4[3][height-i-1,k]=im[height-i-1,k]
if (j>width/2):
break
# i=0
# for img in img4:
# cv2.imwrite('zq2nd'+str(ter)+'_'+str(i)+'.png',img)
# i+=1
# ter+=1
feature = []
for img in img4:
feature = feature+list(itertools.chain(feature_hu2(img)))
return feature
def zonewise_hu3(img):
# global ter
contours, hierarchy = cv2.findContours(img.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
X = [cv2.contourArea(C) for C in contours]
t=[i for i in range (0,len(contours))]
X,t = zip(*sorted(zip(X,t),reverse=True))
cnt = contours[t[0]]
x,y,w,h=cv2.boundingRect(cnt)
im = img[y-1:y+h+1,x-1:x+w+1]
# cv2.imshow("img",im)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.imwrite('./samples/samp'+str(i)+'.png',im)
height,width=img.shape
M = cv2.moments(cnt)
try:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
except:
return [0]*48
img4=[]
img4.append(img[0:cy,0:cx])
img4.append(img[0:cy,cx:width])
img4.append(img[cy:height,0:cx])
img4.append(img[cy:height,cx:width])
i=0
# for img in img4:
# cv2.imwrite('zq'+str(ter)+str(i)+'.png',img)
# i+=1
# ter+=1
feature = []
for img in img4:
feature = feature+list(itertools.chain(feature_hu2(img)))
# print str(len(feature))+'hai'
return feature
def zonewise_hu5(img):#diagonal with more contours
global ter
contours, hierarchy = cv2.findContours(img.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
X = [cv2.contourArea(C) for C in contours]
t=[i for i in range (0,len(contours))]
try:
X,t = zip(*sorted(zip(X,t),reverse=True))
except ValueError:
cv2.imwrite('error.png',img)
print 'no countours'
exit
cnt = contours[t[0]]
x,y,w,h=cv2.boundingRect(cnt)
for i in range(x,x+w):
for j in range(y,y+h):
if(cv2.pointPolygonTest(cnt,(i,j),False)==-1):
img[j,i]=0
im = img[y-1:y+h+1,x-1:x+w+1]
height,width=im.shape
box = img[0:1,0:1]
box[0,0]=0
box = cv2.resize(box,(width,height))
img4=[]
[img4.append(box.copy())for i in range(0,4)]
i=0
for i in range (0,height):
j=(int)(i*width/height)
for k in range(0,width):
if(k<j):
img4[0][i,k]=im[i,k]
img4[0][height-i-1,k]=im[height-i-1,k]
elif(k>width-j):
img4[2][i,k]=im[i,k]
img4[2][height-i-1,k]=im[height-i-1,k]
else:
img4[1][i,k]=im[i,k]
img4[3][height-i-1,k]=im[height-i-1,k]
if (j>width/2):
break
i=0
feature = []
for img in img4:
feature = feature+feature_hu2(img)
return feature
def load():
classifier.load('svm_class.xml')
load()
def test():
load()
count,correct=0,0
url='../samples/train_images/'
for i in range(101,150):
s_list=glob.glob(url+str(i)+'/*.png')
for j in s_list:
imgo=cv2.imread(j,0)
img=pp.preprocess(imgo.copy())
f = find_feature(img.copy())
fu= np.array(f,np.float32)
# print len(fu)
t = classifier.predict(fu)
print label_uni[i-100],label_uni[int(t)],int(i-100==t)
if(i-100==t):
correct+=1
else:
name = './zerr_'+str(i)+'_'+str(count)+'.png'
print j
print count
# cv2.imwrite('./zerr_'+str(i)+'_'+str(count)+'z.png',img)
shutil.copyfile(j,name)
count+=1
print 'accuracy :'+str(100.0*correct/count)+'%'
print ('accurate recognition :'+str(correct))
print ('total character tested :'+str(count))