DEV Community

Masatoshi Nishiguchi
Masatoshi Nishiguchi

Posted on

Raspberry Pi TensorFlow Liteで物体検出を楽しむ

この記事について

  • Raspberry PiTensorFlowPythonのいずれにも詳しくない筆者が、物体検出をやって楽しんだ成果の記録です。
  • TensorFlow公式の物体検出のサンプルプログラムを実行します。

動作環境

  • ボード
    • Raspberry Pi 4 Model B
  • OS
    • Raspberry Pi OS (32-bit または 64-bit)
    • デスクトップ環境
  • カメラ
  • Python
    • Python 3.9
    • 執筆時点で Raspberry Pi OS に入ってたやつそのまま

CleanShot_2023-04-22_at_17.57.27.png

Raspberry Pi ボードは以下のモデルでTensorFlow Lite がサポートされているようです。

  • Raspberry Pi 2
  • Raspberry Pi 3
  • Raspberry Pi 4
  • Raspberry Pi Zero 2

準備するもの

サンプルプログラムについて

  • TensorFlow公式の tensorflow/examples を使います。
  • その中の lite/examples/object_detection/raspberry_pi ディレクトリに物体検出のサンプルプログラムが入っています。
  • Raspberry Pi 上の Python でTensorFlow Liteを使用して、Pi カメラからストリーミングされた画像を使用してリアルタイムの物体検出を実行します。カメラプレビューで検出された各物体の周囲に境界ボックスを描画します。

https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/raspberry_pi

雰囲気を感じる

https://youtu.be/Lyh84KMqUPI

https://youtu.be/aimSGOAUI8Y

https://youtu.be/qJMwNHQNOVU

ハードウェアのセットアップ

まずは、 Raspberry Pi OS を使用してRaspberry Pi をセットアップする必要があります。

Raspberry Pi Foundation 公式の資料がありますし、「Raspberry Pi 初期セットアップ」でネット検索してもいろいろ情報が見つかるはずです。

https://projects.raspberrypi.org/ja-JP/projects/raspberry-pi-setting-up

Raspberry Pi のアップデート

sudo apt update && sudo apt full-upgrade

# のちにGitを使うのでインストールしておく
sudo apt install git

# OpenCVを使うために必要らしい
sudo apt install libatlas-base-dev
Enter fullscreen mode Exit fullscreen mode

カメラのセットアップ

サンプルプログラムはRaspberry Pi カメラモジュールUSB カメラのどちらでも機能するようです。

Raspberry Pi カメラモジュールを使用する場合は、カメラモジュールを有効化する必要があります。Raspberry Pi のターミナルからraspi-configコマンドを打ち設定画面を開きます。

https://zenn.dev/technicarium/articles/449294af295d5c

https://www.raspberrypi.com/documentation/computers/configuration.html

Raspberry Pi カメラモジュールには向きがありますので、正しく接続するように注意してください。

https://youtu.be/VzYGDq0D1mw

サンプルプログラムをダウンロードする

Raspberry Pi のターミナルから以下のようにサンプルプログラムGitリポジトリをクローンします。

# コードを置く場所をつくり、その中に入る
mkdir -p ~/src && cd ~/src

# サンプルプログラムを全部ダウンロード
# 「--depth 1」オプションをつけて不要なGit関連データを取り込まないようにする
git clone https://github.com/tensorflow/examples --depth 1 tensorflow_examples

# 物体検出のディレクトリが深い階層にあるのでアクセスしやすいところにコピーする
cp -r ~/src/tensorflow_examples/lite/examples/object_detection/raspberry_pi \
  ~/src/tflite_object_detection

# 他のコードは不要なので消してもよい
rm -rf ~/src/tensorflow_examples
Enter fullscreen mode Exit fullscreen mode

サンプルプログラムに付属のスクリプトを使用して必要な Python パッケージをインストールし、EfficientDet-Lite モデルをダウンロードします。

# 物体検出のディレクトリへ入る
cd ~/src/tflite_object_detection

# 必要な Python パッケージをインストールし、EfficientDet-Lite モデルをダウンロード
./setup.sh
Enter fullscreen mode Exit fullscreen mode

サンプルプログラムを実行する

モニターにデスクトップが表示されている状態でターミナルからサンプルプログラムを実行します。実行すると、物体検知用のウインドウが開きます。

よくわかりませんが、環境変数DISPLAY=:0がセットされていないとエラーになりました。

DISPLAY=:0 python3 detect.py
Enter fullscreen mode Exit fullscreen mode

raspi-object-detection-20230422_131913.jpg

さいごに

一見簡単そうでしたが、やってみると最初はなかなかうまくいきませんでした。

CleanShot 2023-04-22 at 19.17.44@2x.png

元氣があればなんでもできる!

ダメになりそうな時それが一番大事!

https://youtu.be/PypF2XiQdss

本記事は以下のモクモク會での成果です。みなさんから刺激と元氣をいただき、ありがとうございました。

https://youtu.be/c0LP23SM7BU

https://okazakirin-beam.connpass.com/

https://autoracex.connpass.com

もしご興味のある方はお氣輕にご參加ください。

20230216_133626.jpg

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more