How to turn a video into numpy array?
Solution 1:
skvideo is a python package can be used to read video and stores into the multi-dimensional array.
import skvideo.io
videodata = skvideo.io.vread("video_file_name")
print(videodata.shape)
For more details: http://www.scikit-video.org/stable/index.html and http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html
Solution 2:
The script below does what you want. You may separate part of it into the function.
Code below doesn't check for errors, in particular, production code will check that every frame*
variable is greater than zero.
import cv2
import numpy as np
cap = cv2.VideoCapture('test.mp4')
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))
fc = 0
ret = True
while (fc < frameCount and ret):
ret, buf[fc] = cap.read()
fc += 1
cap.release()
cv2.namedWindow('frame 10')
cv2.imshow('frame 10', buf[9])
cv2.waitKey(0)