python - Python多进程无法腌制opencv videocapture对象
问题描述
我正在尝试创建一个独立的过程来处理我从相机获取的图像。但是多处理似乎很难从 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()))
特定的返回错误表示无法腌制视频捕获对象。
文件“C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\reduction.py”,第 60 行,转储 ForkingPickler(file, protocol).dump(obj)
TypeError:无法腌制 cv2.VideoCapture 对象
解决方案
我不是专家,但我遇到了同样的问题,我以这种方式解决了。
而不是在 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():
它对我有用。也许它也对你有帮助
推荐阅读
- react-native - 访问世博会资产
- flutter - 从另一个小部件访问 Controller.isEmpty
- javascript - 从 Google Analytics Data API (GA4) 获取实时信息,例如 UsersBysource、EventCountByEventName 和 eventParamerts
- mongodb - 可以通过 upsert 做到这一点吗?
- javascript - 如何使用 mongoose 调用 MongoDB 数组
- python - 熊猫在条件下合并数据框
- ios - 如何让我的核心数据实体的属性在 Objective-C 单元测试中可见
- angular - 单击 ion-item 时在 for 循环中选择 Id
- xamarin - 如何在 Xamarin App.Shell 中更改 Android ActionBar 颜色
- sql-server - 用于批量插入的从数据块到 sqlserver 的 Sql Apache Spark 连接器:性能问题