Raspberry Pi Cam Module 3

Hi

I have made a python file to run on a raspberry pi using the latest camera module 3. When i set my code to run i receive a message saying:

from picamera2 import Picamera2
ModuleNotFoundError: No module named ‘picamera2’

I’ve been looking around and the official BalenaCam isnt working either.

Below is my project, any help would be much appreciated.

Thanks

Hi,

Something I noticed is that you’re building a Stretch image.
In the Picamera2 repo, they say “Picamera2 is only supported on Raspberry Pi OS Bullseye (or later) images, both 32 and 64-bit.”
Maybe changing your image to target a newer OS will help?

Also out of curiosity: why are you installing the module with python directly, instead of using pip?

1 Like

Thanks for your reply!

Iam new to balena. Which OS would be suitable?

What I’ve done is copied the example of helloworld.py from balena to attempt to make it easier.

Jake

Hi,

It’s okay to be new at stuff :slight_smile:

In your Dockerfile.template, can you try changing
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
to
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-bullseye-run

It might also be an idea to first try the version of Picamera2 that’s included in the apt repos, instead of the GitHub version:
Instead of

# Clone and install picamera2 from GitHub
RUN git clone https://github.com/raspberrypi/picamera2.git /usr/src/picamera2 \
    && cd /usr/src/picamera2 \
    && python3 setup.py install

try

# Install picamera2 from apt
RUN install_packages python3-picamera2
1 Like

Just tried to upload it and it logged:

[Info]             Starting build for rail-vista, user jakelofthouse
[Info]             Dashboard link: https://dashboard.balena-cloud.com/apps/2121148/devices
[Info]             Building on 4b2d7d0
[Info]             Pulling previous images for caching purposes...
[Success]          Successfully pulled cache images
[Info]             Starting to build Rail-Vista-Main
[Info]             Uploading images
[Success]          Successfully uploaded images
[Error]            Some services failed to build:
[Error]              Service: Rail-Vista-Main
[Error]                Error: The command '/bin/sh -c install_packages     build-essential     libjpeg-dev     git     libatlas-base-dev     libjasper-dev     libqtgui4     libqt4-test     python3-dev' returned a non-zero code: 100
[Info]             Built on 4b2d7d0
[Error]            Not deploying release.

Not sure what to do :frowning:

@Jake1 Looks like there is a package that is not available! do you see more errors on the logs?

Hi,

Just did a quick search for the packages; turns out qt4 is gone.
Bullseye does package libqt5gui5 and libqt5test5.

Edit: also didn’t find libjasper-dev, and I’m not seeing a replacement for it.

It’s just that on the logs

Just revamped the entire code using AI and it works however the camera preview isn’t it just shows the picture icon. Their is no camera issues in the log which i find a bit confusing!

This is the new code: GitHub - ALD-Models/Vista-V2

Thanks for sharing the code @Jake1

Could you please confirm what Raspberry Pi are you using? Let us try to reproduce and see how we can help you more.

Thanks @TJvV for helping here :slight_smile:

Iam using a raspberry pi 4 connected to a pi camera module 3.

Also I’m trying to make it live stream but Iam not sure if this is an appropriate method. I have attached the original code below:

https://github.com/ALD-Models/Rail-Vista-Balena

Thank you

@Jake1 not sure if you tried the balenaCam project before! Actually I don’t have the pi camera module 3 but maybe you can give a try as you can do a live stream from it as well!

I’ve tried that but i get this error:

[balena-cam]  W: The repository 'http://security.debian.org/debian-security stretch/updates Release' does not have a Release file.
[balena-cam]  W: The repository 'http://deb.debian.org/debian stretch Release' does not have a Release file.
[balena-cam]  W: The repository 'http://deb.debian.org/debian stretch-updates Release' does not have a Release file.
[balena-cam]  W: The repository 'http://archive.raspbian.org/raspbian stretch Release' does not have a Release file.
[balena-cam]  E: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/main/binary-armhf/Packages  404  Not Found
[balena-cam]  E: Failed to fetch http://deb.debian.org/debian/dists/stretch/main/binary-armhf/Packages  404  Not Found
[balena-cam]  E: Failed to fetch http://deb.debian.org/debian/dists/stretch-updates/main/binary-armhf/Packages  404  Not Found
[balena-cam]  E: Failed to fetch http://archive.raspbian.org/raspbian/dists/stretch/main/binary-armhf/Packages  404  Not Found
[balena-cam]  E: Some index files failed to download. They have been ignored, or old ones used instead.
[balena-cam]
[balena-cam]  Removing intermediate container 4a0aa83df3ab
[balena-cam]  The command '/bin/sh -c apt-get update &&   apt-get install -yq     python3     python3-dev     python3-pip     python3-setuptools     gstreamer-1.0     v4l-utils     libopus-dev     libvpx-dev     libsrtp2-dev     libopencv-dev     libatlas3-base     libatlas-base-dev     libjasper-dev     libilmbase12     libopenexr22     libavformat-dev     libswscale-dev     libqtgui4     libqt4-test     libavdevice-dev     libavfilter-dev     libavcodec-dev   && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100

Hi,

your problem here is that you’re still trying to use Stretch.
That release is old enough that it was removed from the Debian and Raspbian main repositories.

If you REALLY want to use Stretch, you can try fixing the repository list by adding archive.debian.org and/or legacy.raspbian.org.

To do this, edit your Dockerfile.template, and add this right after the FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run line:

RUN sed -ri 's_http[s]?://(deb|security)\.debian_https://archive\.debian_g' /etc/apt/sources.list
RUN sed -ri 's_http[s]?://(deb|archive|security)\.raspbian_https://legacy\.raspbian_g' /etc/apt/sources.list

I don’t have a camera either, so I can’t help fully debug it; but this should at least help move things forward a bit :slight_smile: .

1 Like

Thanks, I’ve Just tried to add it to the Balena Cam and it is showing this error:

[balena-cam]  Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/main/binary-armhf/Packages  404  Not Found
[balena-cam]  
[balena-cam]  E: Failed to fetch http://deb.debian.org/debian/dists/stretch/main/binary-armhf/Packages  404  Not Found
[balena-cam]
[balena-cam]  E
[balena-cam]  : Failed to fetch http://deb.debian.org/debian/dists/stretch-updates/main/binary-armhf/Packages  404  Not Found
[balena-cam]
[balena-cam]  E
[balena-cam]  : Failed to fetch http://archive.raspbian.org/raspbian/dists/stretch/contrib/binary-armhf/Packages  404  Not Found
[balena-cam]  
[balena-cam]  E: 
[balena-cam]  Some index files failed to download. They have been ignored, or old ones used instead.
[balena-cam]  
[balena-cam]  Removing intermediate container 90be0867853e
[balena-cam]  The command '/bin/sh -c apt-get update &&   apt-get install -yq     python3     python3-dev     python3-pip     python3-setuptools     gstreamer-1.0     v4l-utils     libopus-dev     libvpx-dev     libsrtp2-dev     libopencv-dev     libatlas3-base     libatlas-base-dev     libjasper-dev     libilmbase12     libopenexr22     libavformat-dev     libswscale-dev     libqtgui4     libqt4-test     libavdevice-dev     libavfilter-dev     libavcodec-dev   && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Info]        Uploading images
[Success]     Successfully uploaded images
[Error]       Some services failed to build:
[Error]         Service: balena-cam
[Error]           Error: The command '/bin/sh -c apt-get update &&   apt-get install -yq     python3     python3-dev     python3-pip     python3-setuptools     gstreamer-1.0     v4l-utils     libopus-dev     libvpx-dev     libsrtp2-dev     libopencv-dev     libatlas3-base     libatlas-base-dev     libjasper-dev     libilmbase12     libopenexr22     libavformat-dev     libswscale-dev     libqtgui4     libqt4-test     libavdevice-dev 
    libavfilter-dev     libavcodec-dev   && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Info]        Built on c617b98
[Error]       Not deploying release.
Remote build failed

PS C:\Users\aliso\Downloads\balena-cam-master> 

This is the dockerfile i attempted to upload:

FROM balenalib/%%BALENA_MACHINE_NAME%%-debian:stretch-20190612
RUN sed -ri 's_http[s]?://(deb|security)\.debian_https://archive\.debian_g' /etc/apt/sources.list
RUN sed -ri 's_http[s]?://(deb|archive|security)\.raspbian_https://legacy\.raspbian_g' /etc/apt/sources.list

# Install dependencies
RUN apt-get update && \
  apt-get install -yq \
    python3 \
    python3-dev \
    python3-pip \
    python3-setuptools \
    gstreamer-1.0 \
    v4l-utils \
    libopus-dev \
    libvpx-dev \
    libsrtp2-dev \
    libopencv-dev \
    libatlas3-base \
    libatlas-base-dev \
    libjasper-dev \
    libilmbase12 \
    libopenexr22 \
    libavformat-dev \
    libswscale-dev \
    libqtgui4 \
    libqt4-test \
    libavdevice-dev \
    libavfilter-dev \
    libavcodec-dev \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

# Enable the v4l2 driver for the Raspberry Pi camera
#RUN printf "bcm2835-v4l2\n" >> /etc/modules
RUN pip3 install --upgrade pip 
RUN pip3 install async-timeout av==6.1.0
RUN pip3 install pyee==8.2.2 aiohttp aiohttp_basicauth==0.1.3 aioice==0.6.10 aiortc==0.9.11 numpy==1.15.4 opencv-python==3.4.4.19 --index-url https://www.piwheels.org/simple

WORKDIR /usr/src/app

COPY ./app/ /usr/src/app/

CMD ["python3", "/usr/src/app/server.py"]

Hello @Jake1 is there any reason why you need to use balenalib/%%BALENA_MACHINE_NAME%%-debian:stretch-20190612 ?

As @TJvV mentioned did you try to use bullseye instead of stretch?

Hi,

It sounds like your output doesn’t quite match your Dockerfile.template ?

I found two little bugs in my last suggestion.
It seems the old apt doesn’t have https transport enabled; changing the new URLs to http should help.
Also stretch-updates was removed / replaced with stretch/updates; disabling that repo seems to work.

With the new modifications, your Dockerfile.template should look like:

FROM balenalib/%%BALENA_MACHINE_NAME%%-debian:stretch-20190612

# Change apt repositories to archive.debian.org and legacy.raspbian.org; disable stretch-updates
RUN sed -ri 's_http[s]?://(deb|security)\.debian_http://archive\.debian_g' /etc/apt/sources.list
RUN sed -ri 's_^(.*)(debian.org)(.*)(stretch-updates)(.*)$_# \0_g' /etc/apt/sources.list
RUN sed -ri 's_http[s]?://(deb|archive|security)\.raspbian_http://legacy\.raspbian_g' /etc/apt/sources.list

# Install dependencies
RUN apt-get update && \
  apt-get install -yq \
    python3 \
    python3-dev \
    python3-pip \
    python3-setuptools \
    gstreamer-1.0 \
    v4l-utils \
    libopus-dev \
    libvpx-dev \
    libsrtp2-dev \
    libopencv-dev \
    libatlas3-base \
    libatlas-base-dev \
    libjasper-dev \
    libilmbase12 \
    libopenexr22 \
    libavformat-dev \
    libswscale-dev \
    libqtgui4 \
    libqt4-test \
    libavdevice-dev \
    libavfilter-dev \
    libavcodec-dev \
  && apt-get clean && rm -rf /var/lib/apt/lists/*

# Enable the v4l2 driver for the Raspberry Pi camera
#RUN printf "bcm2835-v4l2\n" >> /etc/modules
RUN pip3 install --upgrade pip
RUN pip3 install async-timeout av==6.1.0
RUN pip3 install pyee==8.2.2 aiohttp aiohttp_basicauth==0.1.3 aioice==0.6.10 aiortc==0.9.11 numpy==1.15.4 opencv-python==3.4.4.19 --index-url https://www.piwheels.org/simple

WORKDIR /usr/src/app

COPY ./app/ /usr/src/app/

CMD ["python3", "/usr/src/app/server.py"]

Note: I only tried building it locally with balena build -d raspberrypi3 -A armv7hf --emulated

With a little bit of luck, I should have some time later this week to see if I can get a bullseye build.

1 Like

Just tried configuring to your suggestions, However it is showing errors:

[balena-cam]  Get:8 http://archive.raspberrypi.org/debian bullseye/main arm64 Packages [307 kB]
[balena-cam]  Fetched 8778 kB in 1s (8088 kB/s)
[balena-cam]  Reading package lists...
[balena-cam]  W: Skipping acquire of configured file 'ui/binary-arm64/Packages' as repository 'http://archive.raspberrypi.org/debian bullseye InRelease' doesn't have the component 'ui' (component misspelt in sources.list?)
[balena-cam]  
[balena-cam]  Reading package lists...
[balena-cam]  Building dependency tree...
[balena-cam]  Reading state information...
[balena-cam]  Package libqtgui4 is not available, but is referred to by another package.
[balena-cam]  This may mean that the package is missing, has been obsoleted, or
[balena-cam]  is only available from another source
[balena-cam]  E: Unable to locate package libjasper-dev
[balena-cam]  E: Unable to locate package libilmbase12
[balena-cam]  E: Unable to locate package libopenexr22
[balena-cam]  E: Package 'libqtgui4' has no installation candidate
[balena-cam]  E: Unable to locate package libqt4-test
[balena-cam]
[balena-cam]  Removing intermediate container 108d075d1496
[balena-cam]  The command '/bin/sh -c apt-get update &&   apt-get install -yq     python3     python3-dev     python3-pip     python3-setuptools     gstreamer-1.0     v4l-utils     libopus-dev     libvpx-dev     libsrtp2-dev     libopencv-dev     libatlas3-base     libatlas-base-dev     libjasper-dev     libilmbase12     libopenexr22     libavformat-dev     libswscale-dev     libqtgui4     libqt4-test     libavdevice-dev     libavfilter-dev     libavcodec-dev   && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Success]     Successfully uploaded images
[Error]       Some services failed to build:
[Error]         Service: balena-cam
[Error]           Error: The command '/bin/sh -c apt-get update &&   apt-get install -yq     python3     python3-dev     python3-pip     python3-setuptools     gstreamer-1.0     v4l-utils     libopus-dev     libvpx-dev     libsrtp2-dev     libopencv-dev     libatlas3-base     libatlas-base-dev     libjasper-dev     libilmbase12     libopenexr22     libavformat-dev     libswscale-dev     libqtgui4     libqt4-test     libavdevice-dev     libavfilter-dev     libavcodec-dev   && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100
[Info]        Built on 4b2d7d0
[Error]       Not deploying release.
Remote build failed

@TJvV I’ll be looking forward to seeing your results with bullseye!

1 Like

Hi,

I did some testing, using your repo (Rail-Vista-Balena) as a basis.

I ran into some issues with the Bullseye build (something weird with pip), so I went straight on to Bookworm.

The following seems to get to the point where it gives an IndexOutOfBounds when trying to initialize the camera in the application:

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

# Install system packages required for compiling native extensions
RUN install_packages \
    build-essential \
    libjpeg-dev \
    git \
    libatlas-base-dev \
    python3-dev \
    python3-pip

# Upgrade pip, setuptools, and wheel to ensure we have the latest versions
RUN python3 -m pip install --upgrade pip setuptools wheel

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/app.py"]

I think it’s probably a good idea to start with this setup and see what does and doesn’t work regarding the camera.
Once that’s up and running, you can see which versions of the other python libraries (aiohttp etc) you need for your application.

I have also modified the Dockerfile.template from balena-cam; in that one I tried to move as much as possible to the Debian system packages; but couldn’t find a substitute for one library (aiohttp_basicauth), so ended up creating a virtual environment for pip to use.

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

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

# Install dependencies
RUN install_packages \
    python3-aiohttp \
    python3-aioice \
    python3-aiortc \
    python3-async-timeout \
    python3-av \
    python3-libcamera \
    python3-numpy \
    python3-opencv \
    python3-picamera2 \
    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

# 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"

# Add libraries that don't have system packages
RUN pip install aiohttp_basicauth

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

I hope this helps a bit :slight_smile:

1 Like

Thanks for your reply. I’ve just attempted to re-upload the code with your suggestion. Unfortunately it failed but it also mentioned in the logs

Unable to locate package python3-libcamera

Is it possible to git clone the libcamera Library (GitHub - raspberrypi/libcamera) ?

[Rail-Vista-Main]  Step 3/12 : RUN install_packages     python3-aiohttp     python3-aioice     python3-aiortc     python3-async-timeout     python3-av     python3-libcamera     python3-numpy     python3-opencv     python3-picamera2     python3-pyee
[Rail-Vista-Main]   ---> Running in 957fdb000269
[Rail-Vista-Main]  Reading package lists...
[Rail-Vista-Main]  Building dependency tree...
[Rail-Vista-Main]  Reading state information...
[Rail-Vista-Main]  E: Unable to locate package python3-libcamera
[Rail-Vista-Main]  E: Unable to locate package python3-picamera2
[Rail-Vista-Main]
[Rail-Vista-Main]  apt failed, retrying
[Rail-Vista-Main]  Reading package lists...
[Rail-Vista-Main]  Building dependency tree...
[Rail-Vista-Main]  Reading state information...
[Rail-Vista-Main]  E: Unable to locate package python3-libcamera
[Rail-Vista-Main]  E: Unable to locate package python3-picamera2
[Rail-Vista-Main]
[Rail-Vista-Main]  apt failed, retrying
[Rail-Vista-Main]  Reading package lists...
[Rail-Vista-Main]  Building dependency tree...
[Rail-Vista-Main]  Reading state information...
[Rail-Vista-Main]  E: Unable to locate package python3-libcamera
[Rail-Vista-Main]  E: Unable to locate package python3-picamera2
[Rail-Vista-Main]
[Rail-Vista-Main]  Removing intermediate container 957fdb000269
[Rail-Vista-Main]  The command '/bin/sh -c install_packages     python3-aiohttp     python3-aioice     python3-aiortc     python3-async-timeout     python3-av     python3-libcamera     python3-numpy     python3-opencv     python3-picamera2     python3-pyee' returned a non-zero code: 100
[Info]             Uploading images
[Success]          Successfully uploaded images
[Error]            Some services failed to build:
[Error]              Service: Rail-Vista-Main
[Error]                Error: The command '/bin/sh -c install_packages     python3-aiohttp     python3-aioice     python3-aiortc     python3-async-timeout     python3-av     python3-libcamera     python3-numpy     python3-opencv     python3-picamera2     python3-pyee' returned a non-zero code: 100
[Info]             Built on 4b2d7d0
[Error]            Not deploying release.
Remote build failed
1 Like