11#!/usr/bin/env python3
22
3- import sys , time , os , pdb , argparse , pickle , subprocess , glob , cv2 , logging
3+ import sys , time , os , argparse , pickle , subprocess , glob , cv2 , logging
44import numpy as np
55import torch
66from shutil import rmtree
99logger = logging .getLogger (__name__ )
1010
1111from scenedetect import open_video , SceneManager , ContentDetector
12+ from tqdm import tqdm
1213
1314from scipy .interpolate import interp1d
1415from scipy .io import wavfile
3031parser .add_argument ('--frame_rate' , type = int , default = 25 , help = 'Frame rate' )
3132parser .add_argument ('--num_failed_det' , type = int , default = 25 , help = 'Number of missed detections allowed before tracking is stopped' )
3233parser .add_argument ('--min_face_size' , type = int , default = 100 , help = 'Minimum face size in pixels' )
34+ parser .add_argument ('--overwrite' , action = 'store_true' , help = 'Overwrite existing output directories' )
3335opt = parser .parse_args ()
3436
3537setattr (opt ,'avi_dir' ,os .path .join (opt .data_dir ,'pyavi' ))
@@ -153,7 +155,8 @@ def crop_video(opt,track,cropfile):
153155
154156 # ========== CROP AUDIO FILE ==========
155157
156- command = ["ffmpeg" , "-y" , "-i" ,
158+ logger .info ('Cropping audio track for %s' , cropfile )
159+ command = ["ffmpeg" , "-y" , "-loglevel" , "error" , "-i" ,
157160 os .path .join (opt .avi_dir , opt .reference , 'audio.wav' ),
158161 "-ss" , "%.3f" % audiostart , "-to" , "%.3f" % audioend ,
159162 audiotmp ]
@@ -163,7 +166,8 @@ def crop_video(opt,track,cropfile):
163166
164167 # ========== COMBINE AUDIO AND VIDEO FILES ==========
165168
166- command = ["ffmpeg" , "-y" , "-i" , cropfile + 't.avi' , "-i" , audiotmp ,
169+ logger .info ('Merging audio and video for %s' , cropfile )
170+ command = ["ffmpeg" , "-y" , "-loglevel" , "error" , "-i" , cropfile + 't.avi' , "-i" , audiotmp ,
167171 "-c:v" , "copy" , "-c:a" , "copy" , cropfile + '.avi' ]
168172 subprocess .run (command , check = True )
169173
@@ -189,22 +193,19 @@ def inference_video(opt):
189193
190194 dets = []
191195
192- for fidx , fname in enumerate (flist ):
196+ with tqdm (enumerate (flist ), total = len (flist ), desc = 'Detecting faces' ) as pbar :
197+ for fidx , fname in pbar :
193198
194- start_time = time . time ( )
199+ image = cv2 . imread ( fname )
195200
196- image = cv2 .imread (fname )
201+ image_np = cv2 .cvtColor (image , cv2 .COLOR_BGR2RGB )
202+ bboxes = DET .detect_faces (image_np , conf_th = 0.9 , scales = [opt .facedet_scale ])
197203
198- image_np = cv2 .cvtColor (image , cv2 .COLOR_BGR2RGB )
199- bboxes = DET .detect_faces (image_np , conf_th = 0.9 , scales = [opt .facedet_scale ])
204+ dets .append ([])
205+ for bbox in bboxes :
206+ dets [- 1 ].append ({'frame' :fidx , 'bbox' :(bbox [:- 1 ]).tolist (), 'conf' :bbox [- 1 ]})
200207
201- dets .append ([])
202- for bbox in bboxes :
203- dets [- 1 ].append ({'frame' :fidx , 'bbox' :(bbox [:- 1 ]).tolist (), 'conf' :bbox [- 1 ]})
204-
205- elapsed_time = time .time () - start_time
206-
207- logger .info ('%s-%05d; %d dets; %.2f Hz' , os .path .join (opt .avi_dir ,opt .reference ,'video.avi' ),fidx ,len (dets [- 1 ]),(1 / elapsed_time ))
208+ pbar .set_postfix (dets = len (dets [- 1 ]))
208209
209210 savepath = os .path .join (opt .work_dir ,opt .reference ,'faces.pckl' )
210211
@@ -250,6 +251,9 @@ def scene_detect(opt):
250251for d in [opt .work_dir , opt .crop_dir , opt .avi_dir , opt .frames_dir , opt .tmp_dir ]:
251252 path = os .path .join (d , opt .reference )
252253 if os .path .exists (path ):
254+ if not opt .overwrite :
255+ sys .exit (f"Output directory already exists: { path } . Use --overwrite to overwrite." )
256+ logger .warning ('Overwriting existing directory: %s' , path )
253257 rmtree (path )
254258
255259# ========== MAKE NEW DIRECTORIES ==========
@@ -259,16 +263,19 @@ def scene_detect(opt):
259263
260264# ========== CONVERT VIDEO AND EXTRACT FRAMES ==========
261265
262- command = ["ffmpeg" , "-y" , "-i" , opt .videofile , "-qscale:v" , "2" , "-async" , "1" , "-r" , "25" ,
266+ logger .info ('Converting video to 25fps: %s' , opt .videofile )
267+ command = ["ffmpeg" , "-y" , "-loglevel" , "error" , "-i" , opt .videofile , "-qscale:v" , "2" , "-async" , "1" , "-r" , "25" ,
263268 os .path .join (opt .avi_dir , opt .reference , 'video.avi' )]
264269subprocess .run (command , check = True )
265270
266- command = ["ffmpeg" , "-y" , "-i" , os .path .join (opt .avi_dir , opt .reference , 'video.avi' ),
271+ logger .info ('Extracting frames from video' )
272+ command = ["ffmpeg" , "-y" , "-loglevel" , "error" , "-i" , os .path .join (opt .avi_dir , opt .reference , 'video.avi' ),
267273 "-qscale:v" , "2" , "-threads" , "1" , "-f" , "image2" ,
268274 os .path .join (opt .frames_dir , opt .reference , '%06d.jpg' )]
269275subprocess .run (command , check = True )
270276
271- command = ["ffmpeg" , "-y" , "-i" , os .path .join (opt .avi_dir , opt .reference , 'video.avi' ),
277+ logger .info ('Extracting audio from video' )
278+ command = ["ffmpeg" , "-y" , "-loglevel" , "error" , "-i" , os .path .join (opt .avi_dir , opt .reference , 'video.avi' ),
272279 "-ac" , "1" , "-vn" , "-acodec" , "pcm_s16le" , "-ar" , "16000" ,
273280 os .path .join (opt .avi_dir , opt .reference , 'audio.wav' )]
274281subprocess .run (command , check = True )
0 commit comments