首页 > 解决方案 > 无法使用多处理 + cv2 将帧写入视频

问题描述

我有一个代码可以将视频分解为帧并编辑图像并将其放回视频中,但我意识到它真的很慢......所以我研究了多处理以加快代码速度,并且它有效!正如我所看到的,它处理图像的速度更快,但问题是,当我将这些帧添加到新视频中时,它不起作用,视频仍然是空的!

这是我的代码:

# Imports
import cv2, sys, time
import numpy as np

from scipy.ndimage import rotate
from PIL import Image, ImageDraw, ImageFont, ImageOps
import concurrent.futures
def function(fullimg):
    img = np.array(Image.fromarray(fullimg).crop((1700, 930, 1920-60, 1080-80)))
    inpaintRadius = 10
    inpaintMethod = cv2.INPAINT_TELEA
    textMask = cv2.imread('permanentmask.jpg', 0)
    final_result = cv2.inpaint(img.copy(), textMask, inpaintRadius, inpaintMethod)
    text = Image.fromarray(np.array([np.array(i) for i in final_result]).astype(np.uint8)).convert('RGBA')
    im = np.array([[tuple(x) for x in i] for i in np.zeros((70, 160, 4))])
    im[1:-1, 1:-1] = (170, 13, 5, 40)
    im[0, :] = (0,0,0,128)
    im[1:-1, [0, -1]] = (0,0,0,128)
    im[-1, :] = (0,0,0,128)
    im = Image.fromarray(im.astype(np.uint8))
    draw = ImageDraw.Draw(im)
    font = ImageFont.truetype('arialbd.ttf', 57)
    draw.text((5, 5),"TEXT",(255,255, 255, 128),font=font)
    text.paste(im, mask=im)
    text = np.array(text)
    fullimg = Image.fromarray(fullimg)
    fullimg.paste(Image.fromarray(text), (1700, 930, 1920-60, 1080-80))
    fullimg = cv2.cvtColor(np.array(fullimg), cv2.COLOR_BGR2RGB)
    return fullimg
cap = cv2.VideoCapture('before2.mp4')
_fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('after.mp4', _fourcc, 29.97, (1280,720))
frames = []
lst = []
while cap.isOpened():
    ret, fullimg = cap.read()
    if not ret:
        break
    frames.append(fullimg)
    
    if len(frames) >= 8:
        if __name__ == '__main__':
            with concurrent.futures.ProcessPoolExecutor() as executor:
                results = executor.map(function, frames)
                for i in results:
                    print(type(i))
                    out.write(i)
        frames.clear() 
cap.release()
out.release()
cv2.destroyAllWindows() # destroy all opened windows

我的代码修复了水印并使用 PIL 添加了另一个水印。

如果我不使用multiprocessing代码工作。但如果我使用multiprocessing,它会给出一个空视频。

标签: pythonopencvvideomultiprocessing

解决方案


我不太熟悉OpenCV,但您的代码中似乎有几处需要更正。首先,如果您在 Windows 下运行,看起来是因为您if __name__ == '__main__':保护了创建新进程的代码(顺便说一下,当您使用 标记问题时multiprocessing,您还应该使用正在使用的平台标记问题),然后全局范围内的任何代码都将由为实现您的池而创建的每个进程执行。这意味着您应该if __name__ == '__main__':按以下方式移动:

if __name__ == '__main__':
    cap = cv2.VideoCapture('before2.mp4')
    _fourcc = cv2.VideoWriter_fourcc(*'MPEG')
    out = cv2.VideoWriter('after.mp4', _fourcc, 29.97, (1280,720))
    frames = []
    lst = []
    while cap.isOpened():
        ret, fullimg = cap.read()
        if not ret:
            break
        frames.append(fullimg)
        
        if len(frames) >= 8:
            with concurrent.futures.ProcessPoolExecutor() as executor:
                results = executor.map(function, frames)
                for i in results:
                    print(type(i))
                    out.write(i)
            frames.clear() 
    cap.release()
    out.release()
    cv2.destroyAllWindows() # destroy all opened windows

如果您不这样做,在我看来,池中的每个子进程将首先尝试并行创建一个空视频(function工作函数并且out.write永远不会被这些进程调用),然后主进程才能能够使用 调用function工作函数map。这并不能完全解释为什么在所有这些浪费的尝试之后主进程没有成功。但...

你还有:

while cap.isOpened():

文档说明如果先前的构造函数成功则isOpened()返回。那么如果这返回一次,为什么它不会在下一次测试时返回,而你最终会无限循环?不应该改成一个吗?这是否表明它可能正在返回,否则您将无限期地循环?或者如果?看起来你也会得到一个空的输出文件。TrueVideoCaptureTrueTruewhileifisOpened()Falselen(frames) < 8

我的建议是进行上述更改并重试。

更新

I took a closer look at the code more closely and it appears that it is looping reading the input (before2.mp4) one frame at a time and when it has accumulated 8 frames or more it creates a pool and processes the frames it has accumulated and writing them out to the output (after.mp4). But that means that if there are, for example, 8 more frames, it will create a brand new processing pool (very wasteful and expensive) and then write out the 8 additional processed frames. But if there were only 7 additional frames, they would never get processed and written out. I would suggest the following code (untested, of course):

def main():
    import os

    cap = cv2.VideoCapture('before2.mp4')
    if not cap.isOpened():
        return

    _fourcc = cv2.VideoWriter_fourcc(*'MPEG')
    out = cv2.VideoWriter('after.mp4', _fourcc, 29.97, (1280,720))

    FRAMES_AT_A_TIME = 8
    pool_size = min(FRAMES_AT_A_TIME, os.cpu_count())
    with concurrent.futures.ProcessPoolExecutor(max_workers=pool_size) as executor:
        more_frames = True

        while more_frames:
            frames = []
            for _ in range(FRAMES_AT_A_TIME):
                ret, fullimg = cap.read()
                if not ret:
                    more_frames = False
                    break
                frames.append(fullimg)

            if not frames:
                break # no frames        

            results = executor.map(function, frames)
            for i in results:
                print(type(i))
                out.write(i)

    cap.release()
    out.release()
    cv2.destroyAllWindows() # destroy all opened windows

if __name__ == '__main__':
    main()

推荐阅读