Python多进程可以拾取OpenCV视频捕获对象

2022-04-07 00:00:00 python opencv multiprocessing pickle

问题描述

我正在尝试创建一个独立的进程来处理从相机获取的图像。但多处理器似乎很难从OpenCV中提取视频捕获模块。有没有人能推荐一种解决办法?我使用的是python3.7.1

from multiprocessing import Process
import multiprocessing as mp
import time
import logging
import logging.handlers
import sys

import logging
from enum import Enum
import cv2


class Logger():
    @property
    def logger(self):
        component = "{}.{}".format(type(self).__module__, type(self).__name__)
        #default log handler to dump output to console

        return logging.getLogger(component)



class MyProcess(Logger):

    def __init__(self, ):
        self.process = Process(target = self.run, args=())
        self.exit = mp.Event()
        self.logger.info("initialize class")
        self.capture = cv2.VideoCapture(0)

    def run(self):
        while not self.exit.is_set():

            pass
        print("You exited!")

    def shutdown(self):
        print("Shutdown initiated")
        self.exit.set()


if __name__ == "__main__":
    p = MyProcess()
    p.process.start()
    print( "Waiting for a while")
    time.sleep(3)
    p.shutdown()
    time.sleep(3)
    print("Child process state: {}".format(p.process.is_alive())) 

返回错误说明无法对视频捕获对象进行酸洗。

文件"C:Program Files(X86)Microsoft Visual StudioSharedAnaconda3_64lib多处理Education tion.py",第60行,转储中 ForkingPickler(文件,协议).转储(Obj)

TypeError:无法Pickle cv2。视频捕获对象


解决方案

我不是专家,但我遇到了同样的问题,我以这种方式解决了问题。

在init函数中不使用cv2.VideoCapture(0)

 def __init__(self, ):
    self.process = Process(target = self.run, args=())
    self.exit = mp.Event()
    self.logger.info("initialize class")
    self.capture = cv2.VideoCapture(0)

我移动了初始化以运行函数

 def run(self):
    self.capture = cv2.VideoCapture(0)
    while not self.exit.is_set():

它对我很管用。 也许它对你也有帮助

相关文章