ei-firmware-raspberry-pi-python

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Edge Impulse Python on Raspberry Pi

Raspberry Pi 上的 Edge Impulse Python

Use the official
edge_impulse_linux
package with a Linux EIM executable built for the Pi's architecture. Do not pass an Arduino library, raw TFLite file, or EIM built for another architecture.
使用官方的
edge_impulse_linux
包,并确保 Linux EIM 可执行文件是为 Pi 的架构构建的。不要传入 Arduino 库、原始 TFLite 文件,或为其他架构构建的 EIM。

Set up

设置

For Raspberry Pi OS:
bash
sudo apt install libatlas-base-dev libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
python3 -m pip install edge_impulse_linux pyaudio
chmod +x model.eim
Keep dependencies in the project's virtual environment when possible. Verify the microphone with ALSA tools before debugging Python.
对于 Raspberry Pi OS:
bash
sudo apt install libatlas-base-dev libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev
python3 -m pip install edge_impulse_linux pyaudio
chmod +x model.eim
尽可能将依赖项保留在项目的虚拟环境中。调试 Python 之前,请使用 ALSA 工具验证麦克风。

Choose the runner

选择 Runner

  • Use
    ImpulseRunner
    for custom numeric data, images, and general models.
  • Use
    AudioImpulseRunner
    for streaming audio and keyword spotting.
  • Use the official SDK image examples as the starting point for camera color conversion, resizing, and feature packing.
Always initialize the runner, inspect
model_info
, and close it in
finally
or a context manager. Model metadata is the source of truth for frequency, frame size, labels, and input shape.
  • 对于自定义数值数据、图像和通用模型,使用
    ImpulseRunner
    。
  • 对于流式音频和关键词识别,使用
    AudioImpulseRunner
    。
  • 以官方 SDK 图像示例为起点,完成摄像头颜色转换、图像缩放和特征打包。
始终初始化 Runner,检查
model_info
,并在
finally
或上下文管理器中将其关闭。模型元数据是采样频率、帧大小、标签和输入形状的权威依据。

General runner pattern

通用 Runner 模式

python
from edge_impulse_linux.runner import ImpulseRunner

runner = ImpulseRunner("model.eim")
try:
    model_info = runner.init()
    # Build one feature window in the exact training order and units.
    features = []
    result = runner.classify(features)
    print(result["result"])
finally:
    runner.stop()
Check the installed SDK version's method signatures before finalizing code. The SDK and examples are canonical when an API differs from this pattern.
python
from edge_impulse_linux.runner import ImpulseRunner

runner = ImpulseRunner("model.eim")
try:
    model_info = runner.init()
    # Build one feature window in the exact training order and units.
    features = []
    result = runner.classify(features)
    print(result["result"])
finally:
    runner.stop()
完成代码前,请检查已安装 SDK 版本的方法签名。如果 API 与此模式存在差异,应以 SDK 和官方示例为准。

USB microphone workflow

USB 麦克风工作流

  1. Enumerate PyAudio devices and select the intended input device explicitly.
  2. Read the model frequency and window requirements from
    model_info
    .
  3. Configure mono PCM with the required sample rate; convert samples exactly as the SDK example expects.
  4. Use
    AudioImpulseRunner
    to maintain the rolling audio window and continuous classifier state.
  5. Handle input overflow by fixing buffering or device configuration, not by silently discarding arbitrary chunks.
  6. Separate inference callbacks from slow network, UI, or file work so audio capture remains timely.
Do not resample unless necessary. If resampling is required, use a deterministic library and verify its output rate and dtype.
  1. 枚举 PyAudio 设备,并明确选择目标输入设备。
  2. 从
    model_info
    中读取模型所需的采样频率和窗口要求。
  3. 按照所需采样率配置单声道 PCM;严格按照 SDK 示例要求转换采样数据。
  4. 使用
    AudioImpulseRunner
    维护滚动音频窗口和持续运行的分类器状态。
  5. 通过修复缓冲机制或设备配置来处理输入溢出,不要静默丢弃任意数据块。
  6. 将推理回调与耗时的网络、UI 或文件操作分离,确保音频采集保持及时。
除非确有必要,否则不要进行重采样。如果必须重采样,请使用确定性库,并验证其输出采样率和 dtype。

Results

结果

Support the model's actual output:
  • Classification and keyword spotting: inspect label scores and apply a user-defined threshold and debounce/cooldown policy.
  • Object detection: iterate nonzero bounding boxes.
  • Anomaly: report the anomaly score when present.
Do not assume scores sum to one for every model type.
支持模型的实际输出:
  • 分类和关键词识别:检查标签分数,并应用用户定义的阈值以及防抖或冷却策略。
  • 目标检测:遍历非零边界框。
  • 异常检测:如果存在异常分数,则报告该分数。
不要假定所有模型类型的分数总和都等于一。

Troubleshoot

故障排查

  • Use
    runner.init(debug=True)
    to surface EIM logs.
  • For very large models, increase the
    ImpulseRunner(..., timeout=SECONDS)
    timeout deliberately.
  • If initialization fails, verify executable permission, CPU architecture, deployment target, and shared-library dependencies.
  • If predictions differ from Studio, compare one identical raw feature window before debugging live capture.
  • For audio errors, verify ALSA/PulseAudio/PipeWire routing, sample format, channels, rate, and device ownership.
  • 使用
    runner.init(debug=True)
    显示 EIM 日志。
  • 对于非常大的模型,应有意识地增大
    ImpulseRunner(..., timeout=SECONDS)
    的超时时间。
  • 如果初始化失败,请验证可执行权限、CPU 架构、部署目标以及共享库依赖项。
  • 如果预测结果与 Studio 不一致,请先对比同一个原始特征窗口,再调试实时采集。
  • 对于音频错误,请验证 ALSA、PulseAudio、PipeWire 的路由,以及采样格式、声道数、采样率和设备所有权。

Verify

验证

  1. Run a known Studio sample through the local pipeline.
  2. Compare the scores and preprocessing assumptions.
  3. Exercise continuous capture for several minutes while checking memory, latency, dropped frames, and clean shutdown.
  1. 通过本地流水线运行一个已知的 Studio 样本。
  2. 比较分数和预处理假设。
  3. 持续采集数分钟,同时检查内存、延迟、丢帧以及是否能够正常关闭。

Documentation

文档

Reference repositories

参考仓库