gpt4 book ai didi

python - 在 Raspberry Pi 上运行的 Docker 容器中访问 picamera 时出错

转载 作者:行者123 更新时间:2023-12-02 16:26:51 26 4
gpt4 key购买 nike

我正在 Raspberry Pi 4 上使用 docker-compose 运行一个 Docker 容器,并想使用 Python 中的 picamera 模块来访问 PiHQCam。我试图使用本教程让它工作:https://www.losant.com/blog/how-to-access-the-raspberry-pi-camera-in-docker .但是,在尝试构建图像时,它给了我这个错误:

ValueError: This system does not appear to be a Raspberry Pi

然后我尝试了自己的解决方案(见下文),但遇到了这个错误:

picamera.exc.PiCameraMMALError: Failed to create MMAL component b'vc.camera_info': I/O error

这是我的设置:

docker-compose.yml:

version: '3.8'

services:
camera:
build: camera
image: eye_camera
devices:
- /dev/vchiq

Dockerfile:

FROM balenalib/rpi-raspbian:latest

RUN apt-get update && \
apt-get upgrade && \
apt-get install -y python3 \
python3-pip

WORKDIR /app

COPY requirements.txt .
RUN pip3 install -r requirements.txt

COPY . .

RUN groupadd -r -g 888 app && \
useradd -r -u 888 -g app -d /app app && \
chown -R app:app /app && \
usermod -a -G video app
USER app

CMD ["python3", "./main.py"]

main.py:

import picamera

def main():
print("Hello World!")

cam = picamera.PiCamera()
img = picamera.array.PiRGBArray(cam)
cam.capture(img)
cam.close()

if __name__ == '__main__':
main()

PiCameraMMALError 恰好在初始化相机时发生。这是完整的错误输出:

camera_1 | Hello World!
camera_1 | Traceback (most recent call last):
camera_1 | File "main.py", line 19, in <module>
camera_1 | main()
camera_1 | File "main.py", line 12, in main
camera_1 | cam = picamera.PiCamera()
camera_1 | File "/usr/local/lib/python3.7/dist-packages/picamera/camera.py", line 367, in __init__
camera_1 | with mo.MMALCameraInfo() as camera_info:
camera_1 | File "/usr/local/lib/python3.7/dist-packages/picamera/mmalobj.py", line 2346, in __init__
camera_1 | super(MMALCameraInfo, self).__init__()
camera_1 | File "/usr/local/lib/python3.7/dist-packages/picamera/mmalobj.py", line 633, in __init__
camera_1 | prefix="Failed to create MMAL component %s" % self.component_type)
camera_1 | File "/usr/local/lib/python3.7/dist-packages/picamera/exc.py", line 184, in mmal_check
camera_1 | raise PiCameraMMALError(status, prefix)
camera_1 | picamera.exc.PiCameraMMALError: Failed to create MMAL component b'vc.camera_info': I/O error
camera_1 | mmal: mmal_vc_shm_init: could not initialize vc shared memory service
camera_1 | mmal: mmal_vc_component_create: failed to initialise shm for 'vc.camera_info' (7:EIO)
camera_1 | mmal: mmal_component_create_core: could not create component 'vc.camera_info' (7)

这里有什么问题?谢谢你的帮助!我很乐意提供更多信息:)

最佳答案

出于某些完全无法理解的原因,您需要魔法咒语“READTHEDOCS=True”来避免这个问题!以下 Dockerfile 在 Raspberry Pi 4 上运行良好,但如果没有它,Docker 镜像构建将失败并出现您看到的错误:

FROM arm32v7/python:latest

# Set in container
ENV TZ=Europe/London

# See https://github.com/waveform80/picamera/issues/578
ENV READTHEDOCS=True

COPY ./src /app

WORKDIR /app

RUN pip install --upgrade pip setuptools wheel

RUN pip install picamera

VOLUME ["/data"]

CMD ["/usr/bin/python", "camera.py"]

我用 docker build 命令构建了它:

docker build -t jrcamera:latest .

并使用 docker run 命令运行它:

docker run --privileged=true -v /opt/vc:/opt/vc --env LD_LIBRARY_PATH=/opt/vc/lib --device /dev/vchiq -it jrcamera:latest

它在 src 目录(复制到 Docker 镜像中的/app)中使用以下 python 脚本 camera.py 运行:

from time import sleep
from datetime import datetime
from picamera import PiCamera

camera = PiCamera()

camera.resolution = (1280, 720)

camera.annotate_text_size = 16 # Can be in range 6 to 160 inclusive, with default 32

MAX_ITER = 5

for iter_no in range(0,MAX_ITER):

camera.start_preview()

# It is important to sleep for at least two seconds before capturing an image,
# because this gives the cameras sensor time to sense the light levels
#
sleep(3)

# Annotate image with current datetime, to nearest millisecond
#
date_time = datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S.%f')[:-3]

camera.annotate_text = date_time

camera.capture("/data/image_{0:}.jpg".format(date_time))

camera.stop_preview()

(现在我只需要弄清楚如何将我的图像大小从大得离谱的 710 MBytes 减小,但这是另一个问题。)

关于python - 在 Raspberry Pi 上运行的 Docker 容器中访问 picamera 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64280809/

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