Delete src/utils.py
Browse files- src/utils.py +0 -50
src/utils.py
DELETED
@@ -1,50 +0,0 @@
|
|
1 |
-
# Importing the requirements
|
2 |
-
from PIL import Image
|
3 |
-
from decord import VideoReader, cpu
|
4 |
-
|
5 |
-
|
6 |
-
# Maximum number of frames to use
|
7 |
-
MAX_NUM_FRAMES = 64 # If CUDA OOM, set a smaller number
|
8 |
-
|
9 |
-
|
10 |
-
def encode_video(video_path):
|
11 |
-
"""
|
12 |
-
Encodes a video file into a list of frames.
|
13 |
-
|
14 |
-
Args:
|
15 |
-
video_path (str): The path to the video file.
|
16 |
-
|
17 |
-
Returns:
|
18 |
-
list: A list of frames, where each frame is represented as an Image object.
|
19 |
-
"""
|
20 |
-
|
21 |
-
def uniform_sample(l, n):
|
22 |
-
"""
|
23 |
-
Uniformly samples elements from a list.
|
24 |
-
|
25 |
-
Args:
|
26 |
-
- l (list): The input list.
|
27 |
-
- n (int): The number of elements to sample.
|
28 |
-
|
29 |
-
Returns:
|
30 |
-
list: A list of sampled elements.
|
31 |
-
"""
|
32 |
-
gap = len(l) / n
|
33 |
-
idxs = [int(i * gap + gap / 2) for i in range(n)]
|
34 |
-
return [l[i] for i in idxs]
|
35 |
-
|
36 |
-
# Read the video file and sample frames
|
37 |
-
vr = VideoReader(video_path, ctx=cpu(0))
|
38 |
-
sample_fps = round(vr.get_avg_fps() / 1) # FPS
|
39 |
-
frame_idx = [i for i in range(0, len(vr), sample_fps)]
|
40 |
-
|
41 |
-
# Uniformly sample frames if the number of frames is too large
|
42 |
-
if len(frame_idx) > MAX_NUM_FRAMES:
|
43 |
-
frame_idx = uniform_sample(frame_idx, MAX_NUM_FRAMES)
|
44 |
-
|
45 |
-
# Extract frames from the video
|
46 |
-
frames = vr.get_batch(frame_idx).asnumpy()
|
47 |
-
frames = [Image.fromarray(v.astype("uint8")) for v in frames]
|
48 |
-
|
49 |
-
# Return video frames
|
50 |
-
return frames
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|