-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathrun_pipeline.py
More file actions
executable file
·345 lines (236 loc) · 10.7 KB
/
run_pipeline.py
File metadata and controls
executable file
·345 lines (236 loc) · 10.7 KB
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
#!/usr/bin/python
import sys, time, os, pdb, argparse, pickle, subprocess
import numpy as np
import tensorflow as tf
import cv2
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 utils import label_map_util
from scipy.io import wavfile
from scipy import signal
# ========== ========== ========== ==========
# # 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='Name of the video');
parser.add_argument('--crop_scale', type=float, default=0.5, help='Scale bounding box');
parser.add_argument('--min_track', type=int, default=100, help='Minimum facetrack duration');
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'))
# ========== ========== ========== ==========
# # 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
numFail = 3 # Number of missed detections allowed
minSize = 0.05 # Minimum size of faces
tracks = []
while True:
track = []
for faces in scenefaces:
for face in faces:
if track == []:
track.append(face)
faces.remove(face)
elif face[0] - track[-1][0] <= numFail:
iou = bb_intersection_over_union(face[1], track[-1][1])
if iou > iouThres:
track.append(face)
faces.remove(face)
continue
else:
break
if track == []:
break
elif len(track) > opt.min_track:
framenum = np.array([ f[0] for f in track ])
bboxes = np.array([np.array(f[1]) 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 np.mean(bboxes_i[:,3]-bboxes_i[:,1]) > minSize:
tracks.append([frame_i,bboxes_i])
return tracks
# ========== ========== ========== ==========
# # VIDEO CROP AND SAVE
# ========== ========== ========== ==========
def crop_video(opt,track,cropfile):
cap = cv2.VideoCapture(os.path.join(opt.avi_dir,opt.reference,'video.avi'))
total_frames = cap.get(7)
cap.set(1,track[0][0]) # CHANGE THIS !!!
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vOut = cv2.VideoWriter(cropfile+'t.avi', fourcc, cap.get(5), (224,224))
fw = cap.get(3)
fh = cap.get(4)
dets = [[], [], []]
for det in track[1]:
dets[0].append(((det[3]-det[1])*fw+(det[2]-det[0])*fh)/4) # H+W / 4
dets[1].append((det[1]+det[3])*fw/2) # crop center x
dets[2].append((det[0]+det[2])*fh/2) # crop center y
# Smooth detections
dets[0] = signal.medfilt(dets[0],kernel_size=5)
dets[1] = signal.medfilt(dets[1],kernel_size=5)
dets[2] = signal.medfilt(dets[2],kernel_size=7)
for det in zip(*dets):
cs = opt.crop_scale
bs = det[0] # Detection box size
bsi = int(bs*(1+2*cs)) # Pad videos by this amount
ret, frame = cap.read()
frame = np.pad(frame,((bsi,bsi),(bsi,bsi),(0,0)), 'constant', constant_values=(0,0))
my = det[2]+bsi # BBox center Y
mx = det[1]+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[0][0]/cap.get(5)
audioend = (track[0][-1]+1)/cap.get(5)
cap.release()
vOut.release()
# ========== CROP AUDIO FILE ==========
command = ("ffmpeg -y -i %s -ac 1 -vn -acodec pcm_s16le -ar 16000 -ss %.3f -to %.3f %s" % (os.path.join(opt.avi_dir,opt.reference,'video.avi'),audiostart,audioend,audiotmp)) #-async 1
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)) #-async 1
output = subprocess.call(command, shell=True, stdout=None)
if output != 0:
pdb.set_trace()
print('Written %s'%cropfile)
os.remove(cropfile+'t.avi')
return [track,dets]
# ========== ========== ========== ==========
# # FACE DETECTION
# ========== ========== ========== ==========
def inference_video(opt):
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = './protos/frozen_inference_graph_face.pb'
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = './protos/face_label_map.pbtxt'
NUM_CLASSES = 2
MIN_CONF = 0.3
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
cap = cv2.VideoCapture(os.path.join(opt.avi_dir,opt.reference,'video.avi'))
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
dets = []
with detection_graph.as_default():
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(graph=detection_graph, config=config) as sess:
frame_num = 0;
while True:
ret, image = cap.read()
if ret == 0:
break
image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_np_expanded = np.expand_dims(image_np, axis=0)
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
scores = detection_graph.get_tensor_by_name('detection_scores:0')
classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
# Actual detection.
start_time = time.time()
(boxes, scores, classes, num_detections) = sess.run(
[boxes, scores, classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
elapsed_time = time.time() - start_time
score = scores[0]
dets.append([]);
for index in range(0,len(score)):
if score[index] > MIN_CONF:
dets[-1].append([frame_num, boxes[0][index].tolist(),score[index]])
print('%s-%05d; %d dets; %.2f Hz' % (os.path.join(opt.avi_dir,opt.reference,'video.avi'),frame_num,len(dets[-1]),(1/elapsed_time)))
frame_num += 1
cap.release()
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,'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
# ========== ========== ========== ==========
if not(os.path.exists(os.path.join(opt.work_dir,opt.reference))):
os.makedirs(os.path.join(opt.work_dir,opt.reference))
if not(os.path.exists(os.path.join(opt.crop_dir,opt.reference))):
os.makedirs(os.path.join(opt.crop_dir,opt.reference))
if not(os.path.exists(os.path.join(opt.avi_dir,opt.reference))):
os.makedirs(os.path.join(opt.avi_dir,opt.reference))
if not(os.path.exists(os.path.join(opt.tmp_dir,opt.reference))):
os.makedirs(os.path.join(opt.tmp_dir,opt.reference))
command = ("ffmpeg -y -i %s -qscale:v 4 -r 25 %s" % (opt.videofile,os.path.join(opt.avi_dir,opt.reference,'video.avi'))) #-async 1 -deinterlace
output = subprocess.call(command, shell=True, stdout=None)
faces = inference_video(opt)
# with open(os.path.join(opt.work_dir,opt.reference,'faces.pckl'), 'rb') as fil:
# faces = pickle.load(fil, encoding='latin1')
scene = scene_detect(opt)
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]))
for ii, track in enumerate(alltracks):
vidtracks.append(crop_video(opt,track,os.path.join(opt.crop_dir,opt.reference,'%05d'%ii)))
savepath = os.path.join(opt.work_dir,opt.reference,'tracks.pckl')
with open(savepath, 'wb') as fil:
pickle.dump(vidtracks, fil)