-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_pipeline.py
363 lines (252 loc) · 11.2 KB
/
run_pipeline.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
import time, os, pickle, subprocess, glob, cv2
import numpy as np
from shutil import rmtree
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
from scipy.interpolate import interp1d
from scipy.io import wavfile
from scipy import signal
from detectors import S3FD
def bb_intersection_over_union(boxA, boxB):
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
interArea = max(0, xB - xA) * max(0, yB - yA)
boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
iou = interArea / float(boxAArea + boxBArea - interArea)
return iou
# ========== ========== ========== ==========
# # FACE TRACKING
# ========== ========== ========== ==========
def track_shot(scenefaces):
iouThres = 0.5 # Minimum IOU between consecutive face detections
tracks = []
while True:
track = []
for framefaces in scenefaces:
for face in framefaces:
if track == []:
track.append(face)
framefaces.remove(face)
elif face['frame'] - track[-1]['frame'] <= num_failed_det:
iou = bb_intersection_over_union(face['bbox'], track[-1]['bbox'])
if iou > iouThres:
track.append(face)
framefaces.remove(face)
continue
else:
break
if track == []:
break
elif len(track) > min_track:
framenum = np.array([f['frame'] for f in track])
bboxes = np.array([np.array(f['bbox']) for f in track])
frame_i = np.arange(framenum[0], framenum[-1] + 1)
bboxes_i = []
for ij in range(0, 4):
interpfn = interp1d(framenum, bboxes[:, ij])
bboxes_i.append(interpfn(frame_i))
bboxes_i = np.stack(bboxes_i, axis=1)
if max(np.mean(bboxes_i[:, 2] - bboxes_i[:, 0]), np.mean(bboxes_i[:, 3] - bboxes_i[:, 1])) > min_face_size:
tracks.append({'frame': frame_i, 'bbox': bboxes_i})
return tracks
# ========== ========== ========== ==========
# # VIDEO CROP AND SAVE
# ========== ========== ========== ==========
def crop_video(track, cropfile, crop_face=True):
flist = glob.glob(os.path.join(frames_dir, reference, '*.jpg'))
flist.sort()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vOut = cv2.VideoWriter(cropfile + 't.avi', fourcc, frame_rate, (224, 224))
dets = {'x': [], 'y': [], 's': []}
for det in track['bbox']:
dets['s'].append(max((det[3] - det[1]), (det[2] - det[0])) / 2)
dets['y'].append((det[1] + det[3]) / 2) # crop center x
dets['x'].append((det[0] + det[2]) / 2) # crop center y
# Smooth detections
dets['s'] = signal.medfilt(dets['s'], kernel_size=13)
dets['x'] = signal.medfilt(dets['x'], kernel_size=13)
dets['y'] = signal.medfilt(dets['y'], kernel_size=13)
for fidx, frame in enumerate(track['frame']):
cs = crop_scale
bs = dets['s'][fidx] # Detection box size
bsi = int(bs * (1 + 2 * cs)) # Pad videos by this amount
image = cv2.imread(flist[frame])
if crop_face:
frame = np.pad(image, ((bsi, bsi), (bsi, bsi), (0, 0)), 'constant', constant_values=(110, 110))
else:
frame = image
my = dets['y'][fidx] + bsi # BBox center Y
mx = dets['x'][fidx] + bsi # BBox center X
if crop_face:
face = frame[int(my - bs):int(my + bs * (1 + 2 * cs)), int(mx - bs * (1 + cs)):int(mx + bs * (1 + cs))]
else:
face = frame
vOut.write(cv2.resize(face, (224, 224)))
audiotmp = os.path.join(tmp_dir, reference, 'audio.wav')
audiostart = (track['frame'][0]) / frame_rate
audioend = (track['frame'][-1] + 1) / frame_rate
vOut.release()
# ========== CROP AUDIO FILE ==========
command = ("ffmpeg -y -i %s -ss %.3f -to %.3f %s" % (
os.path.join(avi_dir, reference, 'audio.wav'), audiostart, audioend, audiotmp))
output = subprocess.call(command, shell=True, stdout=None)
# print(output)
# if output != 0:
# pdb.set_trace()
sample_rate, audio = wavfile.read(audiotmp)
# ========== COMBINE AUDIO AND VIDEO FILES ==========
command = ("ffmpeg -y -i %st.avi -i %s -c:v copy -c:a copy %s.avi" % (cropfile, audiotmp, cropfile))
output = subprocess.call(command, shell=True, stdout=None)
# if output != 0:
# pdb.set_trace()
print('Written %s' % cropfile)
os.remove(cropfile + 't.avi')
print('Mean pos: x %.2f y %.2f s %.2f' % (np.mean(dets['x']), np.mean(dets['y']), np.mean(dets['s'])))
return {'track': track, 'proc_track': dets}
# ========== ========== ========== ==========
# # FACE DETECTION
# ========== ========== ========== ==========
def inference_video():
DET = S3FD(device='cuda')
flist = glob.glob(os.path.join(frames_dir, reference, '*.jpg'))
flist.sort()
dets = []
for fidx, fname in enumerate(flist):
start_time = time.time()
image = cv2.imread(fname)
image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
bboxes = DET.detect_faces(image_np, conf_th=0.9, scales=[facedet_scale])
dets.append([]);
for bbox in bboxes:
dets[-1].append({'frame': fidx, 'bbox': (bbox[:-1]).tolist(), 'conf': bbox[-1]})
elapsed_time = time.time() - start_time
print('%s-%05d; %d dets; %.2f Hz' % (
os.path.join(avi_dir, reference, 'video.avi'), fidx, len(dets[-1]), (1 / elapsed_time)))
savepath = os.path.join(work_dir, reference, 'faces.pckl')
with open(savepath, 'wb') as fil:
pickle.dump(dets, fil)
return dets
# ========== ========== ========== ==========
# # SCENE DETECTION
# ========== ========== ========== ==========
def scene_detect():
video_manager = VideoManager([os.path.join(avi_dir, reference, 'video.avi')])
stats_manager = StatsManager()
scene_manager = SceneManager(stats_manager)
# Add ContentDetector algorithm (constructor takes detector options like threshold).
scene_manager.add_detector(ContentDetector())
base_timecode = video_manager.get_base_timecode()
video_manager.set_downscale_factor()
video_manager.start()
scene_manager.detect_scenes(frame_source=video_manager)
scene_list = scene_manager.get_scene_list(base_timecode)
savepath = os.path.join(work_dir, reference, 'scene.pckl')
scene_list = [(video_manager.get_base_timecode(), video_manager.get_current_timecode())]
with open(savepath, 'wb') as fil:
pickle.dump(scene_list, fil)
print('%s - scenes detected %d' % (os.path.join(avi_dir, reference, 'video.avi'), len(scene_list)))
return scene_list
def run_pipeline(arg_data_dir, arg_videofile, arg_reference, label, crop_face=True):
global data_dir;
data_dir = arg_data_dir
global videofile;
videofile = arg_videofile
global reference;
reference = arg_reference
global avi_dir;
avi_dir = os.path.join(data_dir, 'pyavi')
global tmp_dir;
tmp_dir = os.path.join(data_dir, 'pytmp')
global work_dir;
work_dir = os.path.join(data_dir, 'pywork')
global crop_dir;
crop_dir = os.path.join(data_dir, 'pycrop', label)
global frames_dir;
frames_dir = os.path.join(data_dir, 'pyframes')
global facedet_scale;
facedet_scale = 0.25
global crop_scale;
crop_scale = 0.40
global min_track;
min_track = 50
global frame_rate;
frame_rate = 30
global num_failed_det;
num_failed_det = 40
global min_face_size;
min_face_size = 50
if not os.path.exists(os.path.join(data_dir, 'pycrop')):
os.makedirs(os.path.join(data_dir, 'pycrop'))
if not os.path.exists(avi_dir):
os.makedirs(avi_dir)
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
if not os.path.exists(work_dir):
os.makedirs(work_dir)
if not os.path.exists(crop_dir):
os.makedirs(crop_dir)
if not os.path.exists(frames_dir):
os.makedirs(frames_dir)
# ========== DELETE EXISTING DIRECTORIES ==========
if os.path.exists(os.path.join(work_dir, reference)):
rmtree(os.path.join(work_dir, reference))
if os.path.exists(os.path.join(crop_dir, reference)):
rmtree(os.path.join(crop_dir, reference))
if os.path.exists(os.path.join(avi_dir, reference)):
rmtree(os.path.join(avi_dir, reference))
if os.path.exists(os.path.join(frames_dir, reference)):
rmtree(os.path.join(frames_dir, reference))
if os.path.exists(os.path.join(tmp_dir, reference)):
rmtree(os.path.join(tmp_dir, reference))
# ========== MAKE NEW DIRECTORIES ==========
os.makedirs(os.path.join(work_dir, reference))
os.makedirs(os.path.join(crop_dir, reference))
os.makedirs(os.path.join(avi_dir, reference))
os.makedirs(os.path.join(frames_dir, reference))
os.makedirs(os.path.join(tmp_dir, reference))
# ========== CONVERT VIDEO AND EXTRACT FRAMES ==========
# -r is for setting frame rate in fps
# -y Overwrite output files without asking
# -f image2 will extract frames from video
# third command extracts audio from video
command = ("ffmpeg -y -i %s -qscale:v 2 -async 1 -r 30 %s" % (
videofile, os.path.join(avi_dir, reference, 'video.avi')))
output = subprocess.call(command, shell=True, stdout=None)
command = ("ffmpeg -y -i %s -qscale:v 2 -threads 1 -f image2 %s" % (
os.path.join(avi_dir, reference, 'video.avi'), os.path.join(frames_dir, reference, '%06d.jpg')))
output = subprocess.call(command, shell=True, stdout=None)
command = ("ffmpeg -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 48000 %s" % (
os.path.join(avi_dir, reference, 'video.avi'), os.path.join(avi_dir, reference, 'audio.wav')))
output = subprocess.call(command, shell=True, stdout=None)
# ========== FACE DETECTION ==========
faces = inference_video()
# ========== SCENE DETECTION ==========
scene = scene_detect()
# ========== FACE TRACKING ==========
alltracks = []
vidtracks = []
for shot in scene:
if shot[1].frame_num - shot[0].frame_num >= min_track:
alltracks.extend(track_shot(faces[shot[0].frame_num:shot[1].frame_num]))
try:
# ========== FACE TRACK CROP ==========
for ii, track in enumerate(alltracks):
vidtracks.append(crop_video(track, os.path.join(crop_dir, reference, '%05d' % ii), crop_face=crop_face))
# ========== SAVE RESULTS ==========
savepath = os.path.join(work_dir, reference, 'tracks.pckl')
with open(savepath, 'wb') as fil:
pickle.dump(vidtracks, fil)
rmtree(os.path.join(tmp_dir, reference))
except:
rmtree(os.path.join(tmp_dir, reference))
rmtree(os.path.join(crop_dir, reference))
rmtree(os.path.join(avi_dir, reference))
rmtree(os.path.join(work_dir, reference))
rmtree(os.path.join(frames_dir, reference))