Python multiprocess can#39;t pickle opencv videocapture object(Python多进程可以拾取OpenCV视频捕获对象)
本文介绍了Python多进程可以拾取OpenCV视频捕获对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个独立的进程来处理从相机获取的图像。但多处理器似乎很难从OpenCV中提取视频捕获模块。有没有人能推荐一种解决办法?我使用的是python3.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 StudioSharedAnaconda3_64lib多处理 Education tion.py",第60行,转储中 ForkingPickler(文件,协议).转储(Obj)TypeError:无法Pickle cv2。视频捕获对象
推荐答案
我不是专家,但我遇到了同样的问题,我以这种方式解决了问题。
在init函数中不使用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():
它对我很管用。 也许它对你也有帮助
这篇关于Python多进程可以拾取OpenCV视频捕获对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:Python多进程可以拾取OpenCV视频捕获对象
基础教程推荐
猜你喜欢
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
