Real-time Object Detection with Raspberry Pi AI HAT+
Summary: This article provides a comprehensive guide to implementing real-time object detection using the Raspberry Pi AI HAT+ and a Raspberry Pi 5. It details the setup process from OS installation and hardware configuration to leveraging the Viam platform for machine management, webcam integration, and deploying YOLO-based vision services. The tutorial culminates in creating a live traffic dashboard, offering a practical example for embedding AI capabilities into standalone projects.
Key Takeaways
- Accessible AI Integration: Learn to seamlessly integrate the Raspberry Pi AI HAT+ into embedded projects, moving beyond desktop GUI demos for practical, standalone applications.
- Comprehensive Setup: Follow a step-by-step guide covering Raspberry Pi OS installation, SSH access, PCIe Gen 3.0 enablement, Hailo-all package setup, and USB webcam integration.
- Viam for Streamlined Deployment: Utilize the Viam app to configure your Raspberry Pi machine, webcam, Hailo Runtime vision service, and object detection sensors, simplifying complex AI deployments.
- Real-time Monitoring Dashboard: Build a live tele-operations dashboard to visualize real-time video streams and object detection data, such as traffic analysis, from your edge device.
Detailed Overview
As an enthusiast of Raspberry Pi projects, the prospect of integrating AI models without performance bottlenecks using the Raspberry Pi AI HAT+ was exciting. However, the existing documentation primarily focused on GUI app demos, which wasn't ideal for embedded, standalone applications. This guide addresses that gap by providing a reusable module and a step-by-step tutorial to set up real-time object detection. We'll build a live traffic dashboard using a Raspberry Pi 5, the AI HAT+, a USB webcam, and the Viam platform to showcase its capabilities.
Essential Supplies for Your AI Project
To get started with this project, you'll need the following components:
- A free Viam account
- 1x Raspberry Pi 5 w/ active cooler (active cooler is recommended for best performance)
- 1x microSD card
- 1x Power supply for your Pi
- 1x Raspberry Pi AI HAT+ (either 13 TOPS or 26 TOPS version will work)
- 1x USB webcam
Follow the official documentation for instructions on installing the AI HAT+ on your Raspberry Pi 5.
Step 1: Setting Up Your Raspberry Pi
The Raspberry Pi boots from a microSD card, so the first step is to install Raspberry Pi OS.
Install Raspberry Pi OS
- Connect the microSD card to your computer.
- Download and launch the Raspberry Pi Imager.

- Click CHOOSE DEVICE and select Raspberry Pi 5.
- Click CHOOSE OS and select Raspberry Pi OS (64-bit).
- Click CHOOSE STORAGE and select your microSD card.

- Click Next and then EDIT SETTINGS to configure your Pi for remote access.
- Check Set hostname (e.g.,
test) and Set username and password (create a secure username and password). - Check Configure wireless LAN and enter your Wi-Fi SSID and password. Set the Wireless LAN country.

- Go to the SERVICES tab, check Enable SSH, and select Use password authentication.

- Save your settings, confirm to apply OS customization, and erase data.
- Once the installation is complete, remove the microSD card.
Connect with SSH
- Insert the microSD card into your Raspberry Pi and power it on.
- From a terminal, connect via SSH using the hostname and username you set:
ssh <USERNAME>@<HOSTNAME>.local - If prompted, type "yes" to continue connecting and enter your password.

- Update your Raspberry Pi's packages:
sudo apt update sudo apt upgrade
Step 2: Hardware Configuration for AI HAT+
Now, let's set up the AI HAT+ hardware and its dependencies.

Enable PCIe Gen 3.0 speeds for the best performance
- Connect to your Pi via SSH.
- Open the Raspberry Pi Configuration interface:
sudo raspi-config - Select Advanced Options.

- Select PCIe Speed.

- Choose Yes to enable PCIe Gen 3 mode.

- Select Finish and reboot your device:
sudo reboot - Reconnect to the Pi with SSH.
Set up system dependencies
- Install the "hailo-all" package, which includes firmware, drivers, and processing libraries for the AI HAT+:
sudo apt install -y hailo-all - Reboot the device:
sudo reboot - Reconnect to the Pi with SSH.
- Verify the installation:
You should see output similar to this:hailortcli fw-control identifyExecuting on device: 0000:01:00.0 Identifying board Control Protocol Version: 2 Firmware Version: 4.17.0 (release,app,extended context switch buffer) Logger Version: 0 Board Name: Hailo-8 Device Architecture: HAILO8L Serial Number: HLDDLBB234500054 Part Number: HM21LB1C2LAE Product Name: HAILO-8L AI ACC M.2 B+M KEY MODULE EXT TMP
Add your webcam
- Connect your webcam's USB lead to any USB port on your Raspberry Pi.

Step 3: Configuring Your Machine and Peripherals with Viam
Now, we'll configure your Raspberry Pi and its peripherals using the Viam app.
Configure your machine
- In the Viam app, navigate to the LOCATIONS tab, type a name for your machine, and click Add machine.

- Click View setup instructions.
- Select Linux / Aarch64 as the platform and keep
viam-agentas the installation method.
- Run the provided
viam-agentcommand from your Raspberry Pi's SSH prompt.
- The Viam app will indicate when your machine is successfully connected.

Configure your webcam
- In the Viam app under the CONFIGURE tab, click the + icon in the left menu and select Component.
- Choose
cameraand find thewebcammodule. Keep the default namecamera-1. - In the Attributes section, select a
video_path.
- Click Save.
- Expand the TEST section at the bottom of the
camera-1panel to verify the video feed.
Configure your vision service
- In the Viam app under the CONFIGURE tab, click the + icon and select Service.
- Choose
visionand find thehailo-rtmodule. Select "Add module" and keep the default namevision-1. - In the Depends on section, select
camera-1from the "Search resources" dropdown.
- Click Save.
- Expand the TEST section of the
vision-1panel to see images fromcamera-1with object detection boxes.
Configure your detections sensor
- In the Viam app under the CONFIGURE tab, click the + icon and select Component.
- Choose
sensorand find thedetectionsmodule. Keep the default namesensor-1. - In the Attributes section, add the following JSON configuration:
{ "camera": "camera-1", "detector": "vision-1", "labels": ["car", "bus", "person"] }
- In the Data capture section, click "+ Add method". Set "Method" as "Readings" and "Frequency (hz)" as 1. Click "Create data management service" to automatically create
data_manager-1.
- Click Save.
- Expand the TEST section of the
sensor-1panel to see a list of configured labels with detection counts.
Step 4: Building Your Real-time Traffic Dashboard
With all components and services configured, let's create a live tele-operations dashboard.
Configure your teleop workspace
- In the Viam app, return to the home page.
- Under the TELEOP tab, click Create workspace.

- Click on "untitled-workspace" to rename your dashboard.

- Click "Select machine" and choose your configured Raspberry Pi.

Configure camera stream widget
- Click "+ Add widget" and select "Camera stream".

- In the Camera panel, select
camera-1for "Camera name" and keep "Refresh type" as "Live".
Configure time series graph widget
- Click "+ Add widget" and select "Time series".

- Set the "Title" to "Traffic" and "Time range (min)" to 30.
- Under the Lines section, click "+ Add line". Set "Resource name" to
sensor-1, "Capture method" toReadings, "Title" tocars, and "Path" toreadings.car.
- Add new lines for
busandpersonfollowing the same pattern (e.g., "Title":bus, "Path":readings.bus). - Click "Save changes" to view your live camera stream and updating traffic data.

Conclusion & Resources
You have successfully created an edge device capable of real-time object detection and remote monitoring! This project serves as a robust foundation for numerous AI-powered applications. You can expand on this by:
- Developing a smart city control system with simulated traffic lights using LEDs.
- Integrating with smart home platforms like Home Assistant.
- Making your AI system mobile by mounting it on a rover.
The default YOLO-based models can detect a wide range of objects. For a full list, refer to the module's repository. If your project requires different detection capabilities, explore the Hailo Model Zoo or consider retraining a model on a custom dataset.
- GitHub Repository: https://github.com/HipsterBrown/viam-pi-hailo-ml/
- Original Blog Source: https://www.instructables.com/How-to-Use-the-Raspberry-Pi-AI-HAT-for-Real-time-O/
- Viam Codelabs Tutorial: https://codelabs.viam.com/guide/pi-ai-hat/index.html