-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_m2e_data.py
executable file
·321 lines (225 loc) · 9.16 KB
/
generate_m2e_data.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
#!/usr/bin/python3
from facial_recipe import FacialRecipe
from facial_video import FacialVideo
import argparse
import cv2
import magic
import matplotlib.pyplot as plt
import numpy as np
import os
import time
def log_start(data_path, input_video_path):
# remove directory part
_, file_name = os.path.split(input_video_path)
# remove ext part
file_name, _ = os.path.splitext(file_name)
while True:
# get current time (local time)
now = time.localtime()
timestamp = time.strftime('%Y-%m%d-%H%M', now)
file_name = '%s-%s.log' % (file_name, timestamp)
log_path = os.path.join(data_path, file_name)
log_path = os.path.abspath(log_path)
if os.path.exists(log_path) == False:
break
log_file = open(log_path, 'w')
return log_file, timestamp
def log_stop(log_file):
log_file.close()
return
def log_print(log_file, string, end = '\n'):
# print to screen directly
print(string, end = end)
if end == '\r':
end = '\n'
log_file.write('%s%s' % (string, end))
return
def process_one_video(input_video_path, data_path, start_frame, end_frame):
frame_index = 0
frame_no_landmarks = 0
log_file, timestamp = log_start(data_path, input_video_path)
log_print(log_file, 'Process video:')
log_print(log_file, ' input video path: %s' % (input_video_path))
log_print(log_file, ' start frame: %d' % (start_frame))
log_print(log_file, ' end frame: %d' % (end_frame))
# remove directory part
_, file_name = os.path.split(input_video_path)
# remove ext part
file_name, _ = os.path.splitext(file_name)
file_name = '%s-%s.mp4' % (file_name, timestamp)
output_video_path = os.path.join(data_path, file_name)
output_video_path = os.path.abspath(output_video_path)
log_print(log_file, ' output video path: %s' % (output_video_path))
fv = FacialVideo(input_video_path)
if fv.init() == False:
print(' fail to init engine')
return False, 0.0, 0.0, 0.0
ret = fv.update_statistic_data(start_frame, end_frame)
print('Statistic data:')
if ret == False:
print(' fail')
return False, 0.0, 0.0, 0.0
em_min = fv.get_eye_to_mouth_length(fv.MIN)
em_avg = fv.get_eye_to_mouth_length(fv.AVG)
em_max = fv.get_eye_to_mouth_length(fv.MAX)
mh_max = fv.get_mouth_height(fv.MAX)
print(' eye to mouth(left): min %.3f, avg %.3f, max %.3f' % (em_min[fv.LEFT_EYE], em_avg[fv.LEFT_EYE], em_max[fv.LEFT_EYE]))
print(' eye to mouth(right): min %.3f, avg %.3f, max %.3f' % (em_min[fv.RIGHT_EYE], em_avg[fv.RIGHT_EYE], em_max[fv.RIGHT_EYE]))
# calculate the output video dimensions
face_rect = fv.find_face_rect(start_frame, end_frame)
p1 = face_rect[0] # left, top
p2 = face_rect[1] # right, bottom
log_print(log_file, ' face: %s, %s' % (str(p1), str(p2)))
width = p2[0] - p1[0]
height = p2[1] - p1[1]
# always use mp4
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'mp4v'),
fv.fps, (width, height))
log_print(log_file, 'Process frame:')
em_prev = np.zeros((2, 1), dtype = float)
em_delta_total = np.zeros((2, 1), dtype = float)
ma_prev = 0.0
ma_delta_total = 0.0
mh_prev = 0.0
mh_delta_total = 0.0
while True:
frame_index += 1
if frame_index < start_frame:
# don't decode this frame to speed up
ret, _ = fv.read(True)
continue
elif frame_index > end_frame:
break
else:
# decode this frame
ret, frame = fv.read()
if ret == False:
# no frame to process
break;
if frame_index != fv.get_frame_index():
print(' expect frame %d but got %d' %(frame_index, fv.get_frame_index()))
break
if fv.available() != False:
time_stamp = (frame_index - start_frame) / fv.fps
landmarks = fv.get_landmarks()
rect = fv.get_rect()
em = np.array(fv.calculate_eye_to_mouth_length())
if em_prev[0] != 0.0 or em_prev[1] != 0.0:
em_delta = em - em_prev
else:
em_delta = np.zeros((2, 1), dtype = float)
em_prev = em
ma = fv.calculate_mouth_angle()
if ma_prev != 0.0:
ma_delta = ma - ma_prev
else:
ma_delta = 0.0
ma_prev = ma
mh = fv.calculate_mouth_height()
if mh_prev != 0.0:
mh_delta = mh - mh_prev
else:
mh_delta = 0.0
mh_prev = mh
#length_left = em[fv.LEFT_EYE] * 100.0 / fv.eye_to_mouth[fv.LEFT_EYE][fv.MAX]
#length_right = em[fv.RIGHT_EYE] * 100.0 / fv.eye_to_mouth[fv.RIGHT_EYE][fv.MAX]
log_print(log_file, ' frame: %3d, time: %.3f, length: %.3f %.3f, em_delta %+.3f %+.3f, ma_delta %+.3f, mh_delta %+.3f' % (frame_index, time_stamp, em[fv.LEFT_EYE], em[fv.RIGHT_EYE], em_delta[0], em_delta[1], ma_delta, mh_delta), end = '')
log_print(log_file, '')
em_delta_total[0] = em_delta_total[0] + abs(em_delta[0])
em_delta_total[1] = em_delta_total[1] + abs(em_delta[1])
ma_delta_total = ma_delta_total + abs(ma_delta)
mh_delta_total = mh_delta_total + abs(mh_delta)
else:
log_print(log_file, ' frame: %3d, no landmarks' % (frame_index))
frame_no_landmarks += 1
crop = frame[p1[1]:p2[1], p1[0]:p2[0]]
video_writer.write(crop)
video_writer.release()
log_print(log_file, 'Statistic:')
log_print(log_file, ' total %d frames processed' % (end_frame - start_frame + 1))
log_print(log_file, ' %d frames (%3.2f%%) has no landmarks' % (frame_no_landmarks, (frame_no_landmarks * 100.0) / (end_frame - start_frame + 1)))
log_print(log_file, ' total em_delta %.3f %.3f' % (em_delta_total[0], em_delta_total[1]))
log_print(log_file, 'Process complete')
log_stop(log_file)
return True, (em_delta_total[0] / em_max[fv.LEFT_EYE]) + (em_delta_total[1] / em_max[fv.RIGHT_EYE]), ma_delta_total, mh_delta_total
def process_training_csv(csv_path, data_path):
_, filename = os.path.split(csv_path)
print('Process training csv: %s' % (filename))
fr = FacialRecipe(csv_path)
if fr.init() == False:
print(' fail to init recipe')
return False
while fr.read_next() != False:
if fr.get_m2e() != 'yes':
continue
start_frame = fr.get_start_frame()
if start_frame == 0:
continue
video_path = fr.get_file_path()
end_frame = fr.get_end_frame()
if fr.get_data_m2e() == 0.0 or fr.get_data_ma() == 0.0 or fr.get_data_mh() == 0.0:
ret, em_delta, ma_delta, mh_delta = process_one_video(video_path, data_path, start_frame, end_frame)
if ret == False:
print(' fail')
return False
fr.set_data_m2e(em_delta)
fr.set_data_ma(ma_delta)
fr.set_data_mh(mh_delta)
print(' success')
return True
def main():
start_time = 0.0
duration = 0.0
# check data directory first
data_path = os.path.abspath('./m2e_data')
if os.path.isdir(data_path) == False:
try:
print('create data directory')
os.mkdir(data_path)
except OSError:
print('fail to create data directory')
return False
# parse argument
parser = argparse.ArgumentParser()
parser.add_argument('input_path', help = 'path to a video file or a training recipe file')
parser.add_argument('-s', '--start_time', help = 'start time (sec)')
parser.add_argument('-d', '--duration', help = 'duration (sec)')
args = parser.parse_args()
input_path = args.input_path
if args.start_time != None:
start_time = float(args.start_time)
if args.duration != None:
duration = float(args.duration)
print('User input:')
print(' input path: %s' % (input_path))
print(' start time: %.3f' % (start_time))
print(' duration: %.3f' % (duration))
_, ext = os.path.splitext(input_path)
if ext == '.csv':
if args.start_time != None:
print(' ignore start time')
if args.duration != None:
print(' ignore duration')
# could be a training recipe
ret = process_training_csv(input_path, data_path)
elif os.path.isfile(input_path) != False:
mime = magic.Magic(mime=True)
file_mine = mime.from_file(input_path)
if file_mine.find('video') == -1:
print(' not a video file')
return
fv = FacialVideo(input_path)
if fv.init() == False:
print(' fail to init engine')
return
start_frame = int(start_time * fv.fps)
if duration == 0.0:
end_frame = fv.frame_count
else:
end_frame = start_frame + int(duration * fv.fps) - 1
ret, _ = process_one_video(input_path, data_path, start_frame, end_frame)
else:
print('Unrecognized path')
return
if __name__ == '__main__':
main()