user2386301
user2386301

Reputation: 451

Python multiprocess can't pickle opencv videocapture object

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

Upvotes: 8

Views: 9662

Answers (1)

Julio Zurbitu
Julio Zurbitu

Reputation: 49

I am not an expert, but i had the same issue and i solved in this way.

Instead of using cv2.VideoCapture(0) in init fuction

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

I moved the initialization to run function

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

And it works for me. Maybe it helps you too

Upvotes: 4

Related Questions