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

## この記事について

* [Raspberry Pi](https://ja.wikipedia.org/wiki/Raspberry_Pi)、[TensorFlow](https://ja.wikipedia.org/wiki/TensorFlow)、[Python](https://ja.wikipedia.org/wiki/Python)のいずれにも詳しくない筆者が、[物体検出](https://ja.wikipedia.org/wiki/%E7%89%A9%E4%BD%93%E6%A4%9C%E5%87%BA)をやって楽しんだ成果の記録です。
    
* TensorFlow公式の物体検出のサンプルプログラムを実行します。
    

## 動作環境

* ボード
    
    * Raspberry Pi 4 Model B
        
* OS
    
    * Raspberry Pi OS (32-bit または 64-bit)
        
    * デスクトップ環境
        
* カメラ
    
    * [Raspberry Pi カメラモジュール v2](https://www.raspberrypi.com/documentation/accessories/camera.html)
        
* Python
    
    * Python 3.9
        
    * 執筆時点で Raspberry Pi OS に入ってたやつそのまま
        

![CleanShot_2023-04-22_at_17.57.27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/ab79ef3a-44f8-0641-924c-4113a240e611.png align="left")

## 準備するもの

* [Raspberry Pi カメラモジュール](https://www.raspberrypi.com/documentation/accessories/camera.html) もしくは [USB カメラ](https://www.google.com/search?q=USB%E3%82%AB%E3%83%A1%E3%83%A9&tbs=qdr%3Ay)
    
* [HDMI ケーブル](https://www.google.com/search?q=HDMI%E3%82%B1%E3%83%BC%E3%83%96%E3%83%AB&tbs=qdr%3Ay)（Raspberry Pi をモニターに接続）
    
* モニター
    
* キーボード
    
* マウス
    

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

* TensorFlow公式の [tensorflow/examples](https://github.com/tensorflow/examples) を使います。
    
* その中の [lite/examples/object\_detection/raspberry\_pi](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/raspberry_pi) ディレクトリに物体検出のサンプルプログラムが入っています。
    
* Raspberry Pi 上の Python で[TensorFlow Lite](https://translate.google.com/website?sl=en&tl=ja&hl=en&client=webapp&u=https://tensorflow.org/lite)を使用して、Pi カメラからストリーミングされた画像を使用してリアルタイムの物体検出を実行します。カメラプレビューで検出された各物体の周囲に境界ボックスを描画します。
    

[https://github.com/tensorflow/examples/tree/master/lite/examples/object\_detection/raspberry\_pi](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/raspberry_pi)

## 雰囲気を感じる

[https://youtu.be/Lyh84KMqUPI](https://youtu.be/Lyh84KMqUPI)

[https://youtu.be/aimSGOAUI8Y](https://youtu.be/aimSGOAUI8Y)

[https://youtu.be/qJMwNHQNOVU](https://youtu.be/qJMwNHQNOVU)

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

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

[Raspberry Pi Foundation 公式の資料](https://projects.raspberrypi.org/ja-JP/projects/raspberry-pi-setting-up)がありますし、[「Raspberry Pi 初期セットアップ」でネット検索](https://www.google.com/search?q=Raspberry+Pi+%E5%88%9D%E6%9C%9F%E3%82%BB%E3%83%83%E3%83%88%E3%82%A2%E3%83%83%E3%83%97&tbs=qdr%3Ay)してもいろいろ情報が見つかるはずです。

[https://projects.raspberrypi.org/ja-JP/projects/raspberry-pi-setting-up](https://projects.raspberrypi.org/ja-JP/projects/raspberry-pi-setting-up)

## カメラのセットアップ

サンプルプログラムは[Raspberry Pi カメラモジュール](https://www.raspberrypi.com/documentation/accessories/camera.html) 、[USB カメラ](https://www.google.com/search?q=USB%E3%82%AB%E3%83%A1%E3%83%A9&tbs=qdr%3Ay)のどちらでも機能するようです。

[Raspberry Pi カメラモジュール](https://www.raspberrypi.com/documentation/accessories/camera.html)を使用する場合は、カメラモジュールを有効化する必要があります。Raspberry Pi のターミナルから`raspi-config`コマンドを打ち設定画面を開きます。

[https://zenn.dev/technicarium/articles/449294af295d5c](https://zenn.dev/technicarium/articles/449294af295d5c)

[https://www.raspberrypi.com/documentation/computers/configuration.html](https://www.raspberrypi.com/documentation/computers/configuration.html)

[Raspberry Pi カメラモジュール](https://www.raspberrypi.com/documentation/accessories/camera.html)には向きがありますので、正しく接続するように注意してください。

[https://youtu.be/VzYGDq0D1mw](https://youtu.be/VzYGDq0D1mw)

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

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

```bash
# コードを置く場所をつくり、その中に入る
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
```

[サンプルプログラムに付属のスクリプト](https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/raspberry_pi/setup.sh)を使用して[必要な Python パッケージ](https://github.com/tensorflow/examples/blob/master/lite/examples/object_detection/raspberry_pi/requirements.txt)をインストールし、[EfficientDet-Lite モデル](https://tfhub.dev/tensorflow/lite-model/efficientdet/lite0/detection/metadata/1)をダウンロードします。

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

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

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

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

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

```bash
DISPLAY=:0 python3 detect.py
```

![raspi-object-detection-20230422_131913.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/c9a9e8ca-3f83-e2fb-b5a2-73aedbc402e2.jpeg align="left")

## さいごに

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

![CleanShot 2023-04-22 at 19.17.44@2x.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/9004a9ed-78a2-94d7-3bf9-6cf15262e6aa.png align="left")

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

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

[https://youtu.be/PypF2XiQdss](https://youtu.be/PypF2XiQdss)

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

[https://youtu.be/c0LP23SM7BU](https://youtu.be/c0LP23SM7BU)

[https://okazakirin-beam.connpass.com/](https://okazakirin-beam.connpass.com/)

[https://autoracex.connpass.com](https://autoracex.connpass.com)

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

![20230216_133626.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/82804/0479f61f-8f87-11ad-41d0-88ef0bea8303.jpeg align="left")
