Skip to content

Tutorials and Resources to support Raspberry Pi 5 (with an emphasis on AI + cameras)

License

Notifications You must be signed in to change notification settings

opensensor/rpi5-resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

rpi5-resources

Repository for resources and tutorials to support Raspberry Pi 5, with an emphasis on AI & cameras.

Introduction

I started this repository to document the steps and processes to utilize Hailo 8L module on Ubuntu 24.04.

First, a bit about my setup: I am using the Pimoroni Base Duo which gives me 2 M2 slots on a baseplate mounted under the Raspberry Pi 5. I have a 1TB NVMe SSD in one slot and the Hailo 8L in the other.

Hardware Setup

More instructions/documentation to be written about setting up the hardware. Right now this guide assumes you have already installed the Pimoroni NVMe Duo baseplate. It is recommended to install both the NVMe SSD and the Hailo 8L at the same time to avoid having to disassemble the baseplate multiple times.

If you want to support this project and need an enclosure for your Pi5 + Pimoroni NVMe Duo base, consider purchasing my design: https://www.printables.com/model/924964-enclosure-rpi-5-pimoroni-nvme-duo-base

Don't have a 3D printer but like my design? I can print you one and ship it for a nominal fee--just contact via www.opensensor.io

Storage Configuration & Installation

Install a bootable SD card in the slot of the Raspberry Pi 5 before installing the NVMe Duo baseplate and PCIe cable. This is important because you'll want a fallback mechanism in case the OS does not boot; additionally you will use this OS to update the firmware before your Pi 5 can boot from an M2 SSD.

Updating to Latest Firmware

Boot from your SD Card and follow these steps.

  1. First, ensure that your Raspberry Pi SD Card runs the latest software. Run the following command to update:

    sudo apt update && sudo apt full-upgrade
  2. Next, ensure that your Raspberry Pi firmware is up-to-date. Run the following command to see what firmware you're running:

    sudo rpi-eeprom-update

    You must see 6 December 2023 or a later date, but definitely upgrade to the latest during setup:

    sudo raspi-config

    Under Advanced Options > Bootloader Version, choose Latest. Then, exit raspi-config with Finish or the Escape key.

    Run the following command to update your firmware to the latest version:

    sudo rpi-eeprom-update -a

    Then, reboot with sudo reboot.

Install Ubuntu 24.04

Utilize the Raspberry Pi Imager to install Ubuntu 24.04 on your NVMe SSD. From your booted Raspbian OS SD card, click on the Raspberry Pi icon in the upper left corner, then Accessories > Raspberry Pi Imager. Choose Ubuntu > Ubuntu 24.04 Desktop (64-bit), then select your NVMe SSD as the target. Click Next to write the image and wait for the process to complete.

Ubuntu 24.04 Install

Boot from NVMe SSD

After the Raspberry Pi Imager has completed writing the image to your NVMe SSD, yuu need to configure the Raspberry Pi 5 to boot from the NVMe SSD.

$ sudo raspi-config
Select `Advanced Options` > `Boot Order` > `NVMe/USB Boot` > `Yes`

Boot from NVMe SSD

Now shut down your Raspberry Pi 5 and remove the SD card. Install the NVMe SSD in the Pimoroni NVMe Duo baseplate and connect the PCIe cable to the Raspberry Pi 5. Power on the Raspberry Pi 5 and you should boot into the Ubuntu 24.04 installer.

Note: There is a sporadic bug affecting the Ubuntu installer for the Raspberry Pi 5. If the media installation fails, and kicks you to a login prompt that has no valid username/pw, then you will need to try again. This is a known issue and is being worked on by the Ubuntu team. If you encounter this issue, completely power off and unplug the device; disconnect the PCIe ribbon cable from the pi and reconnect power.
It should now boot from the sdcard; change the boot order back to sdcard in raspi-config, then power off and reconnect the PCIe cable. It may take a few tries to get a successful installation. It took me three tries on my first Raspberry Pi 5 and at the time of writing it took two tries on this second board.

Once you are able to successfully boot Ubuntu 24.04 from the NVMe SSD, you can proceed with additional software installations to support your efforts.

Apt Update & Upgrade

Run the following commands to update and upgrade your Ubuntu 24.04 installation:

sudo apt update
sudo apt full-upgrade

Prerequisite Software Packages

 sudo apt install git build-essential cmake 

Install also the Linux headers for your kernel version:

 sudo apt install linux-headers-$(uname -r)

Hailo 8L Installation

The Hailo 8L is a PCIe add on card that provides an integer based neural network AI accelerator for your Raspberry Pi 5. The Hailo 8L is a PCIe Gen 2 x1 card that is compatible with the Raspberry Pi 5. While official Haiol 8L support targets Raspberry Pi OS, it is possible to install and utilize the Hailo 8L on Ubuntu 24.04 as well.

HailoRT Driver Installation

HailoRT PCIe driver sources can be cloned from GitHub using:

 git clone https://github.com/hailo-ai/hailort-drivers.git

To compile and install the driver, run the following commands from the driver source path:

 cd hailort-drivers/linux/pcie
 make all CFLAGS="-DHAILO_RASBERRY_PIE"
 sudo make install

Load the driver: this needs to be done once, and after installation the driver will be loaded automatically on boot.

 sudo modprobe hailo_pci

After installation, change back to the root hailort-drivers directory and run the following:

 ./download_firmware.sh
  sudo mkdir /lib/firmware/hailo
  sudo mv hailo8_fw.4.17.1.bin /lib/firmware/hailo/hailo8_fw.bin
  sudo cp ./linux/pcie/51-hailo-udev.rules /etc/udev/rules.d/
  sudo udevadm control --reload-rules && sudo udevadm trigger

Note: The download_firmware.sh script will download the latest firmware from the Hailo website. If they publish a newer firmware version, you will need to update the cp command above to place it in the /lib/firmware/hailo directory.

Now is a good time to reboot the system to ensure the driver is loaded and the firmware is available.

 sudo reboot

After reboot, you can verify the driver is loaded by running the following command:

 lsmod | grep hailo

You should see output similar to the following:

 hailo_pci             90112  0

Now check dmesg for any errors:

 dmesg | grep hailo

You should see output similar to:

[    3.712137] hailo_pci: loading out-of-tree module taints kernel.
[    3.712150] hailo_pci: module verification failed: signature and/or required key missing - tainting kernel
[    3.738221] hailo: Init module. driver version 4.17.1
[    3.738336] hailo 0000:03:00.0: Probing on: 1e60:2864...
[    3.738341] hailo 0000:03:00.0: Probing: Allocate memory for device extension, 11600
[    3.738352] hailo 0000:03:00.0: enabling device (0000 -> 0002)
[    3.738361] hailo 0000:03:00.0: Probing: Device enabled
[    3.738377] hailo 0000:03:00.0: Probing: mapped bar 0 - 00000000a885a668 16384
[    3.738381] hailo 0000:03:00.0: Probing: mapped bar 2 - 000000004c612a24 4096
[    3.738385] hailo 0000:03:00.0: Probing: mapped bar 4 - 00000000375a56e7 16384
[    3.738390] hailo 0000:03:00.0: Probing: Setting max_desc_page_size to 4096, (page_size=4096)
[    3.738398] hailo 0000:03:00.0: Probing: Enabled 64 bit dma
[    3.738561] hailo 0000:03:00.0: Probing: Using userspace allocated vdma buffers
[    3.738569] hailo 0000:03:00.0: Disabling ASPM L0s 
[    3.738574] hailo 0000:03:00.0: Successfully disabled ASPM L0s 
[    3.766048] UBSAN: array-index-out-of-bounds in /home/matteius/Projects/hailort-drivers/linux/pcie/../../common/pcie_common.c:351:53
[    3.766086]  hailo_pcie_write_firmware+0x260/0x280 [hailo_pci]
[    3.766103]  hailo_pcie_probe+0x970/0xd98 [hailo_pci]
[    3.766136]  hailo_pcie_module_init+0x98/0xff8 [hailo_pci]
[    4.040998] hailo 0000:03:00.0: Firmware was loaded successfully
[    4.061876] hailo 0000:03:00.0: Probing: Added board 1e60-2864, /dev/hailo0

HailoRT Installation

Using Hailo Runtime with other Linux distributions is possible via source compilation. HailoRT sources can be cloned from GitHub using:

 git clone https://github.com/hailo-ai/hailort.git

Compiling the sources is done with the following command:

 cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config release

Note: This will take a while (~30 minutes) to compile.

The compilation will create two artifacts:

Binary called hailortcli located in build/hailort/hailortcli/

Library called libhailort.so. located in build/hailort/libhailort/src/

Now install hailort modules:

 sudo cmake --build build --config release --target install 
matteius@matteius-rpi5-2:~/Projects/hailort$ sudo cmake --build build --config release --target install
[ 13%] Built target libprotobuf-lite
[ 15%] Built target hef_proto
[ 16%] Built target scheduler_mon_proto
[ 18%] Built target profiler_proto
[ 22%] Built target spdlog
[ 76%] Built target libhailort
[ 79%] Built target DotWriter
[100%] Built target hailortcli
Install the project...
-- Install configuration: "Release"
-- Up-to-date: /usr/local/lib/libhailort.so.4.17.1
-- Up-to-date: /usr/local/lib/libhailort.so
-- Up-to-date: /usr/local/include/hailo/hailort.h
-- Up-to-date: /usr/local/include/hailo/platform.h
-- Up-to-date: /usr/local/include/hailo/hailort.hpp
-- Up-to-date: /usr/local/include/hailo/buffer.hpp
-- Up-to-date: /usr/local/include/hailo/device.hpp
-- Up-to-date: /usr/local/include/hailo/event.hpp
-- Up-to-date: /usr/local/include/hailo/expected.hpp
-- Up-to-date: /usr/local/include/hailo/hailort_common.hpp
-- Up-to-date: /usr/local/include/hailo/hef.hpp
-- Up-to-date: /usr/local/include/hailo/network_group.hpp
-- Up-to-date: /usr/local/include/hailo/stream.hpp
-- Up-to-date: /usr/local/include/hailo/transform.hpp
-- Up-to-date: /usr/local/include/hailo/vstream.hpp
-- Up-to-date: /usr/local/include/hailo/inference_pipeline.hpp
-- Up-to-date: /usr/local/include/hailo/infer_model.hpp
-- Up-to-date: /usr/local/include/hailo/runtime_statistics.hpp
-- Up-to-date: /usr/local/include/hailo/network_rate_calculator.hpp
-- Up-to-date: /usr/local/include/hailo/vdevice.hpp
-- Up-to-date: /usr/local/include/hailo/quantization.hpp
-- Up-to-date: /usr/local/include/hailo/hailort_defaults.hpp
-- Up-to-date: /usr/local/include/hailo/dma_mapped_buffer.hpp
-- Up-to-date: /usr/local/lib/cmake/HailoRT/HailoRTConfig.cmake
-- Up-to-date: /usr/local/lib/cmake/HailoRT/HailoRTConfigVersion.cmake
-- Up-to-date: /usr/local/lib/cmake/HailoRT/HailoRTTargets.cmake
-- Up-to-date: /usr/local/lib/cmake/HailoRT/HailoRTTargets-release.cmake
-- Up-to-date: /usr/local/bin/hailortcli
-- Up-to-date: /usr/local/usr/share/bash-completion/completions/hailortcli

Now you should be able to run hailortcli and see your Hailo 8L device:

matteius@matteius-rpi5-2:~/Projects/hailort$ hailortcli 
A subcommand is required
Run with --help for more information.

matteius@matteius-rpi5-2:~/Projects/hailort$ hailortcli scan
Hailo Devices:
[-] Device: 0000:03:00.0

Hailo Tappas Installation

Before building rpicam-apps with Hailo support, you need to install the Hailo Tappas library. This library is used to interface with the Hailo 8L device and is required for the rpicam-apps to utilize the Hailo 8L.

Start by installing prerequisite packages:

$ sudo apt-get install -y rsync ffmpeg x11-utils python3-dev python3-pip python3-setuptools python3-virtualenv python-gi-dev libgirepository1.0-dev gcc-9 g++-9 cmake git libzmq3-dev

OpenCV Dependency

# Download Opencv and unzip
wget https://github.com/opencv/opencv/archive/4.5.2.zip
unzip 4.5.2.zip

# cd and make build dir
cd opencv-4.5.2
mkdir build
cd build

# Make and install
cmake -DOPENCV_GENERATE_PKGCONFIG=ON \
    -DBUILD_LIST=core,imgproc,imgcodecs,calib3d,features2d,flann \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DWITH_PROTOBUF=OFF -DWITH_QUIRC=OFF \
    -DWITH_WEBP=OFF -DWITH_OPENJPEG=OFF \
    -DWITH_GSTREAMER=OFF -DWITH_GTK=OFF \
    -DOPENCV_DNN_OPENCL=OFF -DBUILD_opencv_python2=OFF \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_PYTHON_EXAMPLES=ON \
    -DCMAKE_INSTALL_PREFIX=/usr/local  ..

num_cores_to_use=$(($(nproc)/2))
make -j$num_cores_to_use
sudo make install

# Update the linker
sudo ldconfig

Gstreamer Dependency

sudo apt-get install -y libcairo2-dev libgirepository1.0-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio gcc-9 g++-9 python-gi-dev

Pyobject Dependency

sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0

Build Tappas

Clone the opensensor fork tappas repository which has a few build patches for Ubuntu 24.04:

git clone https://github.com/opensensor/tappas.git

Now you need to copy the hailort sources from the earlier part of this guide into a hailort/sources directory inside the tappas directory.

cd tappas
mkdir hailort
cp -rf ../hailort hailort/sources

Now you should be able to kick off the install script for tappas:

./install.sh --skip-hailort --target-platform rpi

There are some manual steps where I copy the .pth file and built shared object libraries to the correct locations. I will try to document this better next time I run through the guide.

Building libcamera

A prerequisite for building rpicam-apps is to build libcamera. First, install the following libcamera dependencies:

sudo apt install -y libboost-dev libgnutls28-dev openssl libtiff5-dev pybind11-dev qtbase5-dev libqt5core5a libqt5gui5 libqt5widgets5 meson cmake python3-yaml python3-ply libglib2.0-dev libgstreamer-plugins-base1.0-dev

Next, clone the RaspberryPi libcamera repository:

git clone https://github.com/raspberrypi/libcamera.git

Here are the commands to build and install libcamera dependency:

cd libcamera
meson setup build --buildtype=release -Dpipelines=rpi/vc4,rpi/pisp -Dipas=rpi/vc4,rpi/pisp -Dv4l2=true -Dgstreamer=enabled -Dtest=false -Dlc-compliance=disabled -Dcam=disabled -Dqcam=disabled -Ddocumentation=disabled -Dpycamera=enabled
sudo ninja -C build install

Building rpicam-apps

We recommend checking out the opensensor fork of the rpicam-apps repository which has a few build patches for Ubuntu 24.04 as well some new features for post-processing recording based on the AI detections:

git clone https://github.com/opensensor/rpicam-apps.git

meson setup build -Denable_libav=enabled -Denable_drm=enabled -Denable_egl=enabled -Denable_qt=enabled -Denable_opencv=enabled -Denable_tflite=disabled --reconfigure
meson compile -C build
sudo meson install -C build

Creating a systemd service to record detections

Create a new systemd service to run the video processing application at boot. This service will start the rpicam-vid application with the Hailo 8L post-processing file and record detections every 30 seconds.

$ sudo vim /etc/systemd/system/rpicam.service
[Unit]
Description=RPiCam Video Processing Service
After=network.target

[Service]
Environment=LD_LIBRARY_PATH=/usr/local/lib/aarch64-linux-gnu/rpicam-apps-postproc:$LD_LIBRARY_PATH
ExecStart=rpicam-vid -t 0 --post-process-file /home/matteius/rpicam-apps/assets/hailo_yolov8_inference2.json --width 1920 --height 1080 --lores-width 640 --lores-height 640 --inline 1 --record-detection 30
WorkingDirectory=/home/matteius
User=matteius
Group=matteius
Restart=always

[Install]
WantedBy=multi-user.target

To be able to run the service as your user, you need to add your user to the video group:

sudo vim /etc/udev/rules.d/raspberrypi.rules
SUBSYSTEM=="dma_heap", GROUP="video", MODE="0660"

Reboot

About

Tutorials and Resources to support Raspberry Pi 5 (with an emphasis on AI + cameras)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages