0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Raspberry Pi 5】AI Camera(IMX500) × YOLOv8n でリアルタイム物体検出(Picamera2デモ&.rpk生成まで)

Last updated at Posted at 2025-08-12

デモ動画


今回のゴール

  • ライブ映像に バウンディングボックス+ラベル をリアルタイム表示
  • オンセンサ推論(IMX500) でPi側の負荷を抑えて軽快に動かす
  • 生成物(packerOut.zip.rpk)を Picamera2のIMX500デモ で読み込んで検出
  • プレビュー中に Enter/s キーでPNG保存(後述のスニペットを追加)

検出ターゲットの例:person / keyboard / cup / mouse / cell phone など。


動作環境(ハードウェア)

  • Raspberry Pi 5(8GB)
  • Raspberry Pi AI Camera(Sony IMX500)
  • (おまけ)Raspberry Pi 公式ACアダプター 27W USB-PD
  • (おまけ)Raspberry Pi 公式キーボード&マウス

接続

AI Camera同梱のFPCケーブルを CAM1(または CAM0)へ接続するだけ。
CAM0を使う場合はスクリプト側のカメラ番号を合わせます。

raspi_cam_connector.png
図: Raspberry Pi 5のCAMコネクタにAI Cameraを接続


Raspberry Pi AI Camera(IMX500)とは

imx500_module.png
図: Raspberry Pi AI Camera(IMX500)

Sony IMX500(Intelligent Vision Sensor) を搭載したカメラモジュールで、
センサ内に推論エンジンを内蔵しています。
そのため、これまでの「カメラ→Pi側で推論」と違い、推論をセンサ内で完結できます。

  • PiのCPU/GPU負荷を抑えやすい
  • 遅延が少ない
  • 必要最小限のメタデータ運用も可能(プライバシー面の選択肢が広がる)

imx500_pipeline.png
図: IMX500のオンセンサ推論パイプライン(概念図)

参考ドキュメント:
https://www.raspberrypi.com/documentation/accessories/ai-camera.html

Camera Module 3(IMX708)とのざっくり比較

  • AI Camera(IMX500):センサ内で推論(.rpk 実行)。
  • Pi側の負荷は低め。メタデータ中心の運用も可能。
  • Camera Module 3:推論はPi側(CPU/GPU/NPU)。
  • 高画質の静止画・動画が得意。

imx500_vs_cm3_table.png
表: IMX500 と Camera Module 3 の比較(ざっくり表)


YOLOの超要約(仕組み)

YOLO(You Only Look Once)は、
入力画像をグリッドに分割し、BBox+クラスを一度に推定 → NMSで重複を間引く流れで、高速な物体検出が得意です。

yolo_explainer.png
図: YOLOの流れ(グリッド/BBox/クラス確定)
You Only Look Once: Unified, Real-Time Object Detectionより引用

IMX500はYOLO系のネットを.rpkとして実行できます。今回は 軽量なYOLOv8n を使用(次回はv11系も試す予定)。


実装の全体像

  1. OSパッケージの導入(Picamera2 / rpicam-apps / IMX500ツール)
  2. YOLOv8n を IMX500形式へエクスポート(Ultralytics)
  3. packerOut.zip.rpk にパッケージング
  4. Picamera2 の IMX500デモ.rpk を読み込んで実行

1) OSパッケージのインストール

sudo apt update
# IMX500 ファーム&ツール
sudo apt install -y imx500-all imx500-tools
# カメラ & Python 周り
sudo apt install -y python3-picamera2 rpicam-apps git python3-venv python3-pip

2) YOLOv8n を IMX500 向けにエクスポート

# 作業用ディレクトリ
mkdir -p ~/person_detection && cd ~/person_detection

# venv
python3 -m venv .
source ~/person_detection/bin/activate

# SDK
pip install --upgrade pip
pip install ultralytics

# エクスポート(YOLOv8n → IMX500形式)
python - <<'PY'
from ultralytics import YOLO
m = YOLO("yolov8n.pt")
m.export(format="imx")
print("DONE")
PY

出力例(環境により異なります):

yolov8n_imx_model/
 ├─ packerOut.zip   # ← これを .rpk にします
 ├─ labels.txt
 ├─ dnnParams.xml
 ├─ yolov8n_imx.onnx
 └─ yolov8n_imx.pbtxt

3) packerOut.zip.rpk にパッケージング

cd ~/person_detection/yolov8n_imx_model
imx500-package -i packerOut.zip -o out
# 生成物(通常 out/network.rpk)
ls -l out

.rpk(Runtime Package)はIMX500で動かすための署名済みパッケージ。


4) Picamera2 の IMX500 デモで実行

cd ~
[ -d picamera2 ] || git clone https://github.com/raspberrypi/picamera2.git
cd ~/picamera2/examples/imx500

# AI Camera を CAM1 で使う場合はスクリプトを複製し、Picamera2(1) に変更
cp imx500_object_detection_demo.py imx500_object_detection_demo_cam1.py
sed -i 's/Picamera2()/Picamera2(1)/' imx500_object_detection_demo_cam1.py

# 実行(しきい値やBBox設定は適宜調整)
python3 imx500_object_detection_demo_cam1.py   --model  ~/person_detection/yolov8n_imx_model/out/network.rpk   --labels ~/person_detection/yolov8n_imx_model/labels.txt   --threshold 0.15   --bbox-normalization --bbox-order xy

よく使う補助コマンド

  • カメラ番号確認:rpicam-hello --list-cameras
  • 別モデルの動作確認(Model Zoo導入済み環境):
    sudo apt install -y imx500-models
    python3 imx500_object_detection_demo_cam1.py     --model /usr/share/imx500-models/imx500_network_yolo11n_pp.rpk     --bbox-normalization --bbox-order xy --threshold 0.15
    

検出結果の例(キャプチャ)

result_mouse.png
検出例:mouse(YOLOv8n × IMX500)

result_plant.png
検出例:potted plant / vase

result_cup.png
検出例:cup


画像保存の追加(Qiita向け追記)

プレビューに出ている枠・ラベル込みの画像を保存できるように、Picamera2デモへ数行追加。
s で保存、q で終了。

# --- Add near the top of the file ---
import os
from pathlib import Path
from datetime import datetime

# Output folder (e.g., ~/captures)
OUT_DIR = Path.home() / "captures"
OUT_DIR.mkdir(parents=True, exist_ok=True)
# --- In your main preview loop, after drawing overlays on a copy ---
# Ensure drawing on a copy:
# display = frame.copy(); draw boxes/labels on "display"
cv2.imshow("preview", display)
key = cv2.waitKey(1) & 0xFF
if key == ord('s'):
    ts = datetime.now().strftime("%Y%m%d-%H%M%S")
    out_path = OUT_DIR / f"imx500_{ts}.png"
    cv2.imwrite(str(out_path), display)
    print(f"[Saved] {out_path}")
elif key == ord('q'):
    break

迷ったら:最短ルート(全部コピペ)

# 0) 必要パッケージ
sudo apt update
sudo apt install -y imx500-all imx500-tools python3-picamera2 rpicam-apps git python3-venv python3-pip

# 1) venv + Ultralytics
mkdir -p ~/person_detection && cd ~/person_detection
python3 -m venv .
source ~/person_detection/bin/activate
pip install --upgrade pip
pip install ultralytics

# 2) export(YOLOv8n → IMX)
python - <<'PY'
from ultralytics import YOLO
m = YOLO("yolov8n.pt")
m.export(format="imx")
print("DONE")
PY

# 3) .rpk にパッケージ
cd ~/person_detection/yolov8n_imx_model
imx500-package -i packerOut.zip -o out
cp out/network.rpk yolov8n.rpk

# 4) デモ実行(CAM1想定)
cd ~
[ -d picamera2 ] || git clone https://github.com/raspberrypi/picamera2.git
cd ~/picamera2/examples/imx500
cp imx500_object_detection_demo.py imx500_object_detection_demo_cam1.py
sed -i 's/Picamera2()/Picamera2(1)/' imx500_object_detection_demo_cam1.py
python3 imx500_object_detection_demo_cam1.py   --model  ~/person_detection/yolov8n_imx_model/yolov8n.rpk   --labels ~/person_detection/yolov8n_imx_model/labels.txt   --threshold 0.15 --bbox-normalization --bbox-order xy

よくあるハマりどころ

パッケージが見つからない / コマンドがない
  • imx500-package: command not foundsudo apt install -y imx500-tools
  • ModuleNotFoundError: ultralytics → venvを有効化して pip install ultralytics
  • Ubuntu系で imx500-all が見つからない → Raspberry Pi OS を推奨
カメラが認識しない / 番号が違う
  • rpicam-hello --list-cameras で番号確認(0/1)
  • スクリプトの Picamera2(0/1) を合わせる
  • FPCの挿し直し、OS側カメラ有効化の確認
検出が弱い / ノイズが多い
  • --threshold を 0.10〜0.30 で調整
  • --max-detections の上限調整
  • ライティングやカメラ向きの見直し

まとめ

  • IMX500のオンセンサ推論で、Raspberry Pi 5でも軽快にリアルタイム検出
  • YOLOv8nを format="imx" でエクスポート → .rpk へパッケージ → Picamera2デモで動作確認
  • 必要に応じて --threshold 等を調整、保存スニペットで記録も簡単

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?