Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
from einops import rearrange
|
4 |
+
from decord import VideoReader
|
5 |
+
|
6 |
+
num_frames = 16
|
7 |
+
input_size = 224
|
8 |
+
patch_size = (16, 16)
|
9 |
+
IMAGENET_MEAN = np.array([0.45, 0.45, 0.45])
|
10 |
+
IMAGENET_STD = np.array([0.225, 0.225, 0.225])
|
11 |
+
|
12 |
+
def format_frames(frame, output_size):
|
13 |
+
frame = tf.image.convert_image_dtype(frame, tf.uint8)
|
14 |
+
frame = tf.image.resize(frame, size=output_size)
|
15 |
+
frame = frame / 255.
|
16 |
+
frame = frame - IMAGENET_MEAN
|
17 |
+
frame = frame / IMAGENET_STD
|
18 |
+
return frame
|
19 |
+
|
20 |
+
def read_video(file_path):
|
21 |
+
container = VideoReader(file_path)
|
22 |
+
return container
|
23 |
+
|
24 |
+
def frame_sampling(container, num_frames):
|
25 |
+
interval = len(container) // num_frames
|
26 |
+
bids = np.arange(num_frames) * interval
|
27 |
+
offset = np.random.randint(interval, size=bids.shape)
|
28 |
+
frame_index = bids + offset
|
29 |
+
frames = container.get_batch(frame_index).asnumpy()
|
30 |
+
frames = np.stack(frames)
|
31 |
+
frames = format_frames(frames, [input_size] * 2)
|
32 |
+
return frames
|
33 |
+
|
34 |
+
def denormalize(image):
|
35 |
+
image = image.numpy() if not isinstance(image, np.ndarray) else image
|
36 |
+
image = image * IMAGENET_STD + IMAGENET_MEAN
|
37 |
+
image = (image * 255).clip(0, 255).astype('uint8')
|
38 |
+
return image
|