Spaces:
Sleeping
Sleeping
salomonsky
commited on
Commit
路
347c16b
1
Parent(s):
95f108c
Update preprocess.py
Browse files- preprocess.py +17 -9
preprocess.py
CHANGED
@@ -26,6 +26,11 @@ fa = [face_detection.FaceAlignment(face_detection.LandmarksType._2D, flip_input=
|
|
26 |
|
27 |
template = 'ffmpeg -loglevel panic -y -i {} -strict -2 {}'
|
28 |
|
|
|
|
|
|
|
|
|
|
|
29 |
def process_video_file(vfile, args, gpu_id):
|
30 |
video_stream = cv2.VideoCapture(vfile)
|
31 |
frames = []
|
@@ -41,7 +46,7 @@ def process_video_file(vfile, args, gpu_id):
|
|
41 |
os.makedirs(fulldir, exist_ok=True)
|
42 |
batches = [frames[i:i + args.batch_size] for i in range(0, len(frames), args.batch_size)]
|
43 |
i = -1
|
44 |
-
for fb in batches:
|
45 |
preds = fa[gpu_id].get_detections_for_batch(np.asarray(fb))
|
46 |
for j, f in enumerate(preds):
|
47 |
i += 1
|
@@ -49,6 +54,7 @@ def process_video_file(vfile, args, gpu_id):
|
|
49 |
continue
|
50 |
x1, y1, x2, y2 = f
|
51 |
cv2.imwrite(path.join(fulldir, '{}.jpg'.format(i)), fb[j][y1:y2, x1:x2])
|
|
|
52 |
|
53 |
def process_audio_file(vfile, args):
|
54 |
vidname = os.path.basename(vfile).split('.')[0]
|
@@ -58,6 +64,7 @@ def process_audio_file(vfile, args):
|
|
58 |
wavpath = path.join(fulldir, 'audio.wav')
|
59 |
command = template.format(vfile, wavpath)
|
60 |
subprocess.call(command, shell=True)
|
|
|
61 |
|
62 |
def mp_handler(job):
|
63 |
vfile, args, gpu_id = job
|
@@ -70,20 +77,21 @@ def mp_handler(job):
|
|
70 |
|
71 |
def main(args):
|
72 |
print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
|
73 |
-
filelist = glob(path.join(args.data_root, '*.mp4'))
|
74 |
|
75 |
-
jobs = [(vfile, args, i % args.ngpu) for i, vfile in enumerate(filelist)]
|
76 |
-
p = ThreadPoolExecutor(args.ngpu)
|
77 |
-
futures = [p.submit(mp_handler, j) for j in jobs]
|
78 |
-
_ = [r.result() for r in tqdm(as_completed(futures), total=len(futures))]
|
|
|
79 |
|
80 |
print('Dumping audios...')
|
81 |
-
|
82 |
-
for vfile in tqdm(filelist):
|
83 |
try:
|
84 |
process_audio_file(vfile, args)
|
85 |
except KeyboardInterrupt:
|
86 |
exit(0)
|
87 |
except:
|
88 |
traceback.print_exc()
|
89 |
-
continue
|
|
|
|
26 |
|
27 |
template = 'ffmpeg -loglevel panic -y -i {} -strict -2 {}'
|
28 |
|
29 |
+
def _progress(generated, to_generate):
|
30 |
+
progress((generated, to_generate))
|
31 |
+
|
32 |
+
_progress(0, 1)
|
33 |
+
|
34 |
def process_video_file(vfile, args, gpu_id):
|
35 |
video_stream = cv2.VideoCapture(vfile)
|
36 |
frames = []
|
|
|
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
|
|
|
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 process_audio_file(vfile, args):
|
60 |
vidname = os.path.basename(vfile).split('.')[0]
|
|
|
64 |
wavpath = path.join(fulldir, 'audio.wav')
|
65 |
command = template.format(vfile, wavpath)
|
66 |
subprocess.call(command, shell=True)
|
67 |
+
_progress(1, 1)
|
68 |
|
69 |
def mp_handler(job):
|
70 |
vfile, args, gpu_id = job
|
|
|
77 |
|
78 |
def main(args):
|
79 |
print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
|
80 |
+
filelist = glob(path.join(args.data_root, '*.mp4'))
|
81 |
|
82 |
+
jobs = [(vfile, args, i % args.ngpu) for i, vfile in enumerate(filelist)]
|
83 |
+
p = ThreadPoolExecutor(args.ngpu)
|
84 |
+
futures = [p.submit(mp_handler, j) for j in jobs]
|
85 |
+
_ = [r.result() for r in tqdm(as_completed(futures), total=len(futures))]
|
86 |
+
_progress(1, 1)
|
87 |
|
88 |
print('Dumping audios...')
|
89 |
+
for vfile in tqdm(filelist, desc='Processing Audio Files'):
|
|
|
90 |
try:
|
91 |
process_audio_file(vfile, args)
|
92 |
except KeyboardInterrupt:
|
93 |
exit(0)
|
94 |
except:
|
95 |
traceback.print_exc()
|
96 |
+
continue
|
97 |
+
_progress(1, len(filelist))
|