【问题标题】:How to terminate "multiprocessing.Process" correctly when using "cv2.VideoCapture" (Python OpenCV)?使用“cv2.VideoCapture”(Python OpenCV)时如何正确终止“multiprocessing.Process”?
【发布时间】:2018-06-21 08:46:52
【问题描述】:

我对这里发生的事情感到茫然。我正在尝试运行 opencv VideoCapture 进程来读取帧并放入队列以供工作人员处理。

应用程序永远不会完成运行,因为 CameraProcess 实例似乎永远不会终止。经过一番调查,看起来相机在调用#release()后报告为关闭,但在主进程中被认为是打开的。

我不应该在 Process 中创建 VideoCapture 实例吗?

示例应用程序:

import cv2
from multiprocessing import Process, Event
from time import sleep

class CameraProcess(Process):
    def __init__(self, camera_id, *args, **kwargs):
        super(CameraProcess, self).__init__(*args, **kwargs)
        self.shutdown = Event()

        self.camera = cv2.VideoCapture(camera_id)
        sleep(1)
        print('Camera is opened? {}'.format(self.camera.isOpened()))

    def run(self):
        while not self.shutdown.wait(0.05):
            print('Doing camera stuff')
            sleep(1)

        self.camera.release()
        print('Camera is closed? {}'.format(not self.camera.isOpened()))

try:
    camera = CameraProcess(0)
    camera.start()
    sleep(5)
    camera.shutdown.set()
    sleep(2)
    print('Camera is closed? {}'.format(not camera.camera.isOpened()))
except KeyboardInterrupt:
    pass
finally:
    camera.terminate()

应用程序输出:

相机打开了?真的 做相机的东西 做相机的东西 做相机的东西 做相机的东西 做相机的东西 相机关了?真的 相机关了?错误的

环境:

  • Debian 延伸
  • Python:3.5.3
  • cv2:3.4.0

【问题讨论】:

    标签: python opencv multiprocessing


    【解决方案1】:

    奇怪的是,它应该被释放。但是相机资源仍然被占用。可能是一个错误?或者也许它是新事物......


    为了正确释放资源,我定义了一个新方法如下。然后就可以了。

    import cv2
    from multiprocessing import Process, Event
    from time import sleep
    
    class CameraProcess(Process):
        def __init__(self, camera_id, *args, **kwargs):
            super(CameraProcess, self).__init__(*args, **kwargs)
            self.shutdown = Event()
    
            self.camera = cv2.VideoCapture(camera_id)
            print('Camera is opened? {}'.format(self.camera.isOpened()))
    
        def run(self):
            while not self.shutdown.wait(0.05):
                print('Doing camera stuff')
                sleep(1)
            self.camera.release()
            print('[run   ] Camera is closed? {}'.format(not self.camera.isOpened()))
    
        ## Defind a new method to release the resourse(again).
        def shutup(self):
            self.shutdown.set()
            if self.camera.isOpened():
                self.camera.release()
            print('[shutup] Camera is closed? {}'.format(not self.camera.isOpened()))
    
    try:
        camera = CameraProcess(0)
        camera.start()
        sleep(5)
        camera.shutup()
        sleep(2)
        print('[main  ] Camera is closed? {}'.format(not camera.camera.isOpened()))
    except KeyboardInterrupt:
        pass
    finally:
        camera.terminate()
    

    结果现在是OK

    Camera is opened? True
    Doing camera stuff
    Doing camera stuff
    Doing camera stuff
    Doing camera stuff
    Doing camera stuff
    [shutup] Camera is closed? True
    [run   ] Camera is closed? True
    [main  ] Camera is closed? True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 2019-12-27
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多