Spaces:
Sleeping
Sleeping
salomonsky
commited on
Commit
路
fdbf59c
1
Parent(s):
347c16b
Update preprocess.py
Browse files- preprocess.py +69 -54
preprocess.py
CHANGED
@@ -15,65 +15,77 @@ import audio
|
|
15 |
from hparams import hparams as hp
|
16 |
import face_detection
|
17 |
|
18 |
-
parser = argparse.ArgumentParser()
|
19 |
-
parser.add_argument('--ngpu', help='Number of GPUs across which to run in parallel', default=1, type=int)
|
20 |
-
parser.add_argument('--batch_size', help='Single GPU Face detection batch size', default=32, type=int)
|
21 |
-
parser.add_argument("--data_root", help="Root folder of the LRS2 dataset", required=True)
|
22 |
-
parser.add_argument("--preprocessed_root", help="Root folder of the preprocessed dataset", required=True)
|
23 |
-
args = parser.parse_args()
|
24 |
-
|
25 |
-
fa = [face_detection.FaceAlignment(face_detection.LandmarksType._2D, flip_input=False, device='cpu') for _ in range(args.ngpu)]
|
26 |
-
|
27 |
-
template = 'ffmpeg -loglevel panic -y -i {} -strict -2 {}'
|
28 |
-
|
29 |
def _progress(generated, to_generate):
|
30 |
progress((generated, to_generate))
|
31 |
|
32 |
-
|
|
|
33 |
|
34 |
-
|
35 |
-
video_stream = cv2.VideoCapture(vfile)
|
36 |
-
frames = []
|
37 |
-
while 1:
|
38 |
-
still_reading, frame = video_stream.read()
|
39 |
-
if not still_reading:
|
40 |
-
video_stream.release()
|
41 |
-
break
|
42 |
-
frames.append(frame)
|
43 |
-
vidname = os.path.basename(vfile).split('.')[0]
|
44 |
-
dirname = vfile.split('/')[-2]
|
45 |
-
fulldir = path.join(args.preprocessed_root, dirname, vidname)
|
46 |
-
os.makedirs(fulldir, exist_ok=True)
|
47 |
-
batches = [frames[i:i + args.batch_size] for i in range(0, len(frames), args.batch_size)]
|
48 |
-
i = -1
|
49 |
-
for fb in tqdm(batches, desc='Processing Video Frames'):
|
50 |
-
preds = fa[gpu_id].get_detections_for_batch(np.asarray(fb))
|
51 |
-
for j, f in enumerate(preds):
|
52 |
-
i += 1
|
53 |
-
if f is None:
|
54 |
-
continue
|
55 |
-
x1, y1, x2, y2 = f
|
56 |
-
cv2.imwrite(path.join(fulldir, '{}.jpg'.format(i)), fb[j][y1:y2, x1:x2])
|
57 |
-
_progress(i + 1, len(batches) * args.batch_size)
|
58 |
|
59 |
-
def
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
def
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
def main(args):
|
79 |
print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
|
@@ -94,4 +106,7 @@ for vfile in tqdm(filelist, desc='Processing Audio Files'):
|
|
94 |
except:
|
95 |
traceback.print_exc()
|
96 |
continue
|
97 |
-
_progress(1, len(filelist))
|
|
|
|
|
|
|
|
15 |
from hparams import hparams as hp
|
16 |
import face_detection
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def _progress(generated, to_generate):
|
19 |
progress((generated, to_generate))
|
20 |
|
21 |
+
def preprocess_video_files(args):
|
22 |
+
fa = [face_detection.FaceAlignment(face_detection.LandmarksType._2D, flip_input=False, device='cpu') for _ in range(args.ngpu)]
|
23 |
|
24 |
+
template = 'ffmpeg -loglevel panic -y -i {} -strict -2 {}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
def process_video_file(vfile, args, gpu_id):
|
27 |
+
video_stream = cv2.VideoCapture(vfile)
|
28 |
+
frames = []
|
29 |
+
while 1:
|
30 |
+
still_reading, frame = video_stream.read()
|
31 |
+
if not still_reading:
|
32 |
+
video_stream.release()
|
33 |
+
break
|
34 |
+
frames.append(frame)
|
35 |
+
vidname = os.path.basename(vfile).split('.')[0]
|
36 |
+
dirname = vfile.split('/')[-2]
|
37 |
+
fulldir = path.join(args.preprocessed_root, dirname, vidname)
|
38 |
+
os.makedirs(fulldir, exist_ok=True)
|
39 |
+
batches = [frames[i:i + args.batch_size] for i in range(0, len(frames), args.batch_size)]
|
40 |
+
i = -1
|
41 |
+
for fb in tqdm(batches, desc='Processing Video Frames'):
|
42 |
+
preds = fa[gpu_id].get_detections_for_batch(np.asarray(fb))
|
43 |
+
for j, f in enumerate(preds):
|
44 |
+
i += 1
|
45 |
+
if f is None:
|
46 |
+
continue
|
47 |
+
x1, y1, x2, y2 = f
|
48 |
+
cv2.imwrite(path.join(fulldir, '{}.jpg'.format(i)), fb[j][y1:y2, x1:x2])
|
49 |
+
_progress(i + 1, len(batches) * args.batch_size)
|
50 |
|
51 |
+
def process_audio_file(vfile, args):
|
52 |
+
vidname = os.path.basename(vfile).split('.')[0]
|
53 |
+
dirname = vfile.split('/')[-2]
|
54 |
+
fulldir = path.join(args.preprocessed_root, dirname, vidname)
|
55 |
+
os.makedirs(fulldir, exist_ok=True)
|
56 |
+
wavpath = path.join(fulldir, 'audio.wav')
|
57 |
+
command = template.format(vfile, wavpath)
|
58 |
+
subprocess.call(command, shell=True)
|
59 |
+
_progress(1, 1)
|
60 |
+
|
61 |
+
def mp_handler(job):
|
62 |
+
vfile, args, gpu_id = job
|
63 |
+
try:
|
64 |
+
process_video_file(vfile, args, gpu_id)
|
65 |
+
except KeyboardInterrupt:
|
66 |
+
exit(0)
|
67 |
+
except:
|
68 |
+
traceback.print_exc()
|
69 |
+
|
70 |
+
print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
|
71 |
+
filelist = glob(path.join(args.data_root, '*.mp4'))
|
72 |
+
|
73 |
+
jobs = [(vfile, args, i % args.ngpu) for i, vfile in enumerate(filelist)]
|
74 |
+
p = ThreadPoolExecutor(args.ngpu)
|
75 |
+
futures = [p.submit(mp_handler, j) for j in jobs]
|
76 |
+
_ = [r.result() for r in tqdm(as_completed(futures), total=len(futures))]
|
77 |
+
|
78 |
+
print('Dumping audios...')
|
79 |
+
|
80 |
+
for vfile in tqdm(filelist):
|
81 |
+
try:
|
82 |
+
process_audio_file(vfile, args)
|
83 |
+
_progress(1, 1)
|
84 |
+
except KeyboardInterrupt:
|
85 |
+
exit(0)
|
86 |
+
except:
|
87 |
+
traceback.print_exc()
|
88 |
+
continue
|
89 |
|
90 |
def main(args):
|
91 |
print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
|
|
|
106 |
except:
|
107 |
traceback.print_exc()
|
108 |
continue
|
109 |
+
_progress(1, len(filelist))
|
110 |
+
|
111 |
+
if __name__ == '__main__':
|
112 |
+
main(args)
|