Ultralytics YOLO27:

快速入门指南:搭配 Ultralytics YOLO26 使用 Raspberry Pi#

Raspberry Pi 是一款小巧、经济实惠的计算机,可在边缘端运行 Ultralytics YOLO26,实现实时 目标检测,无需 GPU。本指南将带你在 Raspberry Pi 4 和 5 上部署 YOLO26:刷写操作系统、安装 Ultralytics、导出为 NCNN 以在 ARM 上实现最快的 推理,以及在实时摄像头画面上运行预测。指南还包含八种导出格式的性能基准测试,帮助你根据硬件选择速度与 精度 的最佳平衡。

跳转至设置 Ultralytics导出为 NCNN 并运行推理基准测试摄像头推理



Watch: Raspberry Pi 5 updates and improvements.
注意

本指南已在运行最新 Raspberry Pi OS Bookworm(Debian 12) 的 Raspberry Pi 4 和 Raspberry Pi 5 上完成测试。只要安装了相同的 Raspberry Pi OS Bookworm,本指南预计也适用于 Raspberry Pi 3 等较旧的 Raspberry Pi 设备。

什么是 Raspberry Pi?#

Raspberry Pi 是一款小巧、经济实惠的单板计算机。它已广泛应用于各种项目和场景,从业余爱好者的家庭自动化到工业用途。Raspberry Pi 主板能够运行多种操作系统,并提供 GPIO(通用输入/输出)引脚,方便与传感器、执行器及其他硬件组件集成。Raspberry Pi 有多个型号,规格各不相同,但都秉持低成本、小巧紧凑且用途广泛的基本设计理念。

Raspberry Pi 系列对比#

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

什么是 Raspberry Pi OS?#

Raspberry Pi OS(之前称为 Raspbian)是一款类 Unix 操作系统,基于 Debian GNU/Linux 发行版,面向 Raspberry Pi Foundation 发布的 Raspberry Pi 系列紧凑型单板计算机。Raspberry Pi OS 针对配备 ARM CPU 的 Raspberry Pi 进行了高度优化,并使用经过修改的 LXDE 桌面环境和 Openbox 堆叠式窗口管理器。Raspberry Pi OS 正在持续开发,重点是尽可能提升 Raspberry Pi 上各种 Debian 软件包的稳定性和性能。

将 Raspberry Pi OS 刷写到 Raspberry Pi#

拿到 Raspberry Pi 后,首先要做的是将 Raspberry Pi OS 刷写到 micro-SD 卡中,将其插入设备并启动进入操作系统。请按照 Raspberry Pi 提供的详细入门文档完成设备的首次使用准备。

设置 Ultralytics#

你可以通过两种方式在 Raspberry Pi 上设置 Ultralytics 软件包,用于接下来的计算机视觉项目:

从 Docker 开始#

在 Raspberry Pi 上开始使用 Ultralytics YOLO26 的最快方式,是运行适用于 Raspberry Pi 的预构建 Docker 镜像。

执行以下命令以拉取 Docker 容器并在 Raspberry Pi 上运行。该容器基于 arm64v8/ubuntu Docker 镜像,可在 Python3 环境中运行 Ubuntu 24.04。

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

该 Docker 镜像已包含 Ultralytics,因此你可以直接将模型导出为 NCNN

不使用 Docker 开始#

安装 Ultralytics 包#

下面将在 Raspberry Pi 上安装 Ultralytics 软件包。首次将 PyTorch 模型导出为 NCNN 等格式时,导出依赖项会自动安装,因此这里只需要安装基础软件包。

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

    sudo apt update
    sudo apt install python3-pip -y
    pip install -U pip
  2. 安装 ultralytics pip 包

    pip install ultralytics
  3. 重启设备

    sudo reboot

在 Raspberry Pi 上使用 NCNN#

在 Ultralytics 支持的所有模型导出格式中,NCNN 在 Raspberry Pi 设备上提供了最佳的推理性能,因为它针对 ARM 架构等移动端/嵌入式平台进行了高度优化。将 YOLO26n PyTorch 模型转换为 NCNN,然后使用导出的模型运行推理:

示例
from ultralytics import YOLO

# Load a YOLO26n PyTorch model
model = YOLO("yolo26n.pt")

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

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

# Run inference
results = ncnn_model("https://ultralytics.com/images/bus.jpg")
提示

有关支持的导出选项的更多详情,请参阅模型部署选项指南

YOLO26 相比 YOLO11 的性能提升#

YOLO26 专为 Raspberry Pi 5 等硬件受限设备而设计。与 YOLO11n 相比,在 Raspberry Pi 5 上使用 ONNX 导出模型、输入尺寸为 640 时,YOLO26n 的 FPS 提升约 ~15%(6.79 → 7.79),同时 mAP 更高(40.1 对比 39.5)。下方的表格和图表展示了这一对比。

YOLO26 benchmarks on RPi 5
Benchmarked with Ultralytics 8.4.14
性能
模型mAP50-95(B)推理时间(ms/图像)
YOLO26n40.1128.42
YOLO26s47.8352.84
YOLO26m52.5993.78
YOLO26l54.41259.46
YOLO26x56.92636.26

使用 Ultralytics 8.4.14 完成基准测试。

Raspberry Pi 5 YOLO26 基准测试#

YOLO26 基准测试由 Ultralytics 团队在八种不同模型格式上完成,用于测量速度和精度:PyTorch、TorchScript、ONNX、OpenVINO、MNN、NCNN、ExecuTorch、LiteRT。基准测试在 Raspberry Pi 5 上以 FP32 精度运行,默认输入图像尺寸为 640。

对比图#

我们仅包含 YOLO26n 和 YOLO26s 模型的基准测试,因为其他模型尺寸过大,无法在 Raspberry Pi 上运行,也无法提供良好的性能。

YOLO26 benchmarks on RPi 5
Benchmarked with Ultralytics 8.4.108

详细对比表#

下表展示了两种不同模型(YOLO26n、YOLO26s)在八种不同格式(PyTorch、TorchScript、ONNX、OpenVINO、MNN、NCNN、ExecuTorch、LiteRT)下的基准测试结果。这些模型均在 Raspberry Pi 5 上运行,表中列出了每种组合的状态、大小、mAP50-95(B) 指标和推理时间。

性能
格式状态磁盘占用大小(MB)mAP50-95(B)推理时间(ms/图像)
PyTorch5.30.4760299.09
TorchScript9.80.4734353.20
ONNX9.50.4734125.99
OpenVINO9.60.4734104.55
MNN9.40.474991.87
NCNN9.40.478467.03
ExecuTorch9.40.4772144.83
LiteRT9.80.4730123.30

使用 Ultralytics 8.4.108 完成基准测试

注意

推理时间不包括预处理和后处理。

复现我们的结果#

要在所有导出格式上复现上述 Ultralytics 基准测试,请运行以下代码:

示例
from ultralytics import YOLO

# Load a YOLO26n PyTorch model
model = YOLO("yolo26n.pt")

# Benchmark YOLO26n speed and accuracy on the COCO128 dataset for all export formats
results = model.benchmark(data="coco128.yaml", imgsz=640)

请注意,基准测试结果可能因系统的确切硬件和软件配置,以及运行基准测试时系统的当前工作负载而有所不同。为获得最可靠的结果,请使用包含大量图像的数据集,例如 data='coco.yaml'(5000 张验证图像)。

LiteRT 导出在设备外运行

litert-converter 不提供 Linux aarch64 wheel,因此 format=litert 导出仅支持 Linux x86_64 和 macOS,不支持在 Raspberry Pi 本身上导出。请在 Linux x86_64 机器或 Mac 上导出 .tflite 模型,将其复制到 Raspberry Pi,然后使用 Pi 进行推理。

使用 Raspberry Pi 摄像头#

在使用 Raspberry Pi 进行计算机视觉项目时,获取实时视频流以执行推理可能至关重要。Raspberry Pi 板载的 MIPI CSI 接口允许你连接官方 Raspberry PI 摄像头模块。在本指南中,我们使用了 Raspberry Pi Camera Module 3 获取视频流,并使用 YOLO26 模型执行推理。

提示

详细了解 Raspberry Pi 提供的不同摄像头模块,以及如何开始使用 Raspberry Pi 摄像头模块

注意

Raspberry Pi 5 使用的 CSI 接口比 Raspberry Pi 4 更小(15 针对 22 针),因此你需要一根15 针转 22 针转接线才能连接 Raspberry Pi Camera。

测试摄像头#

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

rpicam-hello
提示

在 Raspberry Pi 官方文档中详细了解 rpicam-hello 的使用方法

使用摄像头进行推理#

使用 Raspberry Pi Camera 对 YOLO26 模型运行推理有两种方法。

使用 Raspberry Pi 摄像头运行推理

我们可以使用 Raspberry Pi OS 预装的 picamera2 访问摄像头,并对 YOLO26 模型运行推理。

示例
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 YOLO26 model
model = YOLO("yolo26n.pt")

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

    # Run YOLO26 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()
提示

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

使用 Raspberry Pi 时的最佳实践#

要让运行 YOLO26 的 Raspberry Pi 实现最佳性能,需要遵循一些最佳实践。

  1. 使用 SSD

    如果 Raspberry Pi 需要 24x7 持续运行,建议将 SSD 用作系统盘,因为 SD 卡无法承受持续写入,可能会损坏。借助 Raspberry Pi 5 板载的 PCIe 接口,你现在可以使用适配器连接 SSD,例如 Raspberry Pi 5 NVMe Base

  2. 不使用 GUI 刷写

    刷写 Raspberry Pi OS 时,你可以选择不安装桌面环境(Raspberry Pi OS Lite),这样可以节省设备上的一部分 RAM,为计算机视觉处理留出更多空间。

  3. 超频 Raspberry Pi

    如果你想在树莓派 5 上运行 Ultralytics YOLO26 模型时获得一点性能提升,可以将 CPU 从基础的 2.4GHz 超频至 3.0GHz,并将 GPU 从 800MHz 超频至 1GHz。如果系统变得不稳定或崩溃,请以 100MHz 为增量降低超频值。请确保做好适当的散热,因为超频会增加发热量并可能导致降频。

    a. 升级软件

    sudo apt update && sudo apt dist-upgrade

    b. 打开配置文件进行编辑

    sudo nano /boot/firmware/config.txt

    c. 在底部添加以下行

    arm_freq=3000
    gpu_freq=1000
    force_turbo=1

    d. 按 CTRL + X,然后按 Y 并按 ENTER 保存并退出

    e. 重启 Raspberry Pi

后续步骤#

你已成功在 Raspberry Pi 上设置 YOLO26。要进一步探索,请查看预测模式以了解更多推理选项,查看导出模式以了解其他部署格式;如果你需要在边缘端使用更强的计算能力,还可以参阅 NVIDIA Jetson 指南。完整文档请访问 Ultralytics YOLO26 文档

致谢与引用#

本指南最初由 Daan Eeltink 为 Kashmir World Foundation 创建。该组织致力于使用 YOLO 保护濒危物种。我们感谢他们在目标检测技术领域的开创性工作和教育重点。

有关 Kashmir World Foundation 活动的更多信息,请访问其网站

常见问题#

  • 要在不使用 Docker 的情况下在 Raspberry Pi 上设置 Ultralytics YOLO26,请按以下步骤操作:

    1. 更新软件包列表并安装 pip
      sudo apt update
      sudo apt install python3-pip -y
      pip install -U pip
    2. 安装 Ultralytics 软件包:
      pip install ultralytics
    3. 重启设备以应用更改:
      sudo reboot

    如需详细说明,请参阅不使用 Docker 开始部分。

  • Ultralytics YOLO26 的 NCNN 格式针对移动和嵌入式平台进行了高度优化,非常适合在 Raspberry Pi 设备上运行 AI 任务。NCNN 充分利用 ARM 架构来最大化推理性能,与其他格式相比可提供更快速、更高效的处理。如需了解支持的导出格式,请参阅模型导出选项

  • 你可以使用 Python 或 CLI 命令将 PyTorch YOLO26 模型转换为 NCNN 格式:

    示例
    from ultralytics import YOLO
    
    # Load a YOLO26n PyTorch model
    model = YOLO("yolo26n.pt")
    
    # Export the model to NCNN format
    model.export(format="ncnn")  # creates 'yolo26n_ncnn_model'
    
    # Load the exported NCNN model
    ncnn_model = YOLO("yolo26n_ncnn_model")
    
    # Run inference
    results = ncnn_model("https://ultralytics.com/images/bus.jpg")

    如需了解详情,请参阅在 Raspberry Pi 上使用 NCNN部分。

  • 主要差异包括:

    • 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 两种版本。

    这些改进有助于提升 YOLO26 模型在 Raspberry Pi 5 上的性能基准,相比 Raspberry Pi 4 表现更好。如需了解详情,请参阅Raspberry Pi 系列对比表格。

  • 为 YOLO26 推理设置 Raspberry Pi Camera 有两种方法:

    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("yolo26n.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("yolo26n.pt")
      results = model("tcp://127.0.0.1:8888")

    如需详细的设置说明,请参阅使用摄像头进行推理部分。

评论