-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKerasNN.py
401 lines (358 loc) · 15.2 KB
/
KerasNN.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# import the necessary packages
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Activation
from keras.optimizers import SGD
from keras.layers import Dense
from keras.utils import np_utils
from keras.models import load_model
from imutils import paths
import numpy as np
import argparse
import cv2
import os
import glob
from os import listdir
from os.path import isfile, join
from commonfunctions import *
from Segmentation import *
import imageio as iio
from skimage import filters
from skimage.color import rgb2gray # only needed for incorrectly saved images
from skimage.measure import regionprops
import collections
import timeit
import datetime
import time
#***********************************************************************************
#*******Intilize Neural Network Model with 2 hidden Layers********
def InilizedModel():
# define the architecture of the network
model = Sequential()
model.add(Dense(40, input_dim=20, activation="relu"))
model.add(Dense(50, activation="relu"))
model.add(Dense(29))
model.add(Activation("softmax"))
# train the model using SGD
sgd = SGD(lr=0.01)
model.compile(loss="binary_crossentropy", optimizer=sgd,metrics=["accuracy"])
return model
#***********************************************************************************
#*******Prepare Data and Labels before entering the model********
def PrepareLabelsData(data,labels):
# encode the labels, converting them from strings to integers and normalize data
labels = list(map(int, labels))
#print("Labels: ",labels)
data = np.asarray(data)
labels = np_utils.to_categorical(labels, 29)
#print(labels)
return data,labels
#***********************************************************************************
#*******Train Model with 3 epochs and save weights*******
def TrainModel(model,data,labels,flag):
model.fit(data, labels, epochs=3, batch_size=128,verbose=1)
model.save("TrainedModelFinal.h5")
flag=True
return model,flag
#***********************************************************************************
#******Load the Saved Model*******
def LoadModel(modelname):
# Returns a compiled model identical to the previous one
model = load_model(modelname)
return model
#***********************************************************************************
#******Evalute Model on the 20% of the dataset*******
def EvaluteModel(model,data,labels):
# show the accuracy on the testing set
#print("[INFO] evaluating on testing set...")
mypath = r'lettersfinal/validation/'
Number_Of_Files = len([ f for f in listdir(mypath) if isfile(join(mypath,f)) ])
#print(Number_Of_Files)
gen = glob.iglob(mypath + "*.png")
for i in range(Number_Of_Files):
py = next(gen)
underscore=py.split("_")
#print(lbl[1])
lbl=underscore[1].split(".")
labels.append(lbl[0])
input_image = cv2.imread(py)
#show_images([input_image])
# print(labels)
#print(py)
gray = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
data.append(image_to_feature_vector(gray))
data, labels = PrepareLabelsData(data, labels)
(loss, accuracy) = model.evaluate(data, labels,batch_size=128, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
return loss,accuracy
#***********************************************************************************
#******Predict input letter image*******
def PredictImage(model,Predictimage):
#Predictimage = cv2.imread(imagepath)
#gray = cv2.cvtColor(Predictimage, cv2.COLOR_BGR2GRAY)
features = image_to_feature_vector(Predictimage)
features = np.asarray([features])
probs = model.predict(features)[0]
#print(probs)
prediction = probs.argmax(axis=0)
#print(prediction)
return prediction
#***********************************************************************************
#******Write the output letters in words in text file*******
def WriteOutput(wordlist,filehandle):
for listitem in wordlist:
filehandle.write('%s' % listitem)
filehandle.write(' ')
#***********************************************************************************
#******arabic dictionary constructor to get value predicted from the model*******
def ConstructArabicDict():
ArabicDictionary = {}
ArabicDictionary['ا'] = 1
ArabicDictionary['ب'] = 2
ArabicDictionary['ت'] = 3
ArabicDictionary['ث'] = 4
ArabicDictionary['ج'] = 5
ArabicDictionary['ح'] = 6
ArabicDictionary['خ'] = 7
ArabicDictionary['د'] = 8
ArabicDictionary['ز'] = 9
ArabicDictionary['ر'] = 10
ArabicDictionary['ز'] = 11
ArabicDictionary['س'] = 12
ArabicDictionary['ش'] = 13
ArabicDictionary['ص'] = 14
ArabicDictionary['ض'] = 15
ArabicDictionary['ط'] = 16
ArabicDictionary['ظ'] = 17
ArabicDictionary['ع'] = 18
ArabicDictionary['غ'] = 19
ArabicDictionary['ف'] = 20
ArabicDictionary['ق'] = 21
ArabicDictionary['ك'] = 22
ArabicDictionary['ل'] = 23
ArabicDictionary['م'] = 24
ArabicDictionary['ن'] = 25
ArabicDictionary['ه'] = 26
ArabicDictionary['و'] = 27
ArabicDictionary['ي'] = 28
ArabicDictionary['لا'] = 29
return ArabicDictionary
#***********************************************************************************
#******get actual letter predicted from the model*******
def GetLetterFromPrediction(ArabicDictionary, Prediction):
key_list = list(ArabicDictionary.keys())
val_list = list(ArabicDictionary.values())
return key_list[val_list.index(Prediction)]
#***********************************************************************************
#******see if the letter contain any holes or not*******
# def WithHoles(im,gray):
# _,contours,_ = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#
# for cnt in contours:
# area = cv2.contourArea(cnt)
# print (area)
# if area < 20:
# cv2.drawContours(im, [cnt], 0, (255, 0, 0), 2)
# show_images([im])
#***********************************************************************************
#******get center of mass for the letter image*******
def CenterofMass(resized):
threshold_value = filters.threshold_otsu(resized)
labeled_foreground = (resized > threshold_value).astype(int)
properties = regionprops(labeled_foreground, resized)
center_of_mass = properties[0].centroid
weighted_center_of_mass = properties[0].weighted_centroid
com=[]
com.append(weighted_center_of_mass)
return com
#***********************************************************************************
#******get black on white and black on black ratios in 4 regions*******
def RatioRegions(threshed_img):
RatioList = []
resized1 = threshed_img[0:25,0:25]/255
resized2 = threshed_img[25:50,0:25]/255
resized3 = threshed_img[0:25,25:50]/255
resized4 = threshed_img[25:50,25:50]/255
bw11 = (625-np.sum(resized1))/(np.sum(resized1)+1)
bw22 = (625-np.sum(resized2))/(np.sum(resized2)+1)
bw33 = (625-np.sum(resized3))/(np.sum(resized3)+1)
bw44 = (625-np.sum(resized4))/(np.sum(resized4)+1)
bb12 = (625-np.sum(resized1))/(625-np.sum(resized2))
bb34 = (625-np.sum(resized3))/(625-np.sum(resized4))
bb13 = (625-np.sum(resized1))/(625-np.sum(resized3))
bb24 = (625-np.sum(resized2))/(625-np.sum(resized4))
bb14 = (625-np.sum(resized1))/(625-np.sum(resized4))
bb23 = (625-np.sum(resized2))/(625-np.sum(resized3))
RatioList.append(bw11)
RatioList.append(bw22)
RatioList.append(bw33)
RatioList.append(bw44)
RatioList.append(bb12)
RatioList.append(bb34)
RatioList.append(bb13)
RatioList.append(bb24)
RatioList.append(bb14)
RatioList.append(bb23)
#RatioList=np.asarray(RatioList)
return RatioList
#***********************************************************************************
#******get labels of connected components*******
def ConnectedComponents(thresh):
connectivity = 8
out=[]
# Perform the operation
output = cv2.connectedComponentsWithStats(thresh, connectivity, cv2.CV_32S)
out.append(output[0])
#out=np.asarray(out)
return out
#***********************************************************************************
#******Get Vertical Projection*******
def getVerticalProjectionProfile(image):
vertical_projection = np.sum(image, axis=0)
return vertical_projection
#***********************************************************************************
#******Get Horizontal Projection*******
def getHorizontalProjectionProfile(image):
horizontal_projection = np.sum(image, axis=1)
return horizontal_projection
#***********************************************************************************
#******Black INK Histogram *******
def BlackInkHist(thresh):
hist = []
hist.append(getVerticalProjectionProfile(thresh))
hist.append(getHorizontalProjectionProfile(thresh))
return hist
#***********************************************************************************
#******get vector of 7 humoments*******
def HuMoment(image):
hu=[]
hu=cv2.HuMoments(cv2.moments(image))
return hu
#***********************************************************************************
#******flatten list of lists*******
def flatten(x):
if isinstance(x, collections.Iterable):
return [a for i in x for a in flatten(i)]
else:
return [x]
#***********************************************************************************
#******Feature vector for the letter image*******
def image_to_feature_vector(image):
# resize the image to a fixed size, then flatten the image into
# a list of raw pixel intensities
feature_vector = []
resized = cv2.resize(image, (50, 50))
ret, threshed_img = cv2.threshold(resized, 127, 255, cv2.THRESH_BINARY)
feature_vector.append(HuMoment(resized))
feature_vector.append(RatioRegions(threshed_img))
feature_vector.append(ConnectedComponents(threshed_img))
#feature_vector.append(BlackInkHist(threshed_img))
feature_vector.append(CenterofMass(threshed_img))
return np.asarray(flatten(feature_vector)).flatten()
#***********************************************************************************
#******main for training model*******
def main_train():
data = []
labels = []
model=InilizedModel()
count=0
TrainedFlag=False
mypath = r'lettersfinal/training/'
Number_Of_Files = len([ f for f in listdir(mypath) if isfile(join(mypath,f)) ])
#print(Number_Of_Files)
gen = glob.iglob(mypath + "*.png")
for i in range(Number_Of_Files):
count+=1
if count == 50000 or i==(Number_Of_Files-1):
print(i)
if TrainedFlag==True:
data,labels=PrepareLabelsData(data,labels)
#print(labels)
TrainedModel, TrainedFlag = TrainModel(TrainedModel, data, labels, TrainedFlag)
else:
data,labels=PrepareLabelsData(data,labels)
TrainedModel, TrainedFlag = TrainModel(model, data, labels, TrainedFlag)
data=[]
labels=[]
count=0
py = next(gen)
underscore=py.split("_")
#print(lbl[1])
lbl=underscore[1].split(".")
labels.append(lbl[0])
input_image = cv2.imread(py)
#show_images([input_image])
# print(labels)
#print(py)
gray = cv2.cvtColor(input_image,cv2.COLOR_BGR2GRAY)
data.append(image_to_feature_vector(gray))
#***********************************************************************************
#train here
#main_train()
#Predict here
# model=LoadModel('TrainedModelFinal.h5')
# pred=PredictImage(model,'lettersfinal/validation/capr1620-49_27.png')
#Evaluate Here
# data = []
# labels = []
# model=LoadModel('TrainedModelFinal.h5')
#loss,accuracy=EvaluteModel(model,data,labels)
#******main for testing and predicting output give image contain text*******
def main_test(thresh): # to segment the test image
Arabic_Dict=ConstructArabicDict()
TrainedModel=load_model("TrainedModelFinal.h5")
lines = SegmentImg2Lines(thresh)
filehandle = open('Output.txt', 'w+', encoding='utf-8')
for line in lines:
# line = lines[-1]
words, _ = Segmentline2word(line)
# words,_ = Segmentline2word(lines[1])
#show_images([line])
for word in words:
# show_images([word])
BaselineIndex = FindBaselineIndex(word)
MaxTransitionIndex = FindingMaxTrans(word / 255, BaselineIndex)
SeparationRegions, MFV = CutPointIdentification(
word / 255, MaxTransitionIndex)
ValidSeparationRegions = SeparationRegionFilteration(
word / 255, SeparationRegions, BaselineIndex, MaxTransitionIndex, MFV)
ValidSeparationRegions.reverse() # 3ashan ageb el kelma mn awelha mesh el 3aks
#
# word1 = word.copy()
# for i in range(len(ValidSeparationRegions)):
# word1[MaxTransitionIndex, int(ValidSeparationRegions[i].CutIndex)] = 150
#
# show_images([word1])
#WordLettersImage = []
OutputWord = []
if len(ValidSeparationRegions) != 0:
for i in range(len(ValidSeparationRegions) + 1):
letter = 0
if i == 0:
letter = word[:, ValidSeparationRegions[i].CutIndex:]
elif i == len(ValidSeparationRegions):
letter = word[:, :ValidSeparationRegions[i - 1].CutIndex]
else: # in middle (normal)
letter = word[:, ValidSeparationRegions[i].CutIndex: ValidSeparationRegions[i - 1].CutIndex]
#show_images([letter])
#WordLettersImage.append(letter)
Prediction=PredictImage(TrainedModel,letter)
OutputLetter=GetLetterFromPrediction(Arabic_Dict,Prediction)
OutputWord.append(OutputLetter)
WriteOutput(OutputWord,filehandle)
#***********************************************************************************
#******Calling the main test and write time elapsed for performance*******
img = cv2.imread('capr27.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
start = timeit.default_timer()
S = time.time()
main_test(thresh)
stop = timeit.default_timer()
ElapsedTime = time.time() - S
#print('Time: ', stop - start)
#print('elapsed: ', time.time() - S)
filehandle = open('time.txt', 'w+')
filehandle.write('%s' % ElapsedTime)
#***********************************End of Code************************************************