Python多进程可以拾取OpenCV视频捕获对象 [英] Python multiprocess can't pickle opencv videocapture object

查看:19
本文介绍了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视频捕获对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆