首页 > 解决方案 > 将图像转换为视频会抛出内存不足错误,即使使用的内存在 8GB 左右,但我有 64GB

问题描述

尝试将图像转换为视频但收到此错误,

文件“C:/test.py”,第 35 行,在 convert_frames_to_video(pathIn, pathOut, fps) 文件“C:/test.py”,第 17 行,在 convert_frames_to_video img = cv2.imread(filename) cv2.error: OpenCV (4.0.0) C:\projects\opencv- python\opencv\modules\core\src\alloc.cpp:55: error: (-4:Insufficient memory) 无法在函数 'cv::OutOfMemoryError' 中分配 72000000 字节

任务管理器中已用内存为 8GB 总内存为 64GB...我正在使用 PyCharm IDE 运行此代码,

import cv2
import numpy as np
import os

from os.path import isfile, join

def convert_frames_to_video(pathIn, pathOut, fps):
    frame_array = []
    files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]

    for i in range(len(files)):
        filename = pathIn + "\\" + files[i]
        # reading each files
        img = cv2.imread(filename)
        print(filename)
        height, width, layers = img.shape
        size = (width, height)
        print(filename)
        # inserting the frames into an image array
        frame_array.append(img)

    out = cv2.VideoWriter(pathOut, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

    for i in range(len(frame_array)):
        # writing to a image array
        out.write(frame_array[i])
    out.release()

pathIn = 'C:\\Images'
pathOut = 'video.avi'
fps = 25.0
convert_frames_to_video(pathIn, pathOut, fps)

标签: pythonopencvout-of-memory

解决方案


推荐阅读