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'.
我在同一个文件夹中有一个python脚本,我想把它转换成一个numpy数组。我的视频叫“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.
在我的脚本中,我想调用someFunction('test.mp4')并返回一个numpy数组。生成的numpy数组应该是一个numpy图像数组,其中每个图像都是一个3-d numpy数组。
Does that make sense?
这说得通吗?
Thanks!
谢谢!
2 个解决方案
#1
4
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
1
skvideo is a python package can be used to read video and stores into the multi-dimensional array.
skvideo是一个python包,可以用来读取视频并存储到多维数组中。
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
更多细节:http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html
#1
4
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
1
skvideo is a python package can be used to read video and stores into the multi-dimensional array.
skvideo是一个python包,可以用来读取视频并存储到多维数组中。
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
更多细节:http://mllearners.blogspot.in/2018/01/scikit-video-skvideo-tutorial-for.html