File size: 1,325 Bytes
151e21a | 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 | import cv2
import numpy as np
class VideoProcessor:
def __init__(self, mp4):
self.mp4 = mp4
self.video_frames = []
def extract_frames(self, ratio):
# Extracts frames from video
# ratio: ratio of frames to extract
vid_cap = cv2.VideoCapture(self.mp4)
frame_count = int(vid_cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_number = 0
while vid_cap.isOpened():
ret, frame = vid_cap.read()
if ret:
if frame_number % ratio == 0:
self.video_frames.append(frame)
frame_number += 1
else:
break
def resize_and_normalize_frames(self, width, height, normalizing_const=255.0):
# Resizes frames
# width: width of frame
# height: height of frame
resized_normal_frames = []
for frame in self.video_frames:
frame = cv2.resize(frame, (width, height))
frame = frame.astype(np.float32) / normalizing_const
resized_normal_frames.append(frame)
self.video_frames = resized_normal_frames
def get_frames(self):
# Returns: numpy array of frames
if self.video_frames != []:
return np.array(self.video_frames)
else:
return None
|