salomonsky commited on
Commit
95f108c
1 Parent(s): bb6e368

Update preprocess.py

Browse files
Files changed (1) hide show
  1. preprocess.py +17 -42
preprocess.py CHANGED
@@ -1,14 +1,9 @@
1
  import sys
2
-
3
  if sys.version_info[0] < 3 and sys.version_info[1] < 2:
4
  raise Exception("Must be using >= Python 3.2")
5
-
6
  from os import listdir, path
7
-
8
  if not path.isfile('data/face_detection/s3fd-619a316812.pth'):
9
- raise FileNotFoundError('Save the s3fd model to face_detection/detection/sfd/s3fd.pth \
10
- before running this script!')
11
-
12
  import multiprocessing as mp
13
  from concurrent.futures import ThreadPoolExecutor, as_completed
14
  import numpy as np
@@ -18,27 +13,21 @@ from tqdm import tqdm
18
  from glob import glob
19
  import audio
20
  from hparams import hparams as hp
21
-
22
  import face_detection
23
 
24
  parser = argparse.ArgumentParser()
25
-
26
  parser.add_argument('--ngpu', help='Number of GPUs across which to run in parallel', default=1, type=int)
27
  parser.add_argument('--batch_size', help='Single GPU Face detection batch size', default=32, type=int)
28
  parser.add_argument("--data_root", help="Root folder of the LRS2 dataset", required=True)
29
  parser.add_argument("--preprocessed_root", help="Root folder of the preprocessed dataset", required=True)
30
-
31
  args = parser.parse_args()
32
 
33
- fa = [face_detection.FaceAlignment(face_detection.LandmarksType._2D, flip_input=False,
34
- device='cuda:{}'.format(id)) for id in range(args.ngpu)]
35
 
36
  template = 'ffmpeg -loglevel panic -y -i {} -strict -2 {}'
37
- # template2 = 'ffmpeg -hide_banner -loglevel panic -threads 1 -y -i {} -async 1 -ac 1 -vn -acodec pcm_s16le -ar 16000 {}'
38
 
39
  def process_video_file(vfile, args, gpu_id):
40
  video_stream = cv2.VideoCapture(vfile)
41
-
42
  frames = []
43
  while 1:
44
  still_reading, frame = video_stream.read()
@@ -46,40 +35,30 @@ def process_video_file(vfile, args, gpu_id):
46
  video_stream.release()
47
  break
48
  frames.append(frame)
49
-
50
  vidname = os.path.basename(vfile).split('.')[0]
51
  dirname = vfile.split('/')[-2]
52
-
53
  fulldir = path.join(args.preprocessed_root, dirname, vidname)
54
  os.makedirs(fulldir, exist_ok=True)
55
-
56
  batches = [frames[i:i + args.batch_size] for i in range(0, len(frames), args.batch_size)]
57
-
58
  i = -1
59
  for fb in batches:
60
  preds = fa[gpu_id].get_detections_for_batch(np.asarray(fb))
61
-
62
  for j, f in enumerate(preds):
63
  i += 1
64
  if f is None:
65
  continue
66
-
67
  x1, y1, x2, y2 = f
68
  cv2.imwrite(path.join(fulldir, '{}.jpg'.format(i)), fb[j][y1:y2, x1:x2])
69
 
70
  def process_audio_file(vfile, args):
71
  vidname = os.path.basename(vfile).split('.')[0]
72
  dirname = vfile.split('/')[-2]
73
-
74
  fulldir = path.join(args.preprocessed_root, dirname, vidname)
75
  os.makedirs(fulldir, exist_ok=True)
76
-
77
  wavpath = path.join(fulldir, 'audio.wav')
78
-
79
  command = template.format(vfile, wavpath)
80
  subprocess.call(command, shell=True)
81
 
82
-
83
  def mp_handler(job):
84
  vfile, args, gpu_id = job
85
  try:
@@ -88,27 +67,23 @@ def mp_handler(job):
88
  exit(0)
89
  except:
90
  traceback.print_exc()
91
-
92
  def main(args):
93
  print('Started processing for {} with {} GPUs'.format(args.data_root, args.ngpu))
 
94
 
95
- filelist = glob(path.join(args.data_root, '*.mp4'))
96
-
97
- jobs = [(vfile, args, i%args.ngpu) for i, vfile in enumerate(filelist)]
98
- p = ThreadPoolExecutor(args.ngpu)
99
- futures = [p.submit(mp_handler, j) for j in jobs]
100
- _ = [r.result() for r in tqdm(as_completed(futures), total=len(futures))]
101
-
102
- print('Dumping audios...')
103
 
104
- for vfile in tqdm(filelist):
105
- try:
106
- process_audio_file(vfile, args)
107
- except KeyboardInterrupt:
108
- exit(0)
109
- except:
110
- traceback.print_exc()
111
- continue
112
- if __name__ == '__main__':
113
 
114
- main(args)
 
 
 
 
 
 
 
 
1
  import sys
 
2
  if sys.version_info[0] < 3 and sys.version_info[1] < 2:
3
  raise Exception("Must be using >= Python 3.2")
 
4
  from os import listdir, path
 
5
  if not path.isfile('data/face_detection/s3fd-619a316812.pth'):
6
+ raise FileNotFoundError('Save the s3fd model to face_detection/detection/sfd/s3fd.pth before running this script!')
 
 
7
  import multiprocessing as mp
8
  from concurrent.futures import ThreadPoolExecutor, as_completed
9
  import numpy as np
 
13
  from glob import glob
14
  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 process_video_file(vfile, args, gpu_id):
30
  video_stream = cv2.VideoCapture(vfile)
 
31
  frames = []
32
  while 1:
33
  still_reading, frame = video_stream.read()
 
35
  video_stream.release()
36
  break
37
  frames.append(frame)
 
38
  vidname = os.path.basename(vfile).split('.')[0]
39
  dirname = vfile.split('/')[-2]
 
40
  fulldir = path.join(args.preprocessed_root, dirname, vidname)
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
48
  if f is None:
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]
55
  dirname = vfile.split('/')[-2]
 
56
  fulldir = path.join(args.preprocessed_root, dirname, vidname)
57
  os.makedirs(fulldir, exist_ok=True)
 
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
64
  try:
 
67
  exit(0)
68
  except:
69
  traceback.print_exc()
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