【问题标题】:Raspi camera module not working in separate processRaspi 相机模块不在单独的进程中工作
【发布时间】: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


【解决方案1】:

问题似乎是您有两个不同的进程访问 PiCamera 模块,这在文档中Section 5.16 的常见问题解答中明确禁止:

相机固件设计为一次由一个进程使用。尝试同时从多个进程使用相机会以多种方式失败(从简单的错误到进程锁定)。

我将您的代码减少到最低限度以显示问题,即在第一个进程中导入 camera.py 模块时执行相机初始化,但在生成的子进程中执行读取图像 - 因此有两个进程正在访问相机。

#!/usr/bin/env python3

from multiprocessing import Process
import camera as c
import time

proc = Process(target=c.run)
proc.start()

while True:
    time.sleep(1)
    print("main")

还有camera.py 模块:

#!/usr/bin/env python3

import os

print('Running in process: {}'.format(os.getpid()))
print('camera = PiCamera()')
print('camera.resolution = (1280, 720)')
print('camera.framerate = 30')
print('rawCapture = PiRGBArray(camera, size=(1280, 720))')


def run():
    print("run function started")
    print('Running in process: {}'.format(os.getpid()))

当你运行它时,你会看到报告了两个不同的进程id:

样本输出

Running in process: 51513
camera = PiCamera()
camera.resolution = (1280, 720)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(1280, 720))
run function started
Running in process: 51514
main
main
main

一种解决方案是在run() 函数中初始化相机。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2023-03-03
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多