-
Notifications
You must be signed in to change notification settings - Fork 32
/
mtedx_utils.py
350 lines (325 loc) · 14 KB
/
mtedx_utils.py
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
346
347
348
349
350
# Copyright (c) Meta Platforms, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import os
import cv2
import sox
import shutil
import tempfile
import warnings
import pandas as pd
from functools import partial
from collections import defaultdict, OrderedDict
from tqdm.contrib.concurrent import process_map
from utils import *
# define global constants
SPLITS = ["train", "valid", "test"]
def download_mtedx_data(download_path, src, tgt):
"""Downloads mTEDx data from OpenSLR"""
tgz_filename = f"mtedx_{src}-{tgt}.tgz" if src != tgt else f"mtedx_{src}.tgz"
download_extract_file_if_not(
url=f"https://www.openslr.org/resources/100/{tgz_filename}",
tgz_filepath=download_path / tgz_filename,
download_filename=f"{src}-{tgt}"
)
def download_mtedx_lang_videos(mtedx_path, src_lang):
# keep track of non-found videos on YouTube
try:
not_found_videos = set(read_txt_file(mtedx_path / "not_found_videos.txt"))
except FileNotFoundError:
not_found_videos = set()
# get files id per split
for split in SPLITS:
out_path = mtedx_path / "video" / src_lang / split
out_path.mkdir(parents=True, exist_ok=True)
if is_empty(out_path): #TODO: better check
if split == "train":
print(f"\nDownloading {src_lang} videos from YouTube")
# get youtube-ids from audio filenames inside `wav` directory
wav_dir_path = (
mtedx_path / f"{src_lang}-{src_lang}" / "data" / split / "wav"
)
yt_ids = [wav_filepath.stem for wav_filepath in wav_dir_path.glob("*")]
# download videos from YouTube
downloading_status = process_map(
partial(download_video_from_youtube, out_path),
yt_ids,
max_workers=os.cpu_count(),
desc=f"Downloading {src_lang}/{split} Videos",
chunksize=1,
)
assert len(yt_ids) == len(downloading_status)
for yt_id, downloaded in zip(yt_ids, downloading_status):
if not downloaded:
not_found_videos.add(yt_id)
with open(mtedx_path / "not_found_videos.txt", "w") as fout:
fout.writelines([f"{id_}\n" for id_ in not_found_videos])
def segment_normalize_audio_file(out_dir, in_file_info):
out_sr = 16_000
out_channels = 1
out_format = "wav"
in_filepath, fid, seg_id, start_sec, end_sec = in_file_info
out_filepath = out_dir / fid / f"{seg_id}.{out_format}"
if not in_filepath.exists() or out_filepath.exists():
return
out_filepath.parent.mkdir(parents=True, exist_ok=True)
tfm = sox.Transformer()
tfm.set_output_format(rate=out_sr, channels=out_channels)
tfm.trim(start_sec, end_sec)
tfm.build_file(str(in_filepath), str(out_filepath))
def preprocess_mtedx_audio(mtedx_path, src_lang, muavic_path):
for split in SPLITS:
split_dir_path = mtedx_path / f"{src_lang}-{src_lang}" / "data" / split
audio_segments = list(read_txt_file(split_dir_path / "txt" / "segments"))
# create directory for segmented & normalized audio
out_path = muavic_path / src_lang / "audio" / split
out_path.mkdir(parents=True, exist_ok=True)
# early-quit condition
num_curr_audio_segments = len(list(out_path.rglob("*.wav")))
if num_curr_audio_segments == len(audio_segments):
continue # skip if all audio segments are already processed
if split == "train":
print(f"\nSegmenting {src_lang} audio files")
# collect needed info from segment file
segments_info = []
wav_dir_path = split_dir_path / "wav"
for line in audio_segments:
seg_id, fid, start, end = line.strip().split(" ")
segments_info.append(
(
wav_dir_path / (fid + ".flac"),
fid,
seg_id,
float(start),
float(end),
)
)
# preprocess audio files
process_map(
partial(segment_normalize_audio_file, out_path),
segments_info,
max_workers=os.cpu_count(),
desc=f"Preprocessing {src_lang}/{split} Audios",
chunksize=1,
)
def segment_normalize_video(mean_face_metadata, in_path, out_path, seg_info):
fps = 25
out_format = "mp4"
out_filepath = out_path / f"{seg_info['id']}.{out_format}"
# skip if file is already segmented
if out_filepath.exists():
return
# start segmenting
seg_metadata_length = len(seg_info["metadata"])
fstart = round(seg_info["start"] * fps)
fend = (
round(seg_info["end"] * fps)
if seg_metadata_length == 0
else fstart + seg_metadata_length
) # handles minor mismatches between metadata-length and video-length
if fstart == fend:
fend = fstart + 1 # adding 1-frame
num_frames = fend - fstart
vid_frames = (
cv2.imread(f"{in_path}/{i}.png")
for i in range(fstart, fend)
if os.path.exists(f"{in_path}/{i}.png")
)
if seg_metadata_length > 0:
# detect the mouth ROI and crop it
frames = crop_patch(
vid_frames,
num_frames,
seg_info["metadata"],
mean_face_metadata,
)
else:
# resize the frames (since there were no metadata for it)
frames = resize_frames(vid_frames, new_size=(96, 96))
# save video
save_video(frames, out_filepath, fps)
def preprocess_mtedx_video(mtedx_path, metadata_path, src_lang, muavic_path):
mean_face_metadata = load_meanface_metadata(metadata_path)
for split in SPLITS:
split_dir_path = mtedx_path / f"{src_lang}-{src_lang}" / "data" / split
video_segments = list(read_txt_file(split_dir_path / "txt" / "segments"))
# create directory for segmented & normalized video
out_path = muavic_path / src_lang / "video" / split
out_path.mkdir(parents=True, exist_ok=True)
# early-quit condition (might be TOO STRICT)
num_curr_video_segments = len(list(out_path.rglob("*.mp4")))
if num_curr_video_segments == len(video_segments):
continue # skip if all video segments are already processed
if split == "train":
print(
f"\nSegmenting `{src_lang}` videos files "
+ "(It takes a few hours to complete)"
)
# collect needed info from segment file
segment_file = (
mtedx_path / f"{src_lang}-{src_lang}" / "data" / split / "txt" / "segments"
)
video_to_segments = defaultdict(list)
for line in read_txt_file(segment_file):
seg_id, fid, start_sec, end_sec = line.strip().split()
video_to_segments[fid].append(
{
"id": seg_id,
"start": float(start_sec),
"end": float(end_sec),
}
)
# sort videos by the number of segments in ascending order
video_to_segments = OrderedDict(
sorted(video_to_segments.items(), key=lambda x: len(x[1]))
)
out_fps = 25
video_format = "mp4"
in_video_dir_path = mtedx_path / "video" / src_lang / split
for video_id, video_segments in tqdm(video_to_segments.items()):
# check if video has been already processed:
all_segments_are_processed = all(
(out_path / video_id / f"{seg['id']}.{video_format}").exists()
for seg in video_segments
)
if all_segments_are_processed:
continue
# prepare to process video file
in_filepath = in_video_dir_path / f"{video_id}.{video_format}"
if not in_filepath.exists():
warnings.warn(
f"TED talk `{in_filepath.stem}` hasn't been downloaded..." +
" skipping!!"
)
continue
tmp_dir_path = tempfile.mkdtemp()
(
ffmpeg.input(str(in_filepath))
.video.filter("fps", fps=out_fps)
.output(f"{tmp_dir_path}/%d.png", start_number=0)
.run(quiet=True)
)
# load metadata
video_metadata = load_video_metadata(
metadata_path / src_lang / split / f"{video_id}.pkl"
)
if video_metadata is None:
warnings.warn(
f"TED talk `{in_filepath.stem}` doesn't have metadata..." +
" skipping!!"
)
continue
# add metadata to video_segments
for seg in video_segments:
seg["metadata"] = video_metadata.get(seg["id"], [])
# set the output path for the video segments
out_seg_path = out_path / video_id
out_seg_path.mkdir(parents=True, exist_ok=True)
# start segmenting video into smaller segments and process them
process_map(
partial(
segment_normalize_video,
mean_face_metadata,
tmp_dir_path,
out_seg_path,
),
video_segments,
max_workers=min(len(video_segments), os.cpu_count()),
chunksize=1,
disable=True, # disables progress bar
)
shutil.rmtree(tmp_dir_path)
def get_mtedx_fileids(segment_filepath):
# get segments file in mtedx dataset
fids = []
for ln in read_txt_file(segment_filepath):
seg_id, talk_id, _, _ = ln.split()
fids.append(f"{talk_id}/{seg_id}")
return fids
def prepare_mtedx_avsr_manifests(mtedx_path, lang, muavic_path):
for split in SPLITS:
out_manifest_filepath = muavic_path / lang / f"{split}.tsv"
if not out_manifest_filepath.exists():
if split == "train":
print(f"\nCreating AVSR manifests for `{lang}`")
mtedx_txt_dir_path = mtedx_path / f"{lang}-{lang}" / "data" / split / "txt"
audio_datapath = muavic_path / lang / "audio" / split
video_datapath = muavic_path / lang / "video" / split
fileids = get_mtedx_fileids(mtedx_txt_dir_path / "segments")
# get audio/video frames
av_manifest_df = pd.DataFrame(
process_map(
partial(get_audio_video_info, audio_datapath, video_datapath),
fileids,
desc=f"Creating {lang}/{split} manifest",
max_workers=os.cpu_count(),
chunksize=1,
)
)
# write down the manifest TSV file
write_av_manifest(av_manifest_df, out_manifest_filepath)
# copy language transcription
shutil.copyfile(
mtedx_txt_dir_path / f"{split}.{lang}",
muavic_path / lang / f"{split}.{lang}",
)
def prepare_mtedx_avst_manifests(mtedx_path, mt_trans_path, lang, muavic_path):
# download & extract pseudo-translation if that wasn't done already
lang_pair = f"{lang}-en"
tgz_filename = f"{lang_pair}.tgz"
tgz_filepath = mt_trans_path / tgz_filename
url = f"https://dl.fbaipublicfiles.com/muavic/mt_trans/{tgz_filename}"
download_extract_file_if_not(url, tgz_filepath, lang_pair)
for split in tqdm(SPLITS):
# set output files
out_tgt_filepath = muavic_path / lang / "en" / f"{split}_avst.en"
out_manifest_filepath = muavic_path / lang / "en" / f"{split}_avst.tsv"
out_tgt_filepath.parent.mkdir(parents=True, exist_ok=True)
if not out_tgt_filepath.exists():
if split == "train":
print(f"\nCreating AVST manifests for `{lang}-en`")
# combine human translation with MT translation for "train" & "valid"
if split != "test":
# load pseudo-translations from mt_trans_path
pseudo_trans = pd.DataFrame(
{
"id": read_txt_file(
mt_trans_path / f"{lang}-en" / f"{split}_id.txt"
),
lang: read_txt_file(
mt_trans_path / f"{lang}-en" / f"{split}.{lang}"
),
"en": read_txt_file(
mt_trans_path / f"{lang}-en" / f"{split}.en"
),
}
).set_index("id")
# load real translations from mtedx_path
mtedx_trans_path = mtedx_path / f"{lang}-en" / "data" / split / "txt"
human_trans = pd.DataFrame(
{
"id": get_mtedx_fileids(mtedx_trans_path / "segments"),
lang: read_txt_file(mtedx_trans_path / f"{split}.{lang}"),
"en": read_txt_file(mtedx_trans_path / f"{split}.en"),
}
).set_index("id")
# combine pseudo translation with human translation
pseudo_trans.update(human_trans)
write_txt_file(pseudo_trans["en"].tolist(), out_tgt_filepath)
# copy AVSR manifest to be AVST's
shutil.copyfile(
src=muavic_path / lang / f"{split}.tsv",
dst=out_manifest_filepath,
)
# use only human translation for "test"
else:
mtedx_trans_path = mtedx_path / f"{lang}-en" / "data" / split / "txt"
# copy target language
shutil.copyfile(src=mtedx_trans_path / f"test.en", dst=out_tgt_filepath)
# copy AVSR manifest to be AVST's
shutil.copyfile(
src=muavic_path / lang / f"{split}.tsv",
dst=out_manifest_filepath,
)