【发布时间】:2019-05-02 19:42:10
【问题描述】:
我正在尝试构建一个将在 Raspberry Pi 上运行的多进程应用程序。其中一个进程应该从 rpi 相机获取帧并将其保存到磁盘以供其他进程之一使用。但是,python multiprocessing.Process 类处理 rpi 相机模块的方式有些奇怪。
基本上,如果我尝试在 Process 内运行 rpi 摄像头模块,它会在 for frame in self.camera.capture_continuous 行冻结。
下面是一些示例代码:
main.py
from multiprocessing import Process
import camera as c
import time, atexit, sh
def cleanUp():
print("Killed the following processes:\n{}".format(
sh.grep(sh.ps("aux", _piped=True), "python3")))
sh.pkill("python3")
# Used to keep any of the processes from being orphaned
atexit.register(cleanUp)
proc = Process(target=c.run)
proc.start()
while True:
time.sleep(1)
print("main")
camera.py
from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
camera = PiCamera()
camera.resolution = (1280, 720)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(1280, 720))
def run():
print("run function started")
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
print("this never runs")
cv2.imwrite('frame.jpg', frame.array)
rawCapture.truncate(0)
有什么见解吗?
【问题讨论】:
-
如果您认为问题出在进程内运行 RaspiCamera 模块,请显示尝试这样做但不起作用的最简单代码 - 无需显示 Flask 应用程序或任何 HTML。您应该展示最小、完整、可验证的示例,请参阅stackoverflow.com/help/mcve
-
这是更新的简化版。
-
这里时间不早了,我周末去看看。现在看起来更简单了,谢谢。您是否在相机初始化后尝试快速打印语句以查看其初始化是否正常?您是否尝试在 multiprocessing 之外运行该模块,即独立运行?您能否确认没有其他程序正在访问相机?
-
我已验证相机已初始化,我在
Process之外运行了该函数,它工作正常。根据过去的经验,如果相机正被其他资源使用,则初始化相机将失败,并出现 picamera.exc.PiCameraMMALError: Camera component could not be enabled: Out of resources (other than memory) 我没有得到。 -
如果有帮助,我正在使用 Raspian Stretch 开发 Raspberry Pi 3B+。
标签: python python-3.x opencv raspberry-pi