-
Notifications
You must be signed in to change notification settings - Fork 154
/
run_pipeline.py
executable file
·322 lines (215 loc) · 10.4 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
#!/usr/bin/python
import sys, time, os, pdb, argparse, 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
# ========== ========== ========== ==========
# # PARSE ARGS
# ========== ========== ========== ==========
parser = argparse.ArgumentParser(description = "FaceTracker");
parser.add_argument('--data_dir', type=str, default='data/work', help='Output direcotry');
parser.add_argument('--videofile', type=str, default='', help='Input video file');
parser.add_argument('--reference', type=str, default='', help='Video reference');
parser.add_argument('--facedet_scale', type=float, default=0.25, help='Scale factor for face detection');
parser.add_argument('--crop_scale', type=float, default=0.40, help='Scale bounding box');
parser.add_argument('--min_track', type=int, default=100, help='Minimum facetrack duration');
parser.add_argument('--frame_rate', type=int, default=25, help='Frame rate');
parser.add_argument('--num_failed_det', type=int, default=25, help='Number of missed detections allowed before tracking is stopped');
parser.add_argument('--min_face_size', type=int, default=100, help='Minimum face size in pixels');
opt = parser.parse_args();
setattr(opt,'avi_dir',os.path.join(opt.data_dir,'pyavi'))
setattr(opt,'tmp_dir',os.path.join(opt.data_dir,'pytmp'))
setattr(opt,'work_dir',os.path.join(opt.data_dir,'pywork'))
setattr(opt,'crop_dir',os.path.join(opt.data_dir,'pycrop'))
setattr(opt,'frames_dir',os.path.join(opt.data_dir,'pyframes'))
# ========== ========== ========== ==========
# # IOU FUNCTION
# ========== ========== ========== ==========
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(opt,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'] <= opt.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) > opt.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])) > opt.min_face_size:
tracks.append({'frame':frame_i,'bbox':bboxes_i})
return tracks
# ========== ========== ========== ==========
# # VIDEO CROP AND SAVE
# ========== ========== ========== ==========
def crop_video(opt,track,cropfile):
flist = glob.glob(os.path.join(opt.frames_dir,opt.reference,'*.jpg'))
flist.sort()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vOut = cv2.VideoWriter(cropfile+'t.avi', fourcc, opt.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 = opt.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])
frame = np.pad(image,((bsi,bsi),(bsi,bsi),(0,0)), 'constant', constant_values=(110,110))
my = dets['y'][fidx]+bsi # BBox center Y
mx = dets['x'][fidx]+bsi # BBox center X
face = frame[int(my-bs):int(my+bs*(1+2*cs)),int(mx-bs*(1+cs)):int(mx+bs*(1+cs))]
vOut.write(cv2.resize(face,(224,224)))
audiotmp = os.path.join(opt.tmp_dir,opt.reference,'audio.wav')
audiostart = (track['frame'][0])/opt.frame_rate
audioend = (track['frame'][-1]+1)/opt.frame_rate
vOut.release()
# ========== CROP AUDIO FILE ==========
command = ("ffmpeg -y -i %s -ss %.3f -to %.3f %s" % (os.path.join(opt.avi_dir,opt.reference,'audio.wav'),audiostart,audioend,audiotmp))
output = subprocess.call(command, shell=True, stdout=None)
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(opt):
DET = S3FD(device='cuda')
flist = glob.glob(os.path.join(opt.frames_dir,opt.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=[opt.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(opt.avi_dir,opt.reference,'video.avi'),fidx,len(dets[-1]),(1/elapsed_time)))
savepath = os.path.join(opt.work_dir,opt.reference,'faces.pckl')
with open(savepath, 'wb') as fil:
pickle.dump(dets, fil)
return dets
# ========== ========== ========== ==========
# # SCENE DETECTION
# ========== ========== ========== ==========
def scene_detect(opt):
video_manager = VideoManager([os.path.join(opt.avi_dir,opt.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(opt.work_dir,opt.reference,'scene.pckl')
if scene_list == []:
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(opt.avi_dir,opt.reference,'video.avi'),len(scene_list)))
return scene_list
# ========== ========== ========== ==========
# # EXECUTE DEMO
# ========== ========== ========== ==========
# ========== DELETE EXISTING DIRECTORIES ==========
if os.path.exists(os.path.join(opt.work_dir,opt.reference)):
rmtree(os.path.join(opt.work_dir,opt.reference))
if os.path.exists(os.path.join(opt.crop_dir,opt.reference)):
rmtree(os.path.join(opt.crop_dir,opt.reference))
if os.path.exists(os.path.join(opt.avi_dir,opt.reference)):
rmtree(os.path.join(opt.avi_dir,opt.reference))
if os.path.exists(os.path.join(opt.frames_dir,opt.reference)):
rmtree(os.path.join(opt.frames_dir,opt.reference))
if os.path.exists(os.path.join(opt.tmp_dir,opt.reference)):
rmtree(os.path.join(opt.tmp_dir,opt.reference))
# ========== MAKE NEW DIRECTORIES ==========
os.makedirs(os.path.join(opt.work_dir,opt.reference))
os.makedirs(os.path.join(opt.crop_dir,opt.reference))
os.makedirs(os.path.join(opt.avi_dir,opt.reference))
os.makedirs(os.path.join(opt.frames_dir,opt.reference))
os.makedirs(os.path.join(opt.tmp_dir,opt.reference))
# ========== CONVERT VIDEO AND EXTRACT FRAMES ==========
command = ("ffmpeg -y -i %s -qscale:v 2 -async 1 -r 25 %s" % (opt.videofile,os.path.join(opt.avi_dir,opt.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(opt.avi_dir,opt.reference,'video.avi'),os.path.join(opt.frames_dir,opt.reference,'%06d.jpg')))
output = subprocess.call(command, shell=True, stdout=None)
command = ("ffmpeg -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 16000 %s" % (os.path.join(opt.avi_dir,opt.reference,'video.avi'),os.path.join(opt.avi_dir,opt.reference,'audio.wav')))
output = subprocess.call(command, shell=True, stdout=None)
# ========== FACE DETECTION ==========
faces = inference_video(opt)
# ========== SCENE DETECTION ==========
scene = scene_detect(opt)
# ========== FACE TRACKING ==========
alltracks = []
vidtracks = []
for shot in scene:
if shot[1].frame_num - shot[0].frame_num >= opt.min_track :
alltracks.extend(track_shot(opt,faces[shot[0].frame_num:shot[1].frame_num]))
# ========== FACE TRACK CROP ==========
for ii, track in enumerate(alltracks):
vidtracks.append(crop_video(opt,track,os.path.join(opt.crop_dir,opt.reference,'%05d'%ii)))
# ========== SAVE RESULTS ==========
savepath = os.path.join(opt.work_dir,opt.reference,'tracks.pckl')
with open(savepath, 'wb') as fil:
pickle.dump(vidtracks, fil)
rmtree(os.path.join(opt.tmp_dir,opt.reference))