-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacial_recipe.py
423 lines (307 loc) · 11.2 KB
/
facial_recipe.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
418
419
420
421
422
423
#!/usr/bin/python3
from tempfile import NamedTemporaryFile
import csv
import os
import shutil
class FacialRecipe:
csv_fields = ['blink', 'm2e', 'date', 'pid', 'type', 'start_frame', 'end_frame', 'duration', 'width_diff', 'data_blink', 'data_eh', 'data_ma', 'data_mh', 'data_m2e', 'age', 'pd_stage']
def __init__(self, recipe_path, no_update = False):
self.__init = False
self.__no_update = no_update
if os.path.isfile(recipe_path) == False:
# should not get here
print('fr: recipe file not exist')
return
self.recipe_path = recipe_path
self.recipe_file = open(self.recipe_path, 'r', newline = '')
self.csv_reader = csv.DictReader(self.recipe_file)
if self.__no_update == False:
self.temp_file = NamedTemporaryFile(mode = 'w', delete = False)
self.csv_writer = csv.DictWriter(self.temp_file, fieldnames = self.csv_fields)
self.csv_writer.writeheader()
self.__init = True
self.__csv_data = False
return
def __del__(self):
if self.__init == False:
return
if self.__no_update == False:
if self.__csv_data != False:
# update the row
self.csv_writer.writerow(self.csv_row)
# flush all rows
while True:
# read the next row
try:
self.csv_row = next(self.csv_reader)
except StopIteration:
break
self.csv_writer.writerow(self.csv_row)
self.temp_file.close()
self.recipe_file.close()
shutil.move(self.temp_file.name, self.recipe_path)
else:
self.recipe_file.close()
def init(self):
return self.__init
def read_next(self):
ret = True
if self.__no_update == False:
if self.__csv_data != False:
# update the row
self.csv_writer.writerow(self.csv_row)
# read the next row
try:
self.csv_row = next(self.csv_reader)
except StopIteration:
ret = False
self.__csv_data = ret
return ret
def get_file_path(self):
if self.__csv_data == False:
print('fr: csv data not available')
return ''
file_path = '/media/Temp_AIpose%s/SJCAM/%s_%s%s.mp4' % (self.csv_row['date'], self.csv_row['date'], self.csv_row['pid'], self.csv_row['type'])
return file_path
def reset_data_fields(self):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_blink'] = '0'
self.csv_row['data_eh'] = '0'
self.csv_row['data_m2e'] = '0'
def find_data_ma(self):
if self.__csv_data == False:
print('fr: csv data not available')
return False, 0.0
with open(self.recipe_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
if row['m2e'] != 'yes':
continue
if row['date'] != self.csv_row['date']:
continue
if row['pid'] != self.csv_row['pid']:
continue
start_frame = int(row['start_frame'])
if start_frame == 0:
continue
if row['data_ma'] == '':
return 0.0
return True, float(row['data_ma'])
print("fr: fail to find data_ma");
return False, 0.0
def find_data_m2e(self):
if self.__csv_data == False:
print('fr: csv data not available')
return False, 0.0
with open(self.recipe_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
if row['m2e'] != 'yes':
continue
if row['date'] != self.csv_row['date']:
continue
if row['pid'] != self.csv_row['pid']:
continue
start_frame = int(row['start_frame'])
if start_frame == 0:
continue
if row['data_m2e'] == '':
return 0.0
return True, float(row['data_m2e'])
print("fr: fail to find data_m2e");
return False, 0.0
def find_data_mh(self):
if self.__csv_data == False:
print('fr: csv data not available')
return False, 0.0
with open(self.recipe_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
if row['m2e'] != 'yes':
continue
if row['date'] != self.csv_row['date']:
continue
if row['pid'] != self.csv_row['pid']:
continue
start_frame = int(row['start_frame'])
if start_frame == 0:
continue
if row['data_mh'] == '':
return 0.0
return True, float(row['data_mh'])
print("fr: fail to find data_mh");
return False, 0.0
# standard get/set functions
def get_blink(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 'no'
if 'blink' not in self.csv_row:
return 'no'
if self.csv_row['blink'] == '':
return 'no'
return self.csv_row['blink']
def get_m2e(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 'no'
if 'm2e' not in self.csv_row:
return 'no'
if self.csv_row['m2e'] == '':
return 'no'
return self.csv_row['m2e']
def get_date(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'date' not in self.csv_row:
return '0'
if self.csv_row['date'] == '':
return '0'
return self.csv_row['date']
def get_pid(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'pid' not in self.csv_row:
return '0'
if self.csv_row['pid'] == '':
return '0'
return self.csv_row['pid']
def get_start_frame(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'start_frame' not in self.csv_row:
return 0
if self.csv_row['start_frame'] == '':
return 0
return int(self.csv_row['start_frame'])
def set_start_frame(self, start_frame):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['start_frame'] = str(start_frame)
def get_end_frame(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'end_frame' not in self.csv_row:
return 0
if self.csv_row['end_frame'] == '':
return 0
return int(self.csv_row['end_frame'])
def set_end_frame(self, end_frame):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['end_frame'] = str(end_frame)
def get_duration(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0.0
if 'data_blink' not in self.csv_row:
return 0.0
if self.csv_row['duration'] == '':
return 0.0
return float(self.csv_row['duration'])
def set_duration(self, duration):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['duration'] = '%.3f' % (duration)
def set_width_diff(self, width_diff):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['width_diff'] = str(width_diff)
def get_data_blink(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'data_blink' not in self.csv_row:
return 0
if self.csv_row['data_blink'] == '':
return 0
return int(self.csv_row['data_blink'])
def set_data_blink(self, data_blink):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_blink'] = str(data_blink)
def get_data_eh(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0.0
if 'data_eh' not in self.csv_row:
return 0.0
if self.csv_row['data_eh'] == '':
return 0.0
return float(self.csv_row['data_eh'])
def set_data_eh(self, data_eh):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_eh'] = '%.3f' % (data_eh)
def get_data_ma(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0.0
if 'data_ma' not in self.csv_row:
return 0.0
if self.csv_row['data_ma'] == '':
return 0.0
return float(self.csv_row['data_ma'])
def set_data_ma(self, data_ma):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_ma'] = '%.3f' % (data_ma)
def get_data_mh(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0.0
if 'data_mh' not in self.csv_row:
return 0.0
if self.csv_row['data_mh'] == '':
return 0.0
return float(self.csv_row['data_mh'])
def set_data_mh(self, data_mh):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_mh'] = '%.3f' % (data_mh)
def get_data_m2e(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0.0
if 'data_m2e' not in self.csv_row:
return 0.0
if self.csv_row['data_m2e'] == '':
return 0.0
return float(self.csv_row['data_m2e'])
def set_data_m2e(self, data_m2e):
if self.__csv_data == False:
print('fr: csv data not available')
return
self.csv_row['data_m2e'] = '%.3f' % (data_m2e)
def get_age(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'age' not in self.csv_row:
return 0
if self.csv_row['age'] == '':
return 0
return int(self.csv_row['age'])
def get_pd_stage(self):
if self.__csv_data == False:
print('fr: csv data not available')
return 0
if 'pd_stage' not in self.csv_row:
return 0
if self.csv_row['pd_stage'] == '':
return 0
return int(self.csv_row['pd_stage'])