forked from facebookresearch/muavic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
304 lines (268 loc) · 9.53 KB
/
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
import wget
import yt_dlp
import ffmpeg
import pickle
import tarfile
import numpy as np
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 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),
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):
if not tgz_filepath.exists():
raise FileNotFoundError(f"{tgz_filepath} is not found!!")
tgz_filename = tgz_filepath.name
tgz_object = tarfile.open(tgz_filepath)
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_path = tgz_filepath.parent
if not tgz_filepath.exists():
# download file
download_file(url, download_path)
# extract file
extract_tgz(tgz_filepath, download_path)
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(
f"https://dl.fbaipublicfiles.com/muavic/metadata/{lang}_metadata.tgz",
tgz_filepath
)
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 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 split_video_to_frames(video_filepath, fstart, fend, out_fps=25):
# src: https://github.com/kylemcdonald/python-utils/blob/master/ffmpeg.py
# in_params = {}
# if hwaccel is not None:
# in_params['hwaccel'] = hwaccel
width, height = get_video_resolution(video_filepath)
video_stream = (
ffmpeg
.input(str(video_filepath)).video
.filter('fps', fps=out_fps)
)
channels = 3
frames_counter = 0
try:
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)
)
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
finally:
process.stdout.close()
process.wait()
def save_video(frames, out_filepath, fps, vcodec='libx264'):
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):
process.stdin.write(
frame
.astype(np.uint8)
.tobytes()
)
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,
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):
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