如何将视频转换为numpy数组? [英] How to turn a video into numpy array?

查看:617
本文介绍了如何将视频转换为numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与要转换为numpy数组的视频相同的文件夹中有一个python脚本.我的视频叫做"test.mp4".

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'.

在我的脚本中,我想调用someFunction('test.mp4')并返回一个numpy数组.生成的numpy数组应该是一个图像的numpy数组,其中每个图像都是一个3-d的numpy数组.

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.

这有意义吗?

谢谢!

推荐答案

下面的脚本可以满足您的需求.您可以将它的一部分分成函数.

The script below does what you want. You may separate part of it into the function.

下面的代码不检查错误,特别是生产代码将检查每个frame*变量是否大于零.

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)

这篇关于如何将视频转换为numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆