将我的源代码构建到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%的Raspberry CPU。我必须使用
sudo kill
谢谢,因为阅读了我的问题!
最佳答案
我发现使用以下方法解决此问题:多重处理
我们将创建一个用于管理摄像机的服务器,以确保在中只有一次运行的摄像机,并且可以在进程之间共享摄像机。
例如:
manager.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
server.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()
client.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/