How to turn a video into numpy array?

2020-02-05 08:38发布

问题:

I have a python script in the same folder as a video I want to convert to a numpy array. My video is called 'test.mp4'.

Within my script, I want to call someFunction('test.mp4') and get back a numpy array. The resulting numpy array should be a numpy array of images, where each image is a 3-d numpy array.

Does that make sense?

Thanks!

回答1:

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)


回答2:

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://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html