【问题标题】:How to avoid system kills when combining frames into one video将帧组合成一个视频时如何避免系统死机
【发布时间】:2020-07-15 05:33:03
【问题描述】:

我使用 python 将 5576 个图像组合成一个 30 fps 的视频。

我尝试了许多在互联网上发布的代码,但我总是收到此错误。

Process finished with exit code 137 (interrupted by signal 9: SIGKILL)

我查看操作系统正在杀死我的进程。

我正在使用 ubuntu、pycharm、python3.8 64 位。

import time
import cv2
import numpy as np
import glob
import re


#Natural key for natural sort
def atoi(text):
    return int(text) if text.isdigit() else text

def natural_keys(text):
    return [ atoi(c) for c in re.split(r'(\d+)', text) ]

li = []
for filename in glob.glob('det/*.jpg'):
    li.append(filename)

li.sort(key=natural_keys)

img_array = []
for filename in li:
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)
    time.sleep(0.1)
    print(f"Frame #{filename}")

out = cv2.VideoWriter('Video/label_vid.avi', cv2.VideoWriter_fourcc(*'DIVX'), 30, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

【问题讨论】:

    标签: python python-3.x opencv image-processing video


    【解决方案1】:

    在我看来,for filename in li:img = cv2.imread(filename) 中的进程一定是崩溃的。您在这里要做的是使用cv2.imread() 读取5K 图像。假设单个图像的内存大小为 1MB(这与磁盘大小不同),那么上述过程将占用 5GB 内存。在这种情况下,我的建议是,我们应该将帧直接编码到视频流中,而不是读取所有图像并将它们存储在 img_array.append(img) 中:

    out = cv2.VideoWriter('Video/label_vid.avi', cv2.VideoWriter_fourcc(*'DIVX'), 30, size)
    
    for filename in li:
        img = cv2.imread(filename)
        out.write(img)
    out.release()
    

    【讨论】:

    • 非常感谢您对代码的解释和澄清。
    • 我搜索了两天的答案。 :D
    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 2015-05-20
    • 1970-01-01
    相关资源
    最近更新 更多