Python多进程无法腌制opencv videocapture对象

时间:2018-11-14 20:34:48

标签: python opencv multiprocessing pickle

我正在尝试创建一个独立的过程来处理我从相机获取的图像。但是,多处理似乎很难从opencv中选取视频捕获模块。有人可以建议解决方法吗?我正在使用python 3.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 Studio \ Shared \ Anaconda3_64 \ lib \ multiprocessing \ reduction.py”,第60行     ForkingPickler(文件,协议).dump(obj)

TypeError:无法腌制cv2.VideoCapture对象

1 个答案:

答案 0 :(得分:0)

我不是专家,但我遇到了同样的问题,我就是这样解决的。

而不是在初始化函数中使用 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():

它对我有用。 或许对你也有帮助