-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
312 lines (248 loc) · 8.4 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
305
306
307
308
309
310
311
312
import gc
import os
import os.path as osp
import re
import warnings
from pathlib import Path
import cv2
import ffmpeg
import imageio
import numpy as np
import torch
from tqdm import tqdm
warnings.filterwarnings("ignore")
def clean_mem():
torch.cuda.empty_cache()
gc.collect()
def video2frames(video_path, output_dir):
reader = imageio.get_reader(video_path)
meta_data = reader.get_meta_data()
fps = meta_data["fps"]
size = meta_data["size"]
extract_raw_frames(video_path, output_dir)
name = Path(video_path).name
return {"fps": fps, "name": name, "size": size, "video_path": video_path}
def load_DAIN():
# Let the magic happen
from DAIN.DAIN import DAIN
module = DAIN()
# load the weights online
from torch.hub import load_state_dict_from_url
state_dict = load_state_dict_from_url(
"http://vllab1.ucmerced.edu/~wenbobao/DAIN/best.pth"
)
module.load_state_dict(state_dict)
return module
def infer_DAIN(model, meta_data, inframes, outframes, use_gpu=True, scale_precent=100):
"""
Interpolate frames using DAIN
Args
---
model(nn.Module): DAIN Module for processing
meta_data(dict): info about the video like (size and fps)
inframes(str, Path): input frames directory
outframes(str, Path): output frames directory
use_gpu(bool): choosing between GPU and CPU
scale(int): how much to reduce the original size of the frames (it's reversed at the end)
"""
device = "cuda" if torch.cuda.is_available() and use_gpu else "cpu"
model.to(device)
inframes = Path(inframes)
outframes = str(outframes)
frames = sorted(inframes.glob("*"))
width = int(meta_data["size"][0] * scale_precent / 100)
height = int(meta_data["size"][1] * scale_precent / 100)
dim = (width, height)
model.eval()
j = 0
for i in tqdm(range(len(frames) - 1)):
image1 = cv2.resize(
imageio.imread(frames[i]), dim, interpolation=cv2.INTER_AREA
)
image2 = cv2.resize(
imageio.imread(frames[i + 1]), dim, interpolation=cv2.INTER_AREA
)
X0 = torch.from_numpy(np.transpose(image1, (2, 0, 1)).astype("float32") / 255.0)
X1 = torch.from_numpy(np.transpose(image2, (2, 0, 1)).astype("float32") / 255.0)
y_ = torch.FloatTensor()
intWidth = X0.size(2)
intHeight = X0.size(1)
channel = X0.size(0)
if intWidth != ((intWidth >> 7) << 7):
intWidth_pad = ((intWidth >> 7) + 1) << 7 # more than necessary
intPaddingLeft = int((intWidth_pad - intWidth) / 2)
intPaddingRight = intWidth_pad - intWidth - intPaddingLeft
else:
intWidth_pad = intWidth
intPaddingLeft = 32
intPaddingRight = 32
if intHeight != ((intHeight >> 7) << 7):
intHeight_pad = ((intHeight >> 7) + 1) << 7 # more than necessary
intPaddingTop = int((intHeight_pad - intHeight) / 2)
intPaddingBottom = intHeight_pad - intHeight - intPaddingTop
else:
intHeight_pad = intHeight
intPaddingTop = 32
intPaddingBottom = 32
pader = torch.nn.ReplicationPad2d(
[intPaddingLeft, intPaddingRight, intPaddingTop, intPaddingBottom]
)
torch.set_grad_enabled(False)
X0 = torch.unsqueeze(X0, 0)
X1 = torch.unsqueeze(X1, 0)
X0 = pader(X0).to(device)
X1 = pader(X1).to(device)
y_s, offset, filter = model(torch.stack((X0, X1), dim=0))
y_ = y_s[1]
X0 = X0.data.cpu().numpy()
y_ = y_.data.cpu().numpy()
offset = [offset_i.data.cpu().numpy() for offset_i in offset]
filter = (
[filter_i.data.cpu().numpy() for filter_i in filter]
if filter[0] is not None
else None
)
X1 = X1.data.cpu().numpy()
X0 = np.transpose(
255.0
* X0.clip(0, 1.0)[
0,
:,
intPaddingTop : intPaddingTop + intHeight,
intPaddingLeft : intPaddingLeft + intWidth,
],
(1, 2, 0),
)
y_ = np.transpose(
255.0
* y_.clip(0, 1.0)[
0,
:,
intPaddingTop : intPaddingTop + intHeight,
intPaddingLeft : intPaddingLeft + intWidth,
],
(1, 2, 0),
)
offset = [
np.transpose(
offset_i[
0,
:,
intPaddingTop : intPaddingTop + intHeight,
intPaddingLeft : intPaddingLeft + intWidth,
],
(1, 2, 0),
)
for offset_i in offset
]
filter = (
[
np.transpose(
filter_i[
0,
:,
intPaddingTop : intPaddingTop + intHeight,
intPaddingLeft : intPaddingLeft + intWidth,
],
(1, 2, 0),
)
for filter_i in filter
]
if filter is not None
else None
)
X1 = np.transpose(
255.0
* X1.clip(0, 1.0)[
0,
:,
intPaddingTop : intPaddingTop + intHeight,
intPaddingLeft : intPaddingLeft + intWidth,
],
(1, 2, 0),
)
imageio.imsave(
os.path.join(outframes, str(j).zfill(6) + ".jpg"),
cv2.resize(image1, meta_data["size"], interpolation=cv2.INTER_AREA),
)
imageio.imsave(
os.path.join(outframes, str(j + 1).zfill(6) + ".jpg"),
cv2.resize(
np.round(y_).astype(np.uint8),
meta_data["size"],
interpolation=cv2.INTER_AREA,
),
)
j = j + 2
imageio.imsave(
os.path.join(outframes, str(j).zfill(6) + ".jpg"),
cv2.resize(image2, meta_data["size"], interpolation=cv2.INTER_AREA),
)
meta_data["fps"] = meta_data["fps"] * 2
return meta_data
def download_video_from_url(source_url, source_path, quality):
import youtube_dl
source_path = Path(source_path)
if source_path.exists():
source_path.unlink()
ydl_opts = {
"format": "bestvideo[height<={}][ext=mp4]+bestaudio[ext=m4a]/mp4".format(
quality
),
"outtmpl": str(source_path),
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([source_url])
def purge_images(dir):
for f in os.listdir(dir):
if re.search(".*?\.jpg", f):
os.remove(os.path.join(dir, f))
def extract_raw_frames(video_path, save_path):
"""
Extracts frames from a video to a specified path
Args
---
video_path(str, Path): absolute or a relative path for the video
save_path(str, Path): directory to extract frames in
"""
inframes_root = Path(save_path)
inframe_path_template = str(inframes_root / "%6d.jpg")
ffmpeg.input(str(video_path)).output(
str(inframe_path_template),
format="image2",
vcodec="mjpeg",
qscale=0,
start_number=0,
).run(capture_stdout=True)
def build_video(frames_dir, save_dir, meta_data, audio_path=None):
"""
Construct video from frames
Args
----
frames_dir(str, Path): folder path to frames
save_dir(str, Path):
"""
save_dir = Path(save_dir)
out_path = str(save_dir / meta_data["name"])
frames_dir = Path(frames_dir)
inp_path = str(frames_dir / '%6d.jpg')
# Use ffmpeg to reconstruct the video
ffmpeg.input(
inp_path, format="image2", vcodec="mjpeg", framerate=meta_data["fps"]
).output(out_path, crf=17, vcodec="libx264").run(capture_stdout=True)
def get_thumbnail(video_path):
cwd = os.getcwd()
thumb_path = osp.join(cwd, "thumb.jpg")
if osp.exists(thumb_path):
os.remove(thumb_path)
command = (
'ffmpeg -i "'
+ video_path
+ '" -ss 3 -vf "select=gt(scene\\,0.4)" -frames:v 5 -vsync vfr -vf fps=1/6 thumb.jpg'
)
os.system(command)
def multiply_nameby2(frames_path):
frames = sorted(frames_path.glob("*.jpg"))
for frame in frames:
x = str(int(frame.stem) * 2).zfill(5) + ".jpg"
frame.rename(frames_path / x)