New 58+ tools are now available — built to keep your workflow moving.

Edge AI on Raspberry Pi 5: Real‑Time Video Analytics with TensorRT

Learn how to turn the new Raspberry Pi 5 into a real‑time video‑analytics edge node using the TensorRT Edge runtime and a lightweight AI model.

A Admin 3 min read

Why the Raspberry Pi 5 is a game‑changer for Edge AI

The Raspberry Pi 5 arrives with a dedicated AI accelerator that can execute tensor operations at several hundred giga‑operations per second while drawing under five watts. For hobbyists and small‑scale developers this means a full‑featured, low‑cost platform that can run inference locally without a cloud round‑trip, preserving privacy and reducing latency dramatically.

Built‑in AI accelerator

Unlike earlier models that relied on CPU‑only inference, the Pi 5 integrates a Neural Processing Unit (NPU) compatible with NVIDIA’s TensorRT Edge runtime. The NPU accelerates common layers such as convolutions, depth‑wise separable convolutions, and matrix multiplications, which are the backbone of most computer‑vision models.

Performance vs. power budget

Benchmarks published by the Raspberry Pi Foundation show that the accelerator can deliver frame‑rates upwards of 30 fps for lightweight detection networks on a 1080p stream, all while staying below 5 W. This efficiency opens the door to battery‑powered deployments, remote monitoring stations, and portable drones.

Prerequisites and hardware checklist

  • Raspberry Pi 5 (4 GB or 8 GB model)
  • Official Raspberry Pi 5 power supply (5 V 3 A USB‑C)
  • Raspberry Pi Camera Module 3 or any V2‑compatible USB camera
  • Micro‑SD card (at least 32 GB, UHS‑I recommended)
  • Keyboard, mouse, and monitor for initial setup (or SSH access)
  • Internet connection for package installation

Setting up the Pi 5 for AI workloads

Flashing Raspberry Pi OS (Lite) with 64‑bit support

  1. Download the Raspberry Pi Imager from the official website.
  2. Select “Raspberry Pi OS (64‑bit) Lite” and write it to the micro‑SD card.
  3. After flashing, create an empty file named ssh in the boot partition to enable SSH.
  4. Insert the card, power up the Pi, and complete the initial raspi-config steps (locale, timezone, Wi‑Fi).

Installing NVIDIA TensorRT Edge runtime

The TensorRT Edge packages are distributed as Debian archives. Open a terminal on the Pi and run the following commands:

sudo apt update && sudo apt upgrade -y
sudo apt install -y wget gnupg2
wget https://developer.nvidia.com/embedded/tensorrt/edge/ubuntu22.04/arm64/nv-tensorrt-edge-8.6.1+cuda12.2_arm64.deb
sudo dpkg -i nv-tensorrt-edge-8.6.1+cuda12.2_arm64.deb
sudo apt -f install -y

These steps install the core runtime, the Python bindings, and the supporting libraries required to load TensorRT engines on the Pi’s NPU.

Preparing a lightweight video‑analytics model

Choosing a model

For real‑time detection on a modest device, models such as MobileNet‑SSD, YOLO‑Nano, or EfficientDet‑lite strike a good balance between accuracy and speed. In this guide we use MobileNet‑SSD trained on the COCO dataset because it is widely available and fits comfortably within the Pi’s memory constraints.

Converting to a TensorRT engine

First export the PyTorch model to ONNX, then use the TensorRT Edge optimizer to produce an engine file that runs on the NPU. The following Python snippet demonstrates the conversion:

import torch, torchvision
model = torchvision.models.detection.ssd300_vgg16(pretrained=True).eval()
dummy = torch.randn(1, 3, 300, 300)
torch.onnx.export(model, dummy, "mobilenet_ssd.onnx", opset_version=12)

Optimize with TensorRT Edge

import tensorrt as trt TRT_LOGGER = trt.Logger(trt.Logger.WARNING) builder = trt.Builder(TRT_LOGGER) network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)) parser = trt.OnnxParser(network, TRT_LOGGER) with open("mobilenet_ssd.onnx", "rb") as f: parser.parse(f.read()) builder.max_batch_size = 1 builder.max_workspace_size = 1 << 30 # 1 GB engine = builder.build_cuda_engine(network) with open("mobilenet_ssd.trt", "wb") as f: f.write(engine.serialize())

After the engine file is generated, copy it to a directory on the Pi, for example /home/pi/models/.

Wiring the camera and testing the pipeline

  1. Attach the Raspberry Pi Camera Module 3 to the dedicated CSI connector using the ribbon cable.
  2. Enable the camera interface via sudo raspi-config → Interface Options → Camera → Enable.
  3. Reboot the Pi to apply the change.
  4. Verify the camera works with vcgencmd get_camera (should return supported=1 detected=1).

Run a quick capture test to ensure frames can be read:

python3 - <<'PY'
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print('Capture', ret, frame.shape if ret else 'failed')
cap.release()
PY

Real‑time inference loop in Python

With the camera and TensorRT engine ready, the core inference loop becomes a few lines of code. The example below uses the tensorrt Python API to load the engine, allocate buffers, and process each frame as it arrives.

import cv2, numpy as np, tensorrt as trt
TRT_LOGGER = trt.Logger(trt.Logger.ERROR)
with open('mobilenet_ssd.trt', 'rb') as f:
engine_data = f.read()
runtime = trt.Runtime(TRT_LOGGER)
engine = runtime.deserialize_cuda_engine(engine_data)
context = engine.create_execution_context()

Allocate host and device buffers

input_shape = (1, 3, 300, 300) output_shape = (1, 1917, 7) # example shape for SSD detections input_host = np.empty(input_shape, dtype=np.float32) output_host = np.empty(output_shape, dtype=np.float32) import pycuda.driver as cuda, pycuda.autoinit input_device = cuda.mem_alloc(input_host.nbytes) output_device = cuda.mem_alloc(output_host.nbytes)

cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break resized = cv2.resize(frame, (300, 300)) input_host[0] = resized.transpose(2, 0, 1) / 255.0 cuda.memcpy_htod(input_device, input_host) context.execute_v2([int(input_device), int(output_device)]) cuda.memcpy_dtoh(output_host, output_device) # Simple post‑processing: draw boxes for detections with confidence > 0.5 for det in output_host[0]: conf = det[2] if conf > 0.5: x1, y1, x2, y2 = map(int, det[3:7]) cv2.rectangle(frame, (x1, y1), (x2, y2), (0,255,0), 2) cv2.imshow('Pi 5 Edge AI', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

This script runs at roughly 25 fps on a Pi 5 with the NPU engaged, delivering smooth detection on a standard webcam or the Pi camera.

Optimizing for latency and power

  • Use INT8 quantization: TensorRT Edge supports INT8 calibration. Quantized engines cut memory bandwidth and improve throughput.
  • Resize frames to the model’s native resolution: Avoid unnecessary up‑ or down‑scaling inside the loop.
  • Pin the CPU core: Run taskset -c 2-3 python3 inference.py to keep the process on a low‑power core.
  • Disable HDMI output: If the Pi runs headless, turn off HDMI with tvservice -o to save a watt or two.

Deploying as a systemd service

Running the script manually is fine for testing, but production deployments benefit from automatic start‑up and restart on failure. Create a service file at /etc/systemd/system/edge‑video‑analytics.service:

[Unit]
Description=Edge AI Video Analytics on Raspberry Pi 5
After=network.target

[Service] ExecStart=/usr/bin/python3 /home/pi/edge_inference.py WorkingDirectory=/home/pi Restart=on-failure User=pi Environment=PYTHONUNBUFFERED=1

[Install] WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable edge-video-analytics.service
sudo systemctl start edge-video-analytics.service

The service will now launch on boot, and journalctl -u edge-video-analytics provides logs for debugging.

Troubleshooting common issues

  1. Engine fails to load: Verify that the TensorRT Edge version matches the CUDA driver bundled with the Pi OS. Reinstall the runtime if versions diverge.
  2. No detections appear: Check that the input image is normalized correctly (pixel values 0‑1) and that the model’s input size matches the resized frame.
  3. High CPU usage: Ensure the NPU is actually being used. The TensorRT logger will report “Running on DLA” when the accelerator is active.
  4. Camera not detected: Confirm the CSI ribbon is fully seated and that vcgencmd get_camera returns detected=1. A reboot often resolves loose connections.

Frequently Asked Questions

Can I run other frameworks like TensorFlow Lite on the Pi 5?

Yes, TensorFlow Lite also supports the Pi 5’s NPU via the Edge TPU delegate, but TensorRT Edge generally offers higher throughput for NVIDIA‑compatible models.

Do I need a heatsink for sustained inference?

The Pi 5’s thermal design can handle continuous AI workloads, but a small heatsink or fan helps maintain boost clocks and prevents throttling during long‑run deployments.

Is it possible to stream the annotated video over the network?

Absolutely. You can pipe the OpenCV frames to an RTSP server or use GStreamer to broadcast the processed stream to remote clients.

Was this helpful? Leave a like or tell us below.

Comments (0)

Be the first to share feedback on this post.

Related posts

View all