我正在尝试创建一个独立的进程来处理从相机获取的图像。但多处理似乎很难从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()))
返回的错误特定于它的视频捕获对象不能被pickle。
文件"C:\Program Files(x86(\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\reduction.py",第60行,转储ForkingPickler(文件,协议(.dump(obj(
TypeError:无法pickle cv2.VideoCapture对象
我不是专家,但我遇到了同样的问题,我用这种方式解决了。
而不是在初始功能中使用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():
它对我有效。也许它对你也有帮助