[英]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.
但是 multiprocessing 似乎很难从 opencv 中酸洗视频捕获模块。 Can anyone suggest a work around ?
任何人都可以建议解决方法吗? I am using python 3.7.1
我正在使用 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)文件“C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Anaconda3_64\\lib\\multiprocessing\\reduction.py”,第 60 行,在转储 ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle cv2.VideoCapture objects类型错误:无法腌制 cv2.VideoCapture 对象
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而不是在初始化函数中使用 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)
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
也许它也对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.