跳至内容

Quick Start Guide: Raspberry Pi with Ultralytics YOLO11

This comprehensive guide provides a detailed walkthrough for deploying Ultralytics YOLO11 on Raspberry Pi devices. Additionally, it showcases performance benchmarks to demonstrate the capabilities of YOLO11 on these small and powerful devices.



观看: Raspberry Pi 5 的更新和改进。

备注

本指南已在运行最新 Raspberry PiOS Bookworm(Debian 12)的 Raspberry Pi 4 和 Raspberry Pi 5 上进行了测试。对于较旧的 Raspberry Pi 设备,如 Raspberry Pi 3,只要安装了相同的 Raspberry Pi OS Bookworm,使用本指南也可正常工作。

什么是 Raspberry Pi?

Raspberry Pi 是一种小型、经济实惠的单板计算机。从业余爱好者的家庭自动化到工业用途,它在各种项目和应用中广受欢迎。Raspberry Pi 板能够运行各种操作系统,并提供 GPIO(通用输入/输出)引脚,可轻松与传感器、执行器和其他硬件组件集成。它们有不同的型号,规格也各不相同,但都具有相同的基本设计理念,即低成本、紧凑型和多功能。

树莓派系列比较

树莓派 3树莓派 4树莓派 5
CPUBroadcom BCM2837、Cortex-A53 64 位 SoCBroadcom BCM2711、Cortex-A72 64 位 SoCBroadcom BCM2712、Cortex-A76 64 位 SoC
CPU 最大频率1.4GHz1.8GHz2.4GHz
GPUVideocore IVVideocore VI视频核心 VII
GPU 最大频率400Mhz500Mhz800MHz
内存1GB LPDDR2 SDRAM1GB、2GB、4GB、8GB LPDDR4-3200 SDRAM4GB, 8GB LPDDR4X-4267 SDRAM
PCIe不适用不适用1xPCIe 2.0 接口
最大功率2.5A@5V3A@5V5A@5V(启用 PD)

什么是 Raspberry Pi OS?

Raspberry Pi OS(原名 Raspbian)是一个类 Unix 操作系统,基于 Debian GNU/Linux 发行版,适用于 Raspberry Pi 基金会发行的 Raspberry Pi 系列紧凑型单板计算机。Raspberry Pi OS 针对配备 ARM CPU 的 Raspberry Pi 进行了高度优化,并使用经过修改的 LXDE 桌面环境和 Openbox 堆叠窗口管理器。Raspberry Pi OS 正在积极开发中,重点是提高尽可能多的 Debian 软件包在 Raspberry Pi 上的稳定性和性能。

将 Raspberry Pi 操作系统闪存到 Raspberry Pi 上

拿到 Raspberry Pi 后,要做的第一件事就是用 Raspberry Pi OS 闪存 micro-SD 卡,插入设备并启动操作系统。按照RaspberryPi 提供的详细入门文档,为首次使用设备做好准备。

设置Ultralytics

There are two ways of setting up Ultralytics package on Raspberry Pi to build your next Computer Vision project. You can use either of them.

从 Docker 开始

The fastest way to get started with Ultralytics YOLO11 on Raspberry Pi is to run with pre-built docker image for Raspberry Pi.

执行下面的命令,调出 Docker 容器并在 Raspberry Pi 上运行。这是基于arm64v8/debiandocker 镜像,其中包含 Python3 环境下的 Debian 12 (Bookworm)。

t=ultralytics/ultralytics:latest-arm64 && sudo docker pull $t && sudo docker run -it --ipc=host $t

完成上述步骤后,请跳至在 Raspberry Pi 上使用NCNN 部分

不使用 Docker 启动

安装Ultralytics 软件包

Here we will install Ultralytics package on the Raspberry Pi with optional dependencies so that we can export the PyTorch models to other different formats.

  1. 更新软件包列表,安装 pip 并升级到最新版本

    sudo apt update
    sudo apt install python3-pip -y
    pip install -U pip
    
  2. 安装 ultralytics pip 软件包与可选依赖项

    pip install ultralytics[export]
    
  3. 重启设备

    sudo reboot
    

在 Raspberry Pi 上使用NCNN

在Ultralytics.NET 支持的所有模型导出格式中,.NET 是最受欢迎的、 NCNN由于NCNN 针对移动/嵌入式平台(如 ARM 架构)进行了高度优化,因此在使用 Raspberry Pi 设备时可提供最佳推理性能。因此,我们建议在使用 Raspberry Pi 时使用NCNN 。

将模型转换为NCNN 并运行推理

The YOLO11n model in PyTorch format is converted to NCNN to run inference with the exported model.

示例

from ultralytics import YOLO

# Load a YOLO11n PyTorch model
model = YOLO("yolo11n.pt")

# Export the model to NCNN format
model.export(format="ncnn")  # creates 'yolo11n_ncnn_model'

# Load the exported NCNN model
ncnn_model = YOLO("yolo11n_ncnn_model")

# Run inference
results = ncnn_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLO11n PyTorch model to NCNN format
yolo export model=yolo11n.pt format=ncnn  # creates 'yolo11n_ncnn_model'

# Run inference with the exported model
yolo predict model='yolo11n_ncnn_model' source='https://ultralytics.com/images/bus.jpg'

提示

有关支持的导出选项的详细信息,请访问Ultralytics 部署选项文档页面

Raspberry Pi 5 YOLO11 Benchmarks

YOLO11 benchmarks were run by the Ultralytics team on nine different model formats measuring speed and accuracy: PyTorch, TorchScript, ONNX, OpenVINO, TF SavedModel, TF GraphDef, TF Lite, PaddlePaddle, NCNN. Benchmarks were run on a Raspberry Pi 5 at FP32 precision with default input image size of 640.

对比图表

We have only included benchmarks for YOLO11n and YOLO11s models because other models sizes are too big to run on the Raspberry Pis and does not offer decent performance.

YOLO11 benchmarks on RPi 5

详细对照表

The below table represents the benchmark results for two different models (YOLO11n, YOLO11s) across nine different formats (PyTorch, TorchScript, ONNX, OpenVINO, TF SavedModel, TF GraphDef, TF Lite, PaddlePaddle, NCNN), running on a Raspberry Pi 5, giving us the status, size, mAP50-95(B) metric, and inference time for each combination.

性能

格式现状磁盘大小(MB)mAP50-95(B)推理时间(毫秒/分钟)
PyTorch5.40.61524.828
TorchScript10.50.6082666.874
ONNX10.20.6082181.818
OpenVINO10.40.6082530.224
TF SavedModel25.80.6082405.964
TF GraphDef10.30.6082473.558
TF 轻型10.30.6082324.158
PaddlePaddle20.40.6082644.312
NCNN10.20.610693.938
格式现状磁盘大小(MB)mAP50-95(B)推理时间(毫秒/分钟)
PyTorch18.40.75261226.426
TorchScript36.50.74161507.95
ONNX36.30.7416415.24
OpenVINO36.40.74161167.102
TF SavedModel91.10.7416776.14
TF GraphDef36.40.74161014.396
TF 轻型36.40.7416845.934
PaddlePaddle72.50.74161567.824
NCNN36.20.7419197.358

复制我们的结果

要在所有导出格式上重现上述Ultralytics 基准,请运行此代码:

示例

from ultralytics import YOLO

# Load a YOLO11n PyTorch model
model = YOLO("yolo11n.pt")

# Benchmark YOLO11n speed and accuracy on the COCO8 dataset for all all export formats
results = model.benchmarks(data="coco8.yaml", imgsz=640)
# Benchmark YOLO11n speed and accuracy on the COCO8 dataset for all all export formats
yolo benchmark model=yolo11n.pt data=coco8.yaml imgsz=640

请注意,基准测试结果可能会因系统的具体硬件和软件配置以及运行基准测试时系统的当前工作量而有所不同。为获得最可靠的结果,请使用包含大量图像的数据集,即 data='coco8.yaml' (4 val images), ordata='coco.yaml'` (5000 val images)。

使用 Raspberry Pi 摄像头

When using Raspberry Pi for Computer Vision projects, it can be essentially to grab real-time video feeds to perform inference. The onboard MIPI CSI connector on the Raspberry Pi allows you to connect official Raspberry PI camera modules. In this guide, we have used a Raspberry Pi Camera Module 3 to grab the video feeds and perform inference using YOLO11 models.

备注

Raspberry Pi 5 使用比 Raspberry Pi 4 更小的 CSI 接头(15 针与 22 针),因此您需要15 针到 22 针的转接线来连接 Raspberry Pi 摄像头。

测试相机

将摄像头连接到 Raspberry Pi 后执行以下命令。你应该能看到摄像机的实时视频画面,大约持续 5 秒钟。

rpicam-hello

用照相机推理

There are 2 methods of using the Raspberry Pi Camera to inference YOLO11 models.

使用方法

我们可以使用 picamera2which comes pre-installed with Raspberry Pi OS to access the camera and inference YOLO11 models.

示例

import cv2
from picamera2 import Picamera2

from ultralytics import YOLO

# Initialize the Picamera2
picam2 = Picamera2()
picam2.preview_configuration.main.size = (1280, 720)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()
picam2.configure("preview")
picam2.start()

# Load the YOLO11 model
model = YOLO("yolo11n.pt")

while True:
    # Capture frame-by-frame
    frame = picam2.capture_array()

    # Run YOLO11 inference on the frame
    results = model(frame)

    # Visualize the results on the frame
    annotated_frame = results[0].plot()

    # Display the resulting frame
    cv2.imshow("Camera", annotated_frame)

    # Break the loop if 'q' is pressed
    if cv2.waitKey(1) == ord("q"):
        break

# Release resources and close windows
cv2.destroyAllWindows()

我们需要用 rpicam-vid 这样我们就可以在以后推理时将此流 URL 用作输入。执行以下命令启动 TCP 流。

rpicam-vid -n -t 0 --inline --listen -o tcp://127.0.0.1:8888

了解更多 rpicam-vid 使用 Raspberry Pi 官方文档

示例

from ultralytics import YOLO

# Load a YOLO11n PyTorch model
model = YOLO("yolo11n.pt")

# Run inference
results = model("tcp://127.0.0.1:8888")
yolo predict model=yolo11n.pt source="tcp://127.0.0.1:8888"

提示

如果您想更改图像/视频输入类型,请查看我们的推理源文档

使用 Raspberry Pi 的最佳实践

There are a couple of best practices to follow in order to enable maximum performance on Raspberry Pis running YOLO11.

  1. 使用固态硬盘

    在全天候持续使用 Raspberry Pi 时,建议使用固态硬盘作为系统的固态硬盘,因为 SD 卡无法承受连续写入,可能会损坏。有了 Raspberry Pi 5 的板载 PCIe 接口,现在就可以使用适配器(如Raspberry Pi 5 的 NVMe Base)连接固态硬盘了。

  2. 无图形用户界面的 Flash

    闪存 Raspberry Pi OS 时,可以选择不安装桌面环境(Raspberry Pi OS Lite),这样可以节省设备的内存,为计算机视觉处理留出更多空间。

下一步工作

Congratulations on successfully setting up YOLO on your Raspberry Pi! For further learning and support, visit Ultralytics YOLO11 Docs and Kashmir World Foundation.

致谢和引用

本指南最初由 Daan Eeltink 为克什米尔世界基金会(Kashmir World Foundation)制作,该组织致力于利用YOLO 保护濒危物种。我们对他们在物体检测技术领域的开拓性工作和教育重点表示感谢。

有关克什米尔世界基金会活动的更多信息,请访问其网站

常见问题

How do I set up Ultralytics YOLO11 on a Raspberry Pi without using Docker?

To set up Ultralytics YOLO11 on a Raspberry Pi without Docker, follow these steps:

  1. 更新软件包列表并安装 pip:
    sudo apt update
    sudo apt install python3-pip -y
    pip install -U pip
    
  2. 安装Ultralytics 软件包及可选依赖项:
    pip install ultralytics[export]
    
  3. 重新启动设备以应用更改:
    sudo reboot
    

有关详细说明,请参阅 "不使用 Docker 启动"部分。

Why should I use Ultralytics YOLO11's NCNN format on Raspberry Pi for AI tasks?

Ultralytics YOLO11's NCNN format is highly optimized for mobile and embedded platforms, making it ideal for running AI tasks on Raspberry Pi devices. NCNN maximizes inference performance by leveraging ARM architecture, providing faster and more efficient processing compared to other formats. For more details on supported export options, visit the Ultralytics documentation page on deployment options.

How can I convert a YOLO11 model to NCNN format for use on Raspberry Pi?

You can convert a PyTorch YOLO11 model to NCNN format using either Python or CLI commands:

示例

from ultralytics import YOLO

# Load a YOLO11n PyTorch model
model = YOLO("yolo11n.pt")

# Export the model to NCNN format
model.export(format="ncnn")  # creates 'yolo11n_ncnn_model'

# Load the exported NCNN model
ncnn_model = YOLO("yolo11n_ncnn_model")

# Run inference
results = ncnn_model("https://ultralytics.com/images/bus.jpg")
# Export a YOLO11n PyTorch model to NCNN format
yolo export model=yolo11n.pt format=ncnn  # creates 'yolo11n_ncnn_model'

# Run inference with the exported model
yolo predict model='yolo11n_ncnn_model' source='https://ultralytics.com/images/bus.jpg'

更多详情,请参阅在 Raspberry Pi 上使用NCNN 部分。

What are the hardware differences between Raspberry Pi 4 and Raspberry Pi 5 relevant to running YOLO11?

主要区别包括:

  • CPU:Raspberry Pi 4 使用 Broadcom BCM2711、Cortex-A72 64 位 SoC,而 Raspberry Pi 5 使用 Broadcom BCM2712、Cortex-A76 64 位 SoC。
  • CPU 最高频率:Raspberry Pi 4 的最大频率为 1.8GHz,而 Raspberry Pi 5 则达到 2.4GHz。
  • 内存Raspberry Pi 4 提供高达 8GB 的 LPDDR4-3200 SDRAM,而 Raspberry Pi 5 采用 LPDDR4X-4267 SDRAM,有 4GB 和 8GB 两种规格。

These enhancements contribute to better performance benchmarks for YOLO11 models on Raspberry Pi 5 compared to Raspberry Pi 4. Refer to the Raspberry Pi Series Comparison table for more details.

How can I set up a Raspberry Pi Camera Module to work with Ultralytics YOLO11?

There are two methods to set up a Raspberry Pi Camera for YOLO11 inference:

  1. 使用 picamera2:

    import cv2
    from picamera2 import Picamera2
    
    from ultralytics import YOLO
    
    picam2 = Picamera2()
    picam2.preview_configuration.main.size = (1280, 720)
    picam2.preview_configuration.main.format = "RGB888"
    picam2.preview_configuration.align()
    picam2.configure("preview")
    picam2.start()
    
    model = YOLO("yolo11n.pt")
    
    while True:
        frame = picam2.capture_array()
        results = model(frame)
        annotated_frame = results[0].plot()
        cv2.imshow("Camera", annotated_frame)
    
        if cv2.waitKey(1) == ord("q"):
            break
    
    cv2.destroyAllWindows()
    
  2. 使用 TCP 流

    rpicam-vid -n -t 0 --inline --listen -o tcp://127.0.0.1:8888
    
    from ultralytics import YOLO
    
    model = YOLO("yolo11n.pt")
    results = model("tcp://127.0.0.1:8888")
    

有关详细设置说明,请访问 "使用摄像头推理"部分。

📅 Created 1 year ago ✏️ Updated 1 month ago

评论