-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testing.py
358 lines (279 loc) · 10.7 KB
/
Testing.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
"""
Handwritten Mathematical Expression Recognition
Creates Symbol Layout Tree
Run
Testing.py <Test .inkml Directory> <Output Directory> <Symbol classifier pickle> <Segmentor classifier pickle> <Parser classifier pickle>
Input:
Test .inkml Directory - Directory where .inkml files are present for testing purpose
Output Directory - Directory where output .lg files will be created
Symbol classifier pickle - Random forest pickle for symbol classification
Segmentation classifier pickle - Random forest pickle for Segmentation classification
Parser classifier pickle - Random forest pickle for Relationship classification
Output:
.lg files - .lg files in the Output directory for all the .inkml file present in Test Directory
Author
Ritvik Joshi
Rahul Dashora
"""
import glob
import os
import pickle
import warnings
import ParserTest
import LOS_v2
import PSC
from xml.etree import cElementTree
import numpy as np
import SymbolClassifier
import geometric as geo
import sys
from sklearn.externals import joblib
import copy
if (len(sys.argv)<6):
print('Please use the following way to run the program')
print('Testing.py <Test .inkml Directory> <Output Directory> <Symbol classifier pickle> <Segmentor classifier pickle>')
print('Test .inkml Directory - Directory where .inkml files are present for testing purpose')
print('Output Directory - Directory where output .lg files will be created')
print('Symbol classifier pickle - Random forest pickle for symbol classification')
print('Segmentation classifier pickle - Random forest pickle for Segmentation classification')
sys.exit(0)
else:
testdir = sys.argv[1]
outputdir = sys.argv[2]
segmentpickle = sys.argv[4]
symbolpickle = sys.argv[3]
parserpickle=sys.argv[5]
segmentfile = open(segmentpickle,'rb')
rf = pickle.load(segmentfile)
rfSymb = joblib.load(symbolpickle)
Parserfile = open(parserpickle,'rb')
rfParser = pickle.load(Parserfile)
#outputdir = 'C:\\Users\\ritvi\\PycharmProjects\\PatternRecParser\\crohmelib\\bin\\AllParts'
# symbolfile = open('SymbolClassifier.p', 'rb')
# rfSymb = pickle.load(symbolfile )
def read_inkml(filename):
"""
Read inkml file and generate class dict,stroke dict and expression graph
:param filename: file path
:param exp_dict: expression dict for the files
:return:
"""
try:
tree = cElementTree.ElementTree(file=filename)
#Read Strokes in the file
strokes=[]
for traces in tree.findall('{http://www.w3.org/2003/InkML}trace'):
strokes_id = traces.items()[0][1]
strokes_text = traces.text
s_list = strokes_text.split(',');
strokes_array = np.empty((len(s_list),2))
for index in range(len(s_list)):
s_list[index] = s_list[index].strip()
xy = s_list[index].split(' ')
if(len(xy)==2):
strokes_array[index] = np.asarray(xy,dtype='float')
else:
strokes_array[index] = np.asarray(xy[:2],dtype='float')
strokes.append(strokes_array)
#print(strokes)
#Read UID of the file
# files_data={}
# UID=''
# for UI in tree.findall("{http://www.w3.org/2003/InkML}annotation"):
# if(UI.items()[0][1]=='UI'):
# UID=UI.text
temp_list = filename.split('\\')
UID = temp_list[len(temp_list)-1].split(".")
return UID[0],strokes
except Exception as e:
print('Exception:',e)
print(filename)
return None,None
def normalizaion(strokePts):
mat = strokePts
maxY=0
minY = 99999
maxX = 0
minX = 99999
for m in mat:
maxX = max(max(m[:,0]),maxX)
minX = min(min(m[:,0]),minX)
maxY = max(max(m[:, 1]), maxY)
minY = min(min(m[:, 1]), minY)
# print(maxX,minX,maxY,minY)
rangeX = maxX - minX
rangeY = maxY - minY
# print(rangeX,rangeY)
yFactor = 200/rangeY
xFactor = 200/rangeY
for m in mat:
np.subtract(m[:, 0], minX,m[:,0])
np.multiply(m[:,0],xFactor,m[:,0])
#print(m[:,0])
np.subtract(m[:, 1], minY,m[:,1])
np.multiply(m[:,1],yFactor,m[:,1])
return mat,int(rangeX*xFactor)
def pairgeneration(LOS):
SLT = []
for iter in range(len(LOS)):
for jiter in range(iter+1,len(LOS)):
#if(LOS[iter][jiter]==1):
SLT.append([iter,jiter])
return SLT
def baseLinePair(LOS):
SLT=[]
for iter in range(0,len(LOS)-1):
SLT.append([iter,iter+1])
return SLT
def feature_extraction(strokes,SLT):
Label=0
Feature=[]
#print("SLT::",SLT)
try:
for pair in SLT:
#print(pair)
geo_features=geometric_features(strokes,pair)
shape_context=PSC.getAllPSC(strokes,pair)
# print('s:',shape_context)
final_feature=np.append(geo_features,shape_context)
Feature.append(final_feature)
except Exception as e:
print(e)
#print(Feature)
return np.asarray(Feature)
def geometric_features(strokes,pair):
#pair=[0,1]
BM=geo.BackwardMovement(strokes[pair[0]],strokes[pair[1]])
HO=geo.Horizontal_offset(strokes[pair[0]],strokes[pair[1]])
DBC=geo.DistBBcenter(strokes[pair[0]],strokes[pair[1]])
MD,BO =geo.MinDistance_BBOverlapping(strokes[pair[0]],strokes[pair[1]])
DAC=geo.DistAverageCenter(strokes[pair[0]],strokes[pair[1]])
MPD=geo.MaximalPairDist(strokes[pair[0]],strokes[pair[1]])
VDBC=geo.VertDistBBCenter(strokes[pair[0]],strokes[pair[1]])
DBp= geo.DistBeginpts(strokes[pair[0]],strokes[pair[1]])
DEp=geo.DistEndpts(strokes[pair[0]],strokes[pair[1]])
VDs,VDe=geo.Vert_offset(strokes[pair[0]],strokes[pair[1]])
SD =geo.sizediff(strokes[pair[0]],strokes[pair[1]])
Parallel=list(geo.Parallelity(strokes[pair[0]],strokes[pair[1]]))
WS=geo.WritingSlope(strokes[pair[0]],strokes[pair[1]])
#STA = geo.StrokeAngle(strokes[pair[0]],strokes[pair[1]])
geo_features=[BM,HO,DBC,MD,BO,DAC,MPD,VDBC,DBp,DEp,VDs,WS,SD]+Parallel
geo_features=np.asarray(geo_features)
#print('g:',geo_features)
return geo_features
def normalizeGeoMetirc(features,rangeX):
# print(features)
for idx in range(13):
features[:,idx] = np.divide(features[:,idx],rangeX)
return features
def PipeLine(filename):
#Reading strokes from inkml files
UID,Strokes =read_inkml(filename)
Strokes_copy = copy.deepcopy(Strokes)
print(UID)
if len(Strokes)<2:
RDF_test(UID,Strokes,True,None,None)
return
#Normalizing strokes
normalizedData,rangeX = normalizaion(Strokes_copy)
#print(UID)
Losgraph=LOS_v2.getLOSGraph(normalizedData)
#print(Losgraph)
SLT=pairgeneration(Losgraph)
#SLT=baseLinePair(Losgraph)
#print(SLT)
Features=feature_extraction(normalizedData,SLT)
NormalizedFeatures = normalizeGeoMetirc(Features,rangeX)
Symbols=RDF_test(UID,copy.deepcopy(Strokes),False,NormalizedFeatures,SLT)
#print(SLT)
#print(Symbols)
ParserTest.ParserPipeline(UID,Symbols,Strokes,rfParser,outputdir)
def RDF_test(UID,Strokes,flag,data_array=None,SLT=None):
classification_pairs=[]
if flag==True:
classification_pairs.append([0])
else:
#leaf_indices = rdtree.predict(data_array)
leaf_indices = rf.predict(data_array)
# for i in range(len(data_array)):
# print(SLT[i],leaf_indices[i])
#
labeledGraph = [['-' for _ in range(len(Strokes))]for _ in range(len(Strokes))]
for i in range(len(data_array)):
if(leaf_indices[i]==1):
labeledGraph[SLT[i][0]][SLT[i][1]] = '*'
labeledGraph[SLT[i][1]][SLT[i][0]] = '*'
# for label in labeledGraph:
# print(label)
visited={}
classification_index=0
for iter in range(len(labeledGraph)):
if(iter not in visited):
pair=[iter]
visited[iter]=classification_index
for jiter in range(iter+1,len(labeledGraph)):
if(labeledGraph[iter][jiter]=='*'):
pair.append(jiter)
visited[jiter]=classification_index
classification_pairs.append(pair)
classification_index+=1;
else:
cl_index=visited[iter]
pair_list=classification_pairs[cl_index]
for jiter in range(iter+1,len(labeledGraph)):
if(labeledGraph[iter][jiter]=='*' and jiter not in pair_list):
pair_list.append(jiter)
visited[jiter]=cl_index
# print(classification_pairs)
final_result={}
for pairs in classification_pairs:
classification_Strokes=[]
for value in pairs:
classification_Strokes.append(Strokes[value])
pairFeatures = SymbolClassifier.Symbol_feature_extraction(classification_Strokes)
#print(len(pairFeatures))
symbol = rfSymb.predict(pairFeatures)
#print(pairs,symbol)
if(symbol[0] not in final_result):
final_result[symbol[0]]=[pairs]
else:
final_result[symbol[0]].append(pairs)
# print(final_result)
#OR_fromat(UID,final_result)
return final_result
def OR_fromat(filename,symbol):
UID=filename
print(UID)
#target = open('C:\\Users\\ritvi\\PycharmProjects\\PatternRecproject2\\crohmelib\\bin\\Adtest2\\'+UID+'.lg','w')
#target = open('C:\\Users\\ritvi\\PycharmProjects\\PatternRecproject2\\crohmelib\\bin\\baseLine\\'+UID+'.lg','w')
target = open(outputdir+'\\'+UID+'.lg','w')
num_object = len(symbol)
target.write("# IUD, "+UID+'\n')
target.write("# Objects("+str(num_object)+"):\n")
weight=1.0
for keys in symbol.keys():
raw_label = keys.split('\\')
if(len(raw_label)==2):
label=raw_label[1]
else:
label=raw_label[0]
label_counter=0
for values in symbol.get(keys):
final_label=label+'_'+chr(49+label_counter)
label_counter+=1
out_string='O,'+final_label+','+keys+','+str(weight)
for strokes in values:
out_string+=','+str(strokes)
target.write(out_string+'\n')
target.close()
def read_files(dir):
for curr_dir,subdir,files in os.walk(dir):
count=0
for filename in glob.glob(os.path.join(curr_dir, '*.inkml')):
PipeLine(filename)
count+=1
print(count)
#PipeLine('C:\\Users\\ritvi\\PycharmProjects\\PatternRecproject2\\AdityaSplit\\train\\65_alfonso.inkml')
warnings.filterwarnings("ignore",category=DeprecationWarning)
#read_files('C:\\Users\\ritvi\\PycharmProjects\\PatternRecproject2\\AdityaSplit\\test')
read_files(testdir)