Raspberry Pi Cam Module 3

Sure, or you can probably try to add it with pip like I did with aiohttp_basicauth.
Don’t be afraid to experiment :wink:

I’m guessing there’s a difference in supported packages between your device and what I tried.
I tried building for Raspberry Pi 3…

1 Like

After a lot of editing and messing around i couldn’t manage to upload @TJvV last post. So I’ve attempted to change the docker file to run off

python:3.7-slim-buster

Although the upload is now working the logs on the raspberry pi are showing:

 Rail-Vista-Main  172.17.0.2
 Rail-Vista-Main  Traceback (most recent call last):
 Rail-Vista-Main    File "src/main.py", line 29, in <module>
 Rail-Vista-Main      from picamera2 import Picamera2
 Rail-Vista-Main  ModuleNotFoundError: No module named 'picamera2'

This is the updated dockerfile

# Start from the Python slim-buster image
FROM python:3.7-slim-buster

# Set work directory
WORKDIR /usr/src/app

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install system dependencies including Git and libcap development headers
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    gcc \
    libssl-dev \
    libffi-dev \
    ffmpeg \
    libopus-dev \
    libvpx-dev \
    gstreamer1.0-plugins-base \
    gstreamer1.0-plugins-good \
    v4l-utils \
    libsrtp2-dev \
    libopencv-dev \
    libatlas-base-dev \
    libavformat-dev \
    libswscale-dev \
    libavdevice-dev \
    libavfilter-dev \
    libavcodec-dev \
    libcap-dev \
    && rm -rf /var/lib/apt/lists/*

# Upgrade pip
RUN pip install --upgrade pip setuptools wheel

# Install Python dependencies
RUN pip install --no-cache-dir \
    aiohttp \
    aioice \
    aiortc \
    async-timeout \
    av \
    numpy \
    opencv-python-headless \
    pyee \
    aiohttp_basicauth


# Copy the application code
COPY src ./src
COPY VERSION .

# Set PYTHONPATH
ENV PYTHONPATH=/usr/src/python-packages

# Run the application
CMD ["python3", "src/main.py"]

Is their some-sort of directory file name i’am getting wrong?

Hi,

I’m not seeing any reference to apt-get install python3-picamera2 or pip install picamera in your last post; I’m guessing that’s the issue?

I should have some time later today to see if I can make things work for other devices (like Pi Zero, Pi Zero 2, and/or Pi 4).

1 Like

Managed (After 24 attempts) to successfully upload a docker-file. However the logs are now saying:

No module named ‘libcamera’

This is the full log error:

 Rail-Vista-Main  172.17.0.2
 Rail-Vista-Main  Traceback (most recent call last):
 Rail-Vista-Main    File "src/main.py", line 29, in <module>
 Rail-Vista-Main      from picamera2 import Picamera2
 Rail-Vista-Main    File "/usr/local/lib/python3.7/site-packages/picamera2-0.3.18-py3.7.egg/picamera2/__init__.py", line 3, in <module>
 Rail-Vista-Main  ModuleNotFoundError: No module named 'libcamera'

And this is the updated docker-file:

# Use Python slim-buster as base image
FROM python:3.7-slim-buster

# Set work directory
WORKDIR /usr/src/app

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    gcc \
    cmake \
    libssl-dev \
    libcurl4-openssl-dev \
    libudev-dev \
    libjpeg-dev \
    libcap-dev \
    ffmpeg \
    libopus-dev \
    libvpx-dev \
    gstreamer1.0-plugins-base \
    gstreamer1.0-plugins-good \
    v4l-utils \
    libsrtp2-dev \
    libopencv-dev \
    libatlas-base-dev \
    libavformat-dev \
    libswscale-dev \
    libavdevice-dev \
    libavfilter-dev \
    libavcodec-dev \
    gnupg \
    dirmngr \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Upgrade pip
RUN pip install --upgrade pip setuptools wheel

# Install Python dependencies
RUN pip install --no-cache-dir \
    aiohttp \
    aioice \
    aiortc \
    async-timeout \
    av \
    numpy \
    opencv-python-headless \
    pyee \
    aiohttp_basicauth

# Clone and install picamera2 module
RUN git clone https://github.com/raspberrypi/picamera2.git \
    && cd picamera2 \
    && python setup.py install

# Clone libcamera repository to a specific directory
RUN git clone https://github.com/raspberrypi/libcamera.git /usr/src/app/libcamera

# Check the contents of libcamera directory
RUN ls -la /usr/src/app/libcamera

# Check the contents of libcamera subdirectories
RUN ls -la /usr/src/app/libcamera/*

# Build and install libcamera if CMakeLists.txt is found
RUN if [ -f "/usr/src/app/libcamera/CMakeLists.txt" ]; then \
    mkdir -p /usr/src/app/libcamera/build \
    && cd /usr/src/app/libcamera/build \
    && cmake -DCMAKE_BUILD_TYPE=Release .. \
    && make \
    && make install; \
    fi

# Copy the application code
COPY src ./src
COPY VERSION .

# Set PYTHONPATH
ENV PYTHONPATH=/usr/src/python-packages

# Run the application
CMD ["python3", "src/main.py"]

I’ve also uploaded the file using both the RPI4 & RPI3 Default device type and found that it didn’t make a difference the error message :frowning:

I’ve been working getting the balena-cam project to build.
This seems to build for Raspberry Pi (Zero), Zero 2, and Pi 4.
It grabs the libcamera and picamera2 from git.
There’s a few duplicate libs in there, but that’s what you get when you simply copy paste requirements :stuck_out_tongue:

Two notes:

When building libcamera, I keep getting a NO for the V4L2 emulation support; not quite sure how to enable that one yet.

When running the python server as part of the build, I hit a “modprobe: FATAL: Module bcm2835-v4l2 not found in directory /lib/modules/6.1.35-yocto-standard”; don’t know if this will also happen when running on the actual device.

FROM balenalib/%%BALENA_MACHINE_NAME%%-debian:bookworm

# Install base python packages
RUN install_packages \
    python3 \
    python3-dev \
    python3-pip \
    python3-setuptools \
    python3-venv

# Create and use virtual environment
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV --system-site-packages
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN pip install --upgrade pip setuptools wheel

# Install dependencies
RUN install_packages \
    python3-aiohttp \
    python3-aioice \
    python3-aiortc \
    python3-async-timeout \
    python3-av \
    python3-numpy \
    python3-opencv \
    python3-pyee

# Install drivers / libs
RUN install_packages \
    gstreamer-1.0 \
    v4l-utils \
    libopus-dev \
    libvpx-dev \
    libsrtp2-dev \
    libopencv-dev \
    libatlas3-base \
    libatlas-base-dev \
    libavformat-dev \
    libswscale-dev \
    libavdevice-dev \
    libavfilter-dev \
    libavcodec-dev

# Add build system for new libraries
RUN install_packages build-essential gcc cmake git patchelf autoconf autotools-dev automake ninja-build

RUN install_packages libyaml-dev gnutls-dev libcrypto++-dev libdw-dev libunwind-dev
RUN install_packages libcamera-dev libudev-dev libevent-dev libjpeg-dev libdrm-dev libsdl2-dev libtiff-dev
RUN install_packages libcap-dev openssl libssl-dev
RUN install_packages liblttng-ust-dev lttng-tools

# Add libraries that don't have system packages
RUN pip install aiohttp_basicauth
RUN pip install patchelf
RUN pip install pyyaml pybind11 packaging meson pyproject-metadata meson-python jinja2 ply

# Need these for different libcamera options, according to docs in git repo
RUN install_packages libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
RUN install_packages libtiff-dev qtbase5-dev qttools5-dev-tools
RUN install_packages abi-compliance-checker pybind11-dev
RUN install_packages libv4l-dev

# Build libcamera from git
RUN git clone https://git.libcamera.org/libcamera/libcamera.git /opt/libcamera
WORKDIR /opt/libcamera
RUN meson setup build
RUN ninja -C build install

# Build picamera2 from git
RUN git clone https://github.com/raspberrypi/picamera2 /opt/picamera2
WORKDIR /opt/picamera2
RUN pip install .


# Setup our application
WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/
# RUN python3 /usr/src/app/server.py
# CMD ["balena-idle"]
CMD ["python3", "/usr/src/app/server.py"]

Just tried to upload your code however the setup section isn’t working

# Setup our application
WORKDIR /usr/src/app
COPY ./app/ /usr/src/app/
# RUN python3 /usr/src/app/server.py
# CMD ["balena-idle"]
CMD ["python3", "/usr/src/app/server.py"]

It shows this error message whilst attempting to upload:

COPY failed: file not found in build context or excluded by .dockerignore: stat app/: file does not exist

So i changed it back to the old one temporarily:

# Copy the application code
COPY src ./src
COPY VERSION .

# Set PYTHONPATH
ENV PYTHONPATH=/usr/src/python-packages

# Run the application
CMD ["python3", "src/main.py"]

This uploaded fine however got an error in the logs saying:

 Rail-Vista-Main  172.17.0.2
 Rail-Vista-Main  Traceback (most recent call last):
 Rail-Vista-Main    File "/opt/picamera2/src/main.py", line 29, in <module>
 Rail-Vista-Main      from picamera2 import Picamera2
 Rail-Vista-Main    File "/opt/venv/lib/python3.11/site-packages/picamera2/__init__.py", line 3, in <module>
 Rail-Vista-Main      import libcamera
 Rail-Vista-Main  ModuleNotFoundError: No module named 'libcamera'

I think it might have a correlation to me editing of your setup however I’ve tinkered with the directory’s and it brings no luck. Apologies if i sound like a clown this is completely new to me. I thought it would be as simple as uploading a python file :disappointed:

No worries; it’s turning out more difficult than I expected as well :stuck_out_tongue:
It should be much easier.

Doesn’t help that we’re looking at 2 different projects (balena-cam and Rail-Vista-Balena) at the same time and that I don’t actually have a cam.

Just so we’re on the same page, what should I be focusing on to best help you?

  1. Which project?
  2. Which device type?

Hi

Thank you for your reply :slightly_smiling_face:

The project that Iam looking at is the Rail-Vista-Balena

And the device is the Raspberry Pi 4 connected to a raspberry pi cam module 3

Thanks
Jake

Got something that seems to run on my pi4 here.
It will complain about an index out of bounds because I have no camera, but at least it gets past the module not found issues.

I’ve added the Raspberry Pi apt repository, that way we can actually install the packages all the guides are talking about.
This results in a much cleaner Dockerfile than building the modules ourselves.
Results in this Dockerfile (based Rail-Vista commit 25f17635cea079e531176143e51f04d3c8ca6c30):

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3.11-bookworm-run

# Ensure pip builde with latest modules
RUN pip install --upgrade pip setuptools wheel

# Add Raspberry Pi repo
RUN curl -sS http://archive.raspberrypi.org/debian/raspberrypi.gpg.key | gpg --dearmor > /etc/apt/trusted.gpg.d/raspberrypi.gpg.key
RUN echo "deb [signed-by=/etc/apt/trusted.gpg.d/raspberrypi.gpg.key] http://archive.raspberrypi.org/debian/ bookworm main" >> /etc/apt/sources.list.d/raspi.list

# Install the python camera packages
RUN install_packages python3-libcamera python3-picamera2

# Now proceed to install Python dependencies from requirements.txt
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# Copy the rest of your application
COPY . /usr/src/app/

# Enable udevd so that plugged dynamic hardware devices show up in our container
ENV UDEV=1

# Set the working directory
WORKDIR /usr/src/app

# Expose the port for streaming
EXPOSE 8000

# CMD to run your Python application
CMD ["python3", "-u", "src/main.py"]

I did also find out what went wrong with the self-built modules; the path where they got installed wasn’t included in the python and library search paths (PYTHONPATH and LD_LIBRARY_PATH).

Many thanks. I’ve sadly got the same error with the camera not being “in range” even though the camera is connected which is probably now an issue with the python file:

 Rail-Vista-Main  [0:12:08.422438914] [1]  INFO Camera camera_manager.cpp:284 libcamera v0.2.0+120-eb00c13d
 Rail-Vista-Main  Traceback (most recent call last):
 Rail-Vista-Main    File "/usr/src/app/src/main.py", line 163, in <module>
 Rail-Vista-Main      picam2 = Picamera2()
 Rail-Vista-Main               ^^^^^^^^^^^
 Rail-Vista-Main    File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 245, in __init__
 Rail-Vista-Main      camera_num = self.global_camera_info()[camera_num]['Num']
 Rail-Vista-Main                   ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
 Rail-Vista-Main  IndexError: list index out of range

Should i raise it to the libcamera/Raspberry Pi forums?

Many thanks for your help
Jake

According to this issue it sounds like the camera is not being recognized.
They suggest running libcamera-hello --list-camera to test this.

In the balena-cam readme they say to change the configured GPU memory; did you do that?

Did you try to increase the GPU memory from the default 16 to 64 @Jake1 ?

Thank you for your replys @TJvV 7 @mpous I’ve tried the libcamera-hello --list-camera command and got this in the logs:

root@097be03afe92:/usr/src/app# libcamera-hello --list-camera
bash: libcamera-hello: command not found

I have also tried changing the configuration on my fleet settings to the balena cam documentation but i have a feeling that i have done something wrong:

I’ve also changed the config Define device GPU memory in megabytes. to 64 however still getting this error :frowning: :

 Rail-Vista-Main  172.17.0.2
 Rail-Vista-Main  [0:01:12.492794087] [1]  INFO Camera camera_manager.cpp:284 libcamera v0.2.0+120-eb00c13d
 Rail-Vista-Main  Traceback (most recent call last):
 Rail-Vista-Main    File "/usr/src/app/src/main.py", line 163, in <module>
 Rail-Vista-Main      picam2 = Picamera2()
 Rail-Vista-Main               ^^^^^^^^^^^
 Rail-Vista-Main    File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 245, in __init__
 Rail-Vista-Main      camera_num = self.global_camera_info()[camera_num]['Num']
 Rail-Vista-Main                   ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
 Rail-Vista-Main  IndexError: list index out of range

Hi,

I tried out some things on my Pi4.

After installing the libcamera-apps, I got this error:

# libcamera-hello --list
ERROR: rpicam-apps currently only supports the Raspberry Pi platforms.
Contributions for other platforms are welcome at https://github.com/raspberrypi/rpicam-apps.

I then made some changes to my device configuration:

  • I set Define device GPU memory in megabytes. to 128
    This is the same value that’s used in raspi-config.

I also added the Custom configuration:

  • BALENA_HOST_CONFIG_camera_auto_detect = 1
  • BALENA_HOST_CONFIG_disable_fw_kms_setup = 0

With these changes, I now get:

# libcamera-hello --list
No cameras available!

Hello, I was able to detect Camera Module 3 on a Pi 4 running balenaOS 5.2.8, using this example: GitHub - balena-io-experimental/libcamera-apps: Example repo to showcase Raspberry Pi's new libcamera stack on balenaOS
Just be sure to increase the GPU memory to at least 64MB - in the “Define device GPU memory in megabytes.” setting in device configuration. I made no other config changes or additions.

root@4da17ab33680:/usr/src/app# libcamera-hello --list-cameras -n -v
Available cameras
-----------------
0 : imx708 [4608x2592] (/base/soc/i2c0mux/i2c@1/imx708@1a)
    Modes: 'SRGGB10_CSI2P' : 1536x864 [120.13 fps - (768, 432)/3072x1728 crop]
                             2304x1296 [56.03 fps - (0, 0)/4608x2592 crop]
                             4608x2592 [14.35 fps - (0, 0)/4608x2592 crop]

1 Like

Thanks for your reply’s !

I’ve just been experimenting with the libcamera-apps directory and i carn’t figure out how to convert the dockerfile or python file to work with my code. At the moment, libcamera is working with an .sh file which I have 0% experience of using. The good news is however is that i have got a camera found message :grinning: thanks to @alanb128 & @TJvV.

This is my current code which iam attempting to work on :confused: :

1 Like

Hi,

Glad to hear you got something working :smiley:

The start.sh in the repo @alanb128 linked isn’t all that scary.
It is just calling 3 processes.
You could do the same in python with the subprocess module.

The script explained:

  • #!/bin/sh: run this script with /bin/sh
  • udevadm control --reload: this tells the OS to reload any changes to the udev rules regarding device handling;
  • libcamera-hello --list-cameras -n -v: show which cameras you have;
  • sleep infinity: don’t do anything forever; this is here to prevent the container from exiting.

The reason it’s packaged in a shell script, is because the Dockerfile CMD entry accepts only a single command.

The fastest way to test if it works with your own Dockerfile, is to swap out the CMD entry.
For development, I usually use CMD ["balena-idle"], which is a shorthand for CMD ["sleep", "infinity"].
I then use the devices terminal (either through dashboard, or balena ssh <uuid> <service> to test things before adding them to my project.

Apologies about the slight delay, I’ve been working on the bash code. Currently I managed to find the camera however when it attempts to stream it to a web-server it says in the logs:

 camera  #140 (30.01 fps) exp 6206.00 ag 1.12 dg 1.00
 camera  #141 (30.02 fps) exp 6206.00 ag 1.12 dg 1.00
 camera  #142 (30.01 fps) exp 6206.00 ag 1.12 dg 1.00
 camera  Halting: reached timeout of 5000 milliseconds.
 camera  pipe:0: Invalid data found when processing input
 camera  Preview window unavailable

I know that balena doesn’t support a preview but i carn’t think of any other alternatives to over-come that.

This is the code that i’am working with:

Bash:

#!/bin/sh

# Reload udev rules
udevadm control --reload

# List available cameras
libcamera-hello --list-cameras -n -v

# Get the local IP address
local_ip=$(ip route get 1 | awk '{print $7}')

while true; do
    # Start streaming from the camera using libcamera and pipe the output to FFmpeg
    libcamera-vid --output=pipe:0 --raw --codec=H264 | \
    ffmpeg -hide_banner -i pipe:0 -c:v copy -f mjpeg -q:v 5 -r 30 -s 640x480 -bufsize 1024k \
      -vf fps=10 "http://$local_ip:80/stream" >/dev/null

    sleep 1
done

Dockerfile:

# Use a base image suitable for your camera application
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian:bullseye

# Install necessary dependencies
RUN install_packages \
    libcamera-dev \
    libcamera-apps-lite \
    python3 \
    v4l-utils \
    ffmpeg


# Set the working directory
WORKDIR /usr/src/app/camera

# Copy camera-specific files and scripts
COPY stream.sh ./

RUN chmod +x stream.sh

# Expose any required ports
EXPOSE 80

# RUN usermod -a -G video root

ENV UDEV=on

# Define the command to start the camera service
CMD ["./stream.sh"]

Hi,

I’m no expert on video streams, but it sounds to me like you’re outputting h264, and then trying to read it as mjpeg?
What do you get if in the libcamera line, you change --codec=H264 to --codec=mjpeg?

It seems to be getting the same message :frowning_face: :

 camera  #140 (30.01 fps) exp 5764.00 ag 1.12 dg 1.00
 camera  #141 (30.01 fps) exp 5764.00 ag 1.12 dg 1.00
 camera  #142 (30.01 fps) exp 5764.00 ag 1.12 dg 1.00
 camera  Halting: reached timeout of 5000 milliseconds.
 camera  pipe:0: Invalid data found when processing input
 camera  Preview window unavailable