python - 如何将视频转换为 numpy 数组?

标签 python python-3.x numpy machine-learning opencv3.0

关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

7 个月前关闭。




Improve this question




如何将视频转换为多个 numpy 数组或单个数组以将其用于机器学习。我只找到了在图像上执行此操作的方法。

最佳答案

常规图像表示为具有以下形状的 3D 张量:(height, width, channels) .如果图像是 RGB,则 channel 值为 3,如果是灰度,则 channel 值为 1。
视频是 N 帧的集合,其中每一帧是一个图像。您想将此数据表示为 4D 张量:(frames, height, width, channels) .
因此,例如,如果您有 1 分钟的 30 fps 视频,其中每帧为 RGB,分辨率为 256x256,那么您的张量将如下所示:(1800, 256, 256, 3) ,其中 1800 是视频中的帧数:30 (fps) * 60 (秒)。
为此,您基本上可以打开视频的每个单独帧,将它们全部存储在一个列表中,然后沿着新轴(即“帧”维度)将它们连接在一起。

你可以通过 OpenCV 做到这一点:

# Import the video and cut it into frames.
vid = cv2.VideoCapture('path/to/video/file')

frames = []
check = True
i = 0

while check:
    check, arr = vid.read()
    if not i % 20:  # This line is if you want to subsample your video
                    # (i.e. keep one frame every 20)
        frames.append(arr)
    i += 1

frames = np.array(frames)  # convert list of frames to numpy array

关于python - 如何将视频转换为 numpy 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67644826/

相关文章:

python - 将结果转换为对象数组 - Python

python - 'str' 对象没有属性 'decode'

python - 将 numpy 数组转换为给定格式的字符串

python - 为什么我的 3D numpy 数组中的值在我将其写入文件时会发生变化?

python - 计算多个词典之间的相似度 "score"

python - SciPy NumPy 和 SciKit-learn ,创建一个稀疏矩阵

Python Pandas : removing rows not matching multiple conditions from dataframe

python - 试图在 Instagram API 中查找关注者数量

python - 如何在停机时间最短的情况下将 DynamoDB 表迁移到全局 DynamoDB 表?

windows - 为什么对象的 id 会根据 python shell 中的行而改变