File size: 13,809 Bytes
6931c7b |
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# 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 re
import cv2
import sox
import wget
import yt_dlp
import ffmpeg
import pickle
import tarfile
import warnings
import numpy as np
import pandas as pd
from tqdm import tqdm
from skimage import transform
from collections import deque
from urllib.error import HTTPError
def is_empty(path):
return any(path.iterdir()) == False
def read_txt_file(txt_filepath):
with open(txt_filepath) as fin:
return (line.strip() for line in fin.readlines())
def write_txt_file(lines, out_txt_filepath):
with open(out_txt_filepath, "w") as fout:
fout.writelines("\n".join([ln.strip() for ln in lines]))
def normalize_text(text):
PUNCS = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~؟؛,’‘×÷"
# remove sound-effect description
text = re.sub(r"\([^)]*\)", "", text)
# remove punctuations
text = text.translate(str.maketrans("", "", PUNCS))
# normalize case
text = text.lower()
return text.strip()
def download_file(url, download_path):
filename = url.rpartition("/")[-1]
if not (download_path / filename).exists():
try:
# download file
print(f"Downloading {filename} from {url}")
custom_bar = (
lambda current, total, width=80: wget.bar_adaptive(
round(current / 1024 / 1024, 2),
round(total / 1024 / 1024, 2),
width,
)
+ " MB"
)
wget.download(url, out=str(download_path / filename), bar=custom_bar)
except Exception as e:
message = f"Downloading {filename} failed!"
raise HTTPError(e.url, e.code, message, e.hdrs, e.fp)
return True
def extract_tgz(tgz_filepath, extract_path, out_filename=None):
if not tgz_filepath.exists():
raise FileNotFoundError(f"{tgz_filepath} is not found!!")
tgz_filename = tgz_filepath.name
tgz_object = tarfile.open(tgz_filepath)
if not out_filename:
out_filename = tgz_object.getnames()[0]
# check if file is already extracted
if not (extract_path / out_filename).exists():
for mem in tqdm(tgz_object.getmembers(), desc=f"Extracting {tgz_filename}"):
out_filepath = extract_path / mem.get_info()["name"]
if mem.isfile() and not out_filepath.exists():
tgz_object.extract(mem, path=extract_path)
tgz_object.close()
def download_extract_file_if_not(url, tgz_filepath, download_filename):
download_path = tgz_filepath.parent
if not tgz_filepath.exists():
# download file
download_file(url, download_path)
# extract file
extract_tgz(tgz_filepath, download_path, download_filename)
def load_meanface_metadata(metadata_path):
mean_face_filepath = metadata_path / "20words_mean_face.npy"
if not mean_face_filepath.exists():
download_file(
"https://dl.fbaipublicfiles.com/muavic/metadata/20words_mean_face.npy",
metadata_path,
)
return np.load(mean_face_filepath)
def load_video_metadata(filepath):
if not filepath.exists():
# download & extract file
lang_dir = filepath.parent.parent
lang = lang_dir.name
tgz_filepath = lang_dir.parent / f"{lang}_metadata.tgz"
download_extract_file_if_not(
url=f"https://dl.fbaipublicfiles.com/muavic/metadata/{lang}_metadata.tgz",
tgz_filepath=tgz_filepath,
download_filename=lang
)
if not filepath.exists():
# file doesn't have metadata
return None
assert filepath.exists(), f"{filepath} should've been downloaded!"
with open(filepath, "rb") as fin:
metadata = pickle.load(fin)
return metadata
def download_video_from_youtube(download_path, yt_id):
"""Downloads a video from YouTube given its id on YouTube"""
video_out_path = download_path / f"{yt_id}.mp4"
if video_out_path.exists():
downloaded = True
else:
url = f"https://www.youtube.com/watch?v={yt_id}"
# downloads the best `mp4` audio/video resolution.
# TODO: download only video (no audio)
ydl_opts = {"quiet": True, "format": "mp4", "outtmpl": str(video_out_path)}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
ydl.download([url])
downloaded = True
except yt_dlp.utils.DownloadError:
downloaded = False
return downloaded
# def save_video(frames, out_filepath, fps):
# height, width, _ = frames[0].shape
# writer = cv2.VideoWriter(
# filename=out_filepath,
# fourcc=cv2.VideoWriter_fourcc(*'mp4v'),
# fps=float(fps),
# frameSize=(width, height)
# )
# for frame in frames:
# writer.write(frame)
# writer.release()
def resize_frames(input_frames, new_size):
resized_frames = []
for frame in input_frames:
try:
resized_frames.append(cv2.resize(frame, new_size))
except:
pass #some frames are corrupt or missing
return resized_frames
def get_audio_duration(audio_filepath):
return sox.file_info.duration(audio_filepath)
def get_video_duration(video_filepath):
try:
streams = ffmpeg.probe(video_filepath)["streams"]
for stream in streams:
if stream["codec_type"] == "video":
return float(stream["duration"])
except:
warnings.warn(f"Video file: `{video_filepath}` is corrupted... skipping!!")
return -1
def get_video_resolution(video_filepath):
for stream in ffmpeg.probe(video_filepath)["streams"]:
if stream["codec_type"] == "video":
height = int(stream["height"])
width = int(stream["width"])
return height, width
raise TypeError(f"Input file: {video_filepath} doesn't have video stream!")
def get_audio_video_info(audio_path, video_path, fid):
audio_filepath = audio_path / f"{fid}.wav"
video_filepath = video_path / f"{fid}.mp4"
audio_frames = (
int(get_audio_duration(audio_filepath) * 16_000)
if audio_filepath.exists()
else -1
)
video_frames = (
int(get_video_duration(video_filepath) * 25) if video_filepath.exists() else -1
)
return {
"id": fid,
"video": str(video_filepath),
"audio": str(audio_filepath),
"video_frames": video_frames,
"audio_samples": audio_frames,
}
def split_video_to_frames(video_filepath, fstart=None, fend=None, out_fps=25):
# src: https://github.com/kylemcdonald/python-utils/blob/master/ffmpeg.py
#NOTE: splitting video into frames is faster on CPU than GPU
width, height = get_video_resolution(video_filepath)
video_stream = ffmpeg.input(str(video_filepath)).video.filter("fps", fps=out_fps)
channels = 3
try:
if fstart is not None and fend is not None:
process = (
video_stream.trim(start_frame=fstart, end_frame=fend)
.setpts("PTS-STARTPTS")
.output("pipe:", format="rawvideo", pix_fmt="bgr24")
.run_async(pipe_stdout=True, quiet=True)
)
frames_counter = 0
while frames_counter < fend - fstart:
in_bytes = process.stdout.read(width * height * channels)
in_frame = np.frombuffer(in_bytes, np.uint8).reshape(
width, height, channels
)
yield in_frame
frames_counter += 1
else:
process = (
video_stream.setpts("PTS-STARTPTS")
.output("pipe:", format="rawvideo", pix_fmt="bgr24")
.run_async(pipe_stdout=True, quiet=True)
)
while True:
in_bytes = process.stdout.read(width * height * channels)
if not in_bytes:
break
in_frame = np.frombuffer(in_bytes, np.uint8).reshape(
width, height, channels
)
yield in_frame
finally:
process.stdout.close()
process.wait()
def save_video(frames, out_filepath, fps, vcodec="libx264"):
if len(frames) == 0:
warnings.warn(
f"Video segment `{out_filepath.stem}` has no metadata..." +
" skipping!!"
)
return
height, width, _ = frames[0].shape
process = (
ffmpeg.input(
"pipe:", format="rawvideo", pix_fmt="bgr24", s="{}x{}".format(width, height)
)
.output(str(out_filepath), pix_fmt="bgr24", vcodec=vcodec, r=fps)
.overwrite_output()
.run_async(pipe_stdin=True, quiet=True)
)
for _, frame in enumerate(frames):
try:
process.stdin.write(frame.astype(np.uint8).tobytes())
except:
print(process.stderr.read())
process.stdin.close()
process.wait()
def load_video(filename):
cap = cv2.VideoCapture(filename)
while cap.isOpened():
ret, frame = cap.read() # BGR
if ret:
yield frame
else:
break
cap.release()
def warp_img(src, dst, img, std_size):
tform = transform.estimate_transform(
"similarity", src, dst
) # find the transformation matrix
warped = transform.warp(
img, inverse_map=tform.inverse, output_shape=std_size
) # warp
warped = warped * 255 # note output from wrap is double image (value range [0,1])
warped = warped.astype("uint8")
return warped, tform
def apply_transform(trans, img, std_size):
warped = transform.warp(img, inverse_map=trans.inverse, output_shape=std_size)
warped = warped * 255 # note output from warp is double image (value range [0,1])
warped = warped.astype("uint8")
return warped
def cut_patch(img, metadata, height, width, threshold=5):
center_x, center_y = np.mean(metadata, axis=0)
if center_y - height < 0:
center_y = height
if center_y - height < 0 - threshold:
raise Exception("too much bias in height")
if center_x - width < 0:
center_x = width
if center_x - width < 0 - threshold:
raise Exception("too much bias in width")
if center_y + height > img.shape[0]:
center_y = img.shape[0] - height
if center_y + height > img.shape[0] + threshold:
raise Exception("too much bias in height")
if center_x + width > img.shape[1]:
center_x = img.shape[1] - width
if center_x + width > img.shape[1] + threshold:
raise Exception("too much bias in width")
cutted_img = np.copy(
img[
int(round(center_y) - round(height)) : int(round(center_y) + round(height)),
int(round(center_x) - round(width)) : int(round(center_x) + round(width)),
]
)
return cutted_img
def crop_patch(
video_frames,
num_frames,
metadata,
mean_face_metadata,
std_size=(256, 256),
window_margin=12,
start_idx=48,
stop_idx=68,
crop_height=96,
crop_width=96,
):
"""Crop mouth patch"""
stablePntsIDs = [33, 36, 39, 42, 45]
margin = min(num_frames, window_margin)
q_frame, q_metadata = deque(), deque()
sequence = []
for frame_idx, frame in enumerate(video_frames):
if frame_idx >= len(metadata):
break #! Sadly, this is necessary
q_metadata.append(metadata[frame_idx])
q_frame.append(frame)
if len(q_frame) == margin:
smoothed_metadata = np.mean(q_metadata, axis=0)
cur_metadata = q_metadata.popleft()
cur_frame = q_frame.popleft()
# -- affine transformation
trans_frame, trans = warp_img(
smoothed_metadata[stablePntsIDs, :],
mean_face_metadata[stablePntsIDs, :],
cur_frame,
std_size,
)
trans_metadata = trans(cur_metadata)
# -- crop mouth patch
sequence.append(
cut_patch(
trans_frame,
trans_metadata[start_idx:stop_idx],
crop_height // 2,
crop_width // 2,
)
)
while q_frame:
cur_frame = q_frame.popleft()
# -- transform frame
trans_frame = apply_transform(trans, cur_frame, std_size)
# -- transform metadata
trans_metadata = trans(q_metadata.popleft())
# -- crop mouth patch
sequence.append(
cut_patch(
trans_frame,
trans_metadata[start_idx:stop_idx],
crop_height // 2,
crop_width // 2,
)
)
return sequence
def read_av_manifest(tsv_filepath):
with open(tsv_filepath) as fin:
res = []
for ln in fin.readlines()[1:]:
id_, video, audio, video_frames, audio_samples = ln.strip().split("\t")
res.append(
{
"id": id_,
"video": video,
"audio": audio,
"video_frames": video_frames,
"audio_samples": audio_samples,
}
)
df = pd.DataFrame(res)
df["video_frames"] = df["video_frames"].astype(int)
df["audio_samples"] = df["audio_samples"].astype(int)
return df
def write_av_manifest(df, out_filepath):
with open(out_filepath, "w") as fout:
fout.write("/\n")
df.to_csv(out_filepath, sep="\t", header=False, index=False, mode="a")
|