Commit
·
ccc4e99
1
Parent(s):
ade4ac9
Changed OpenCV2 for decord in extracting videoframes
Browse files- handler.py +30 -1
- requirements.txt +1 -0
handler.py
CHANGED
|
@@ -56,7 +56,7 @@ class EndpointHandler:
|
|
| 56 |
print(f"Error downloading the video: {e}")
|
| 57 |
return None, None
|
| 58 |
|
| 59 |
-
def
|
| 60 |
self, video_bytes: bytes, num_frames: int = 32
|
| 61 |
) -> list:
|
| 62 |
# Write bytes to a temporary file
|
|
@@ -88,6 +88,35 @@ class EndpointHandler:
|
|
| 88 |
|
| 89 |
return frames
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
def preprocess_frames(self, video_frames):
|
| 92 |
"""
|
| 93 |
Define a preprocessing function to convert video frames into a format suitable for the model
|
|
|
|
| 56 |
print(f"Error downloading the video: {e}")
|
| 57 |
return None, None
|
| 58 |
|
| 59 |
+
def extract_evenly_spaced_frames_from_bytes_cv2(
|
| 60 |
self, video_bytes: bytes, num_frames: int = 32
|
| 61 |
) -> list:
|
| 62 |
# Write bytes to a temporary file
|
|
|
|
| 88 |
|
| 89 |
return frames
|
| 90 |
|
| 91 |
+
from decord import VideoReader
|
| 92 |
+
from decord import cpu
|
| 93 |
+
|
| 94 |
+
def extract_evenly_spaced_frames_from_bytes(
|
| 95 |
+
self, video_bytes: bytes, num_frames: int = 32
|
| 96 |
+
) -> list:
|
| 97 |
+
# Create a VideoReader object from bytes
|
| 98 |
+
vr = VideoReader(video_bytes, ctx=cpu(0))
|
| 99 |
+
|
| 100 |
+
# Get the total number of frames in the video
|
| 101 |
+
total_frames = len(vr)
|
| 102 |
+
|
| 103 |
+
# Calculate the interval at which frames should be extracted
|
| 104 |
+
interval = total_frames // num_frames
|
| 105 |
+
|
| 106 |
+
frames = []
|
| 107 |
+
|
| 108 |
+
for i in range(num_frames):
|
| 109 |
+
# Seek to the next frame to be captured
|
| 110 |
+
frame_index = min(i * interval, total_frames - 1)
|
| 111 |
+
|
| 112 |
+
# Read the frame
|
| 113 |
+
frame = vr[frame_index].asnumpy()
|
| 114 |
+
|
| 115 |
+
# Add the frame to the list
|
| 116 |
+
frames.append(frame)
|
| 117 |
+
|
| 118 |
+
return frames
|
| 119 |
+
|
| 120 |
def preprocess_frames(self, video_frames):
|
| 121 |
"""
|
| 122 |
Define a preprocessing function to convert video frames into a format suitable for the model
|
requirements.txt
CHANGED
|
@@ -23,3 +23,4 @@ tqdm==4.66.1
|
|
| 23 |
transformers==4.27.2
|
| 24 |
typing_extensions==4.8.0
|
| 25 |
urllib3==2.0.7
|
|
|
|
|
|
| 23 |
transformers==4.27.2
|
| 24 |
typing_extensions==4.8.0
|
| 25 |
urllib3==2.0.7
|
| 26 |
+
decord==0.6.0
|