-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
419 lines (336 loc) · 11.2 KB
/
train.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import cv2
import numpy as np
import shutil
import os
from os import listdir
from os.path import isfile, join, isdir, exists
from distutils.dir_util import copy_tree
import sys
def main() :
global POSITIVE_WIDTH, POSITIVE_HEIGHT, NEGATIVE_WIDTH, NEGATIVE_HEIGHT, SAMPLE_WIDTH, SAMPLE_HEIGHT, MAX_X_ANGLE
global MAX_Y_ANGLE, MAX_Z_ANGLE, FEATURE_LBP, MIN_HIT_RATE, STAGES, SAMPLE_SCALE
SAMPLE_WIDTH = str(SAMPLE_WIDTH)
SAMPLE_HEIGHT = str(SAMPLE_HEIGHT)
STAGES = str(STAGES)
MAX_X_ANGLE = str(MAX_X_ANGLE)
MAX_Y_ANGLE = str(MAX_Y_ANGLE)
MAX_Z_ANGLE = str(MAX_Z_ANGLE)
MIN_HIT_RATE = str(MIN_HIT_RATE)
mp_p = 'pos'
mp_n = 'neg'
pfs = (POSITIVE_WIDTH, POSITIVE_HEIGHT)
nfs = (NEGATIVE_WIDTH, NEGATIVE_HEIGHT)
sps = (SAMPLE_WIDTH, SAMPLE_HEIGHT)
pps = '/tmp/p' + mp_p
nns = '/tmp/n' + mp_n
fvec = '/tmp/if.vec'
fdat = '/tmp/if.dat'
ftxt = '/tmp/bg.txt'
rslt = '/tmp/result'
print ('Creating environment...')
pf = [f for f in listdir(mp_p) if isfile(join(mp_p, f))]
nf = [f for f in listdir(mp_n) if isfile(join(mp_n, f))]
if exists(rslt): shutil.rmtree(rslt)
if exists(pps): shutil.rmtree(pps)
if exists(nns): shutil.rmtree(nns)
if not exists(rslt): os.makedirs(rslt)
if not exists(pps): os.makedirs(pps)
if not exists(nns): os.makedirs(nns)
print ('Creating environment done.')
print ('Preprocessing images...')
for f in pf:
imgpath = mp_p + '/' + f
fn, fe = os.path.splitext(imgpath)
rsl = cv2.imread(imgpath)
# rsl = cv2.cvtColor(rsl, cv2.COLOR_BGR2GRAY)
if rsl is None: continue
rsl = cv2.resize(rsl, pfs)
cv2.imwrite(pps + '/' + f, rsl)
for f in nf:
imgpath = mp_n + '/' + f
rsl = cv2.imread(imgpath)
# rsl = cv2.cvtColor(rsl, cv2.COLOR_BGR2GRAY)
if rsl is None: continue
rsl = cv2.resize(rsl, nfs)
cv2.imwrite(nns + '/' + f, rsl)
print ('Preprocessing images done')
print ('Normalize images...')
cmd = 'cd ' + pps + '; j=1;for i in *; do mv "$i" "$j"; j=$((j+1));done;'
os.system(cmd)
cmd = 'cd ' + nns + '; j=1;for i in *; do mv "$i" "$j"; j=$((j+1));done;'
os.system(cmd)
print ('Normalize images done')
print ('Creating info annotation...')
print ('Creating background annotation...')
ppf = [f for f in listdir(pps) if isfile(join(pps, f))]
nnf = [f for f in listdir(nns) if isfile(join(nns, f))]
s = ['','']
for f in ppf: s[0] += pps + '/' + f + ' 1 0 0 ' + str(pfs[0]) + ' ' + str(pfs[1]) + '\n'
for f in nnf: s[1] += nns + '/' + f + '\n'
def save(fn, d) :
f = open(fn,'w')
f.write(d)
f.close()
os.remove(fvec) if os.path.exists(fvec) else None
os.remove(fdat) if os.path.exists(fdat) else None
os.remove(ftxt) if os.path.exists(ftxt) else None
save(fdat, s[0])
print ('Creating info done.')
save(ftxt, s[1])
print ('Creating background done.')
# create sample positives
cmd = 'cd /tmp; opencv_createsamples -info if.dat -num ' + str(len(pf)) * SAMPLE_SCALE + ' -w ' + str(sps[0]) + ' -h ' + str(sps[1]) + ' -vec if.vec '
if not MAX_X_ANGLE is '' : cmd += ' -maxxangle= ' + MAX_X_ANGLE
if not MAX_Y_ANGLE is '' : cmd += ' -maxyangle= ' + MAX_Y_ANGLE
if not MAX_Z_ANGLE is '' : cmd += ' -maxzangle= ' + MAX_Z_ANGLE
os.system(cmd)
# train cascade
cmd = 'cd /tmp; opencv_traincascade -data result -vec if.vec -bg bg.txt -numPos ' + str(len(pf) * 0.9)
cmd += ' -numNeg ' + str(len(nf)) + ' -numStages ' + STAGES + ' -w ' + str(sps[0]) + ' -h ' + str(sps[1]) + ' '
if FEATURE_LBP is 1 : cmd += ' -featureType LBP'
if not MIN_HIT_RATE is '' : cmd += ' -minHitRate ' + MIN_HIT_RATE
os.system(cmd)
copy_tree(rslt, os.getcwd() + '/result')
shutil.rmtree(pps)
shutil.rmtree(nns)
shutil.rmtree(rslt)
os.remove(fvec)
os.remove(fdat)
os.remove(ftxt)
cff = 'config.txt'
cfl = []
cfld = {}
POSITIVE_WIDTH = '' # required
POSITIVE_HEIGHT = '' # required
NEGATIVE_WIDTH = '' # required
NEGATIVE_HEIGHT = '' # required
SAMPLE_WIDTH = '' # required
SAMPLE_HEIGHT = '' # required
STAGES = '' # requried
SAMPLE_SCALE = '' # requried
MAX_X_ANGLE = ''
MAX_Y_ANGLE = ''
MAX_Z_ANGLE = ''
FEATURE_LBP = ''
MIN_HIT_RATE = ''
MIN_HIT_RATE = ''
def isNumber(s):
try:
int(s)
return True
except ValueError:
return False
def isFloat(s):
try:
float(s)
return True
except ValueError:
return False
def init() :
global POSITIVE_WIDTH, POSITIVE_HEIGHT, NEGATIVE_WIDTH, NEGATIVE_HEIGHT, SAMPLE_WIDTH, SAMPLE_HEIGHT
global MAX_X_ANGLE, MAX_Y_ANGLE, MAX_Z_ANGLE, FEATURE_LBP, MIN_HIT_RATE, STAGES, SAMPLE_SCALE
if os.path.isfile(cff) :
with open(cff) as f:
cfl = f.readlines()
for l in cfl:
l = l.replace('\n', '')
l = l.split('=')
cfld[l[0]] = l[1]
# validate
if 'POSITIVE_WIDTH' in cfld : POSITIVE_WIDTH = cfld['POSITIVE_WIDTH'].strip()
if 'POSITIVE_HEIGHT' in cfld : POSITIVE_HEIGHT = cfld['POSITIVE_HEIGHT'].strip()
if 'NEGATIVE_WIDTH' in cfld : NEGATIVE_WIDTH = cfld['NEGATIVE_WIDTH'].strip()
if 'NEGATIVE_HEIGHT' in cfld : NEGATIVE_HEIGHT = cfld['NEGATIVE_HEIGHT'].strip()
if 'SAMPLE_WIDTH' in cfld : SAMPLE_WIDTH = cfld['SAMPLE_WIDTH'].strip()
if 'SAMPLE_HEIGHT' in cfld : SAMPLE_HEIGHT = cfld['SAMPLE_HEIGHT'].strip()
if 'FEATURE_LBP' in cfld : FEATURE_LBP = cfld['FEATURE_LBP'].strip()
if 'MAX_X_ANGLE' in cfld : MAX_X_ANGLE = cfld['MAX_X_ANGLE'].strip()
if 'MAX_Y_ANGLE' in cfld : MAX_Y_ANGLE = cfld['MAX_Y_ANGLE'].strip()
if 'MAX_Z_ANGLE' in cfld : MAX_Z_ANGLE = cfld['MAX_Z_ANGLE'].strip()
if 'MIN_HIT_RATE' in cfld : MIN_HIT_RATE = cfld['MIN_HIT_RATE'].strip()
if 'STAGES' in cfld : STAGES = cfld['STAGES'].strip()
if 'SAMPLE_SCALE' in cfld : SAMPLE_SCALE = cfld['SAMPLE_SCALE'].strip()
hasError = False
errMsg = ''
if POSITIVE_WIDTH is '':
hasError = True
errMsg += 'Error: POSITIVE_WIDTH is required \n'
else:
POSITIVE_WIDTH = int(POSITIVE_WIDTH)
if POSITIVE_HEIGHT is '':
hasError = True
errMsg += 'Error: POSITIVE_HEIGHT is required \n'
else:
POSITIVE_HEIGHT = int(POSITIVE_HEIGHT)
if NEGATIVE_WIDTH is '':
hasError = True
errMsg += 'Error: NEGATIVE_WIDTH is required \n'
else:
NEGATIVE_WIDTH = int(NEGATIVE_WIDTH)
if NEGATIVE_HEIGHT is '':
hasError = True
errMsg += 'Error: NEGATIVE_HEIGHT is required \n'
else:
NEGATIVE_HEIGHT = int(NEGATIVE_HEIGHT)
if SAMPLE_WIDTH is '':
hasError = True
errMsg += 'Error: SAMPLE_WIDTH is required \n'
else:
SAMPLE_WIDTH = int(SAMPLE_WIDTH)
if SAMPLE_HEIGHT is '':
hasError = True
errMsg += 'Error: SAMPLE_HEIGHT is required \n'
else:
SAMPLE_HEIGHT = int(SAMPLE_HEIGHT)
if STAGES is '':
hasError = True
errMsg += 'Error: STAGES is required \n'
else:
STAGES = int(STAGES)
if SAMPLE_SCALE is '':
hasError = True
errMsg += 'Error: SAMPLE_SCALE is required \n'
else:
SAMPLE_SCALE = int(SAMPLE_SCALE)
if hasError:
print (errMsg)
return
if not isNumber(POSITIVE_WIDTH) :
hasError = True
errMsg += 'Error: POSITIVE_WIDTH must be number\n'
if not isNumber(POSITIVE_HEIGHT) :
hasError = True
errMsg += 'Error: POSITIVE_HEIGHT must be number\n'
if not isNumber(NEGATIVE_WIDTH) :
hasError = True
errMsg += 'Error: NEGATIVE_WIDTH must be number\n'
if not isNumber(NEGATIVE_HEIGHT) :
hasError = True
errMsg += 'Error: NEGATIVE_HEIGHT must be number\n'
if not isNumber(SAMPLE_WIDTH) :
hasError = True
errMsg += 'Error: SAMPLE_WIDTH must be number\n'
if not isNumber(SAMPLE_HEIGHT) :
hasError = True
errMsg += 'Error: SAMPLE_HEIGHT must be number\n'
if not isNumber(STAGES) :
hasError = True
errMsg += 'Error: STAGES must be number\n'
if not isNumber(SAMPLE_SCALE) :
hasError = True
errMsg += 'Error: SAMPLE_SCALE must be number\n'
if hasError:
print (errMsg)
return
if not MAX_X_ANGLE is '':
if not isFloat(MAX_X_ANGLE) :
hasError = True
errMsg += 'Error: MAX_X_ANGLE must be number\n'
else:
MAX_X_ANGLE = int(MAX_X_ANGLE)
if not MAX_Y_ANGLE is '':
if not isFloat(MAX_Y_ANGLE) :
hasError = True
errMsg += 'Error: MAX_Y_ANGLE must be number\n'
else:
MAX_Y_ANGLE = int(MAX_Y_ANGLE)
if not MAX_Z_ANGLE is '':
if not isFloat(MAX_Z_ANGLE) :
hasError = True
errMsg += 'Error: MAX_Z_ANGLE must be number\n'
else:
MAX_Z_ANGLE = int(MAX_Z_ANGLE)
if not FEATURE_LBP is '':
if not isNumber(FEATURE_LBP) :
hasError = True
errMsg += 'Error: FEATURE_LBP must be number\n'
else:
FEATURE_LBP = int(FEATURE_LBP)
if not MIN_HIT_RATE is '':
if not isFloat(MIN_HIT_RATE) :
hasError = True
errMsg += 'Error: MIN_HIT_RATE must be number\n'
else:
MIN_HIT_RATE = int(MIN_HIT_RATE)
if hasError:
print (errMsg)
return
if int(POSITIVE_WIDTH) >= int(NEGATIVE_WIDTH):
hasError = True
errMsg += 'Error: POSITIVE_WIDTH must smaller than NEGATIVE_WIDTH\n'
if POSITIVE_HEIGHT >= NEGATIVE_HEIGHT:
hasError = True
errMsg += 'Error: POSITIVE_HEIGHT must smaller than NEGATIVE_HEIGHT\n'
if hasError:
print (errMsg)
return
if POSITIVE_WIDTH <= 0:
hasError = True
errMsg += 'Error: POSITIVE_WIDTH must be > than 0\n'
if POSITIVE_HEIGHT <= 0:
hasError = True
errMsg += 'Error: POSITIVE_HEIGHT must be > than 0\n'
if NEGATIVE_WIDTH <= 0:
hasError = True
errMsg += 'Error: NEGATIVE_WIDTH must be > than 0\n'
if NEGATIVE_HEIGHT <= 0:
hasError = True
errMsg += 'Error: NEGATIVE_HEIGHT must be > than 0\n'
if SAMPLE_WIDTH <= 0:
hasError = True
errMsg += 'Error: SAMPLE_WIDTH must be > than 0\n'
if SAMPLE_HEIGHT <= 0:
hasError = True
errMsg += 'Error: SAMPLE_HEIGHT must be > than 0\n'
if STAGES <= 0:
hasError = True
errMsg += 'Error: STAGES must be > than 0\n'
if SAMPLE_SCALE <= 0:
hasError = True
errMsg += 'Error: SAMPLE_SCALE must be > than 0\n'
if not FEATURE_LBP is '':
if not (FEATURE_LBP is 0 or FEATURE_LBP is 1):
hasError = True
errMsg += 'Error: FEATURE_LBP must be either 0 or 1\n'
if not MIN_HIT_RATE is '':
if MIN_HIT_RATE <= 0:
hasError = True
errMsg += 'Error: MIN_HIT_RATE must be > than 0\n'
if hasError:
print (errMsg)
return
main()
else :
print ('Config file is not detected.')
file = open(cff,'w')
print ('Config.txt created.')
file.write('POSITIVE_WIDTH=100\n')
file.write('POSITIVE_HEIGHT=100\n')
file.write('NEGATIVE_WIDTH=640\n')
file.write('NEGATIVE_HEIGHT=480\n')
file.write('SAMPLE_WIDTH=40\n')
file.write('SAMPLE_HEIGHT=40\n')
file.write('STAGES=10\n')
file.write('FEATURE_LBP=1\n')
file.write('SAMPLE_SCALE=5\n')
file.write('MAX_X_ANGLE=\n')
file.write('MAX_Y_ANGLE=\n')
file.write('MAX_Z_ANGLE=\n')
file.write('MIN_HIT_RATE=\n')
file.close()
print ('What would you like to do?')
print ('1. Edit the config file')
print ('2. Run the train script')
print ('0. Exit')
ipt = input('>>')
if ipt is '2':
os.system('python3 train.py')
elif ipt is '1':
os.system('gedit config.txt')
print ('Run the script now? (y/n)')
ipt = input('>>')
if ipt is 'y' or ipt is 'Y':
os.system('python3 train.py')
init()
# # opencv_createsamples -vec if.vec -w 34 -h 34
# # opencv_traincascade -data result -vec if.vec -bg bg.txt -numPost 1 -numNeg 1 -numStages 2 -w 34 -h 34 -featureType LBP