gpt4 book ai didi

python - Raspberry Pi cv2.VideoCapture 卡在第二次初始化时具有相同的 ID

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:05 25 4
gpt4 key购买 nike

我在为 Raspberry Pi 构建源代码时遇到问题。

OpenCV 无法初始化新实例并卡在步骤调用 cv2.VideoCapture

这是我的源代码:

import threading
import time
import cv2

CAPTURE_HZ = 30.0

class Person(object):
def __init__(self, name, id):
self._camera = cv2.VideoCapture(id)
print("Debug")
self.name = name
self._capture_frame = None
# Use a lock to prevent access concurrent access to the camera.
self._capture_lock = threading.Lock()
self._capture_thread = threading.Thread(target=self._grab_frames)
self._capture_thread.daemon = True
self._capture_thread.start()

def _grab_frames(self):
while True:
with self._capture_lock:
self._capture_frame = self.name
time.sleep(1.0 / CAPTURE_HZ)

def speak(self):
print("Hi! My name is {self.name}.".format(self=self))

这是我正在测试的结果:

$ python3
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import camera
>>> camera1 = camera.Camera("Cam1", 0)
Debug
>>> camera2 = camera.Camera("Cam2", 0) <<<<< It's stuck at here

并且这个进程占用了树莓派 100% 的 CPU。我必须使用 sudo kill

感谢您阅读我的问题!

最佳答案

我发现解决这个问题的方法是使用:Multiprocessing

我们将创建一个用于管理摄像头的服务器,以确保一次只有一个摄像头在运行,并且可以在进程之间共享摄像头。

例如:

经理.py

import threading
import time
import cv2
from multiprocessing import Process
from multiprocessing.managers import BaseManager

CAPTURE_HZ = 30.0

class Camera(object):
def __init__(self, name, id):
self._camera = cv2.VideoCapture(id)
print("Debug")
self.name = name
self._capture_frame = None
# Use a lock to prevent access concurrent access to the camera.
self._capture_lock = threading.Lock()
self._capture_thread = threading.Thread(target=self._grab_frames)
self._capture_thread.daemon = True
self._capture_thread.start()

def _grab_frames(self):
while True:
with self._capture_lock:
self._capture_frame = self.name
time.sleep(1.0 / CAPTURE_HZ)

def speak(self):
return self.name

class CameraManager(BaseManager):
def __init__(self, address=None, authkey=''):
BaseManager.__init__(self, address, authkey)
self.camera = {}

def addCamera(self, name, texture):
self.camera[name] = texture

def hasCamera(self, name):
return name in self.camera


服务器.py

from multiprocessing import Process
from multiprocessing.managers import BaseManager
from manager import Camera, CameraManager

manager = CameraManager(address=('127.0.0.1', 50000), authkey=b'hello')

def getCamera(name): # Client always call to one function is getCamera
if manager.hasCamera(name): # If camera already running -> return that camera
print("Exist Camera")
return manager.camera[name]
else: # If camera not exist -> create the new one
print("Create New Camera")
camera = Camera(name, 0)
manager.addCamera(name, camera)
manager.register(name, lambda: camera)
return manager.camera[name]

CameraManager.register("getCamera", getCamera)

if __name__ == "__main__":
server = manager.get_server()
server.serve_forever()

客户端.py

from multiprocessing import Process
from multiprocessing.managers import BaseManager
from manager import Camera, CameraManager

if __name__ == "__main__":
manager = CameraManager(address=('127.0.0.1', 50000), authkey=b'hello')
manager.connect()
CameraManager.register("getCamera")
camera = manager.getCamera("camera01")
print("data = %s" % (camera.speak()))

这是我测试的结果:

$ python3 server.py

并为 client.py 运行两个进程

$ python3 client.py

那时我的server.py就是

python3 server.py
Create New Camera
Debug
Exist Camera
Exist Camera

我是从This Stackoverflow Thread 学到这个教程的.非常感谢!

关于python - Raspberry Pi cv2.VideoCapture 卡在第二次初始化时具有相同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com