Python multiprocess can't pickle opencv videocapture object

蓝咒 提交于 2021-01-27 12:52:49

问题


I am trying to create a independent process to handle my image acquire from camera. But multiprocessing seems to have difficulty pickling videocapture module from opencv. Can anyone suggest a work around ? I am using 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())) 

The return error specific said it videocapture object can't be pickled.

File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj)

TypeError: can't pickle cv2.VideoCapture objects

来源:https://stackoverflow.com/questions/53308334/python-multiprocess-cant-pickle-opencv-videocapture-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!