Android Things事始め
先日、DroidKaigi Codelabs Tourにお邪魔したところ、Android ThingsのStarter Kitをお借りして試すことができました。
やってみたのはこれです。
Android Things Image Classifier
TensorFlowLite使った画像認識!……ですが、既に学習済みのモデルを用意してくれていました。今回は、学習させる部分ではなく、認識させる部分のみを実装します。
ちなみに、全体で75分かかることになっていますが、ハードウェアの実装で40分ほど確保されています。そんなに時間をガッツリ確保できなくても進められます。
まずはサンプルの犬の画像を認識してもらいます。Portuguese Water Dogという犬がいるんですね。ただのビショビショの犬じゃなかった。
光って読めませんが、curly-coated retriever, Border collie or English spinner
と認識しています。画像とはちょっと違いますが、学習済みデータの都合とのこと。大体犬です。
次に、カメラに接続して実際に撮影した画像を認識していきます。
Import文にhardware関連が並んでいるのはちょっと新鮮です。
import android.hardware.camera2.CameraAccessException; import android.hardware.camera2.CameraCaptureSession; import android.hardware.camera2.CameraCharacteristics; import android.hardware.camera2.CameraDevice; import android.hardware.camera2.CameraManager; import android.hardware.camera2.CaptureRequest; import android.hardware.camera2.CaptureResult; import android.hardware.camera2.TotalCaptureResult; import android.hardware.camera2.params.StreamConfigurationMap;
iPod……!惜しい!
Codelabは以上です。お試し用なので認識精度はそんなに高くはありませんが、いつものAndroidStudioを使ってIoT向けに遊べるというのは最高ですね。
……と思いながらブラウザで無心にボタンをクリックしていたところ、不思議なメールが来ました。
そして本日、不思議な箱が到着しました。
台湾からの配送で、税関で別途2080円かかりました。
開けてみた様子です。
中にはこんなものが。
箱の裏側に、中に入っているコンポーネントの説明が書いてあります。さすがgoogle……!
内容物は以下の通りです。
- メインのボード(NXP i.MX7D)
- ディスプレイ
- WiFi用アンテナ
- カメラ
- USBケーブル
- ドライバー
- Rainbow HAT
Android Things向けに開発された、Raspberry Piに接続して利用する多彩な拡張基板です。温度・タッチセンサ、ブザー、LEDディスプレイなど様々な入出力を試すことができます。
サイトの案内に沿って組み立てていきます。組み立て後のセットアップのために、Androidの端末が1台あると便利です。
完成品がこちら。
ここで電源を入れるとAndroidが起動する……はずだったのですが、この段階ではまだイメージが焼かれていなかったので作業を続けます。
NXP i.MX7D | Android Things | Android Developers
まずはここからセットアップツールを落とします。Googleアカウントが必須です。
落としてきたzipファイルを解凍し、中に入ります。
$ ls README.md android-things-setup-utility-macos quickstart-temp android-things-setup-utility-linux android-things-setup-utility-windows.exe
各OS用にツールが用意されています。今回はmacでやっていきます。
$ ./android-things-setup-utility-macos ... What do you want to do? 1 - Install Android Things and optionally set up Wi-Fi 2 - Set up Wi-Fi on an existing Android Things device
インストールするので1
を選択。
What hardware are you using? 1 - Raspberry Pi 3 2 - NXP Pico i.MX7D
購入したモデルを選びます。今回は2
を選択。
Setting up required tools… Fetching additional configuration… Downloading platform tools… Unzipping platform tools… Finished setting up required tools.
ツール群のダウンロードが始まります。
Do you want to use the default image or a custom image? 1 - Default image: Used for development purposes and supported by Android Things Toolkit, a mobile companion app that can be downloaded at https://goo.gl/rPWhmv. No access to the Android Things Console features such as metrics, crash reports, and OTA updates. 2 - Custom image: Upload your custom image for full device development and management with all Android Things Console features.
インストールするイメージを選択します。クラッシュレポートやOverTheAirでのアップデート配信など、製品化を向けたイメージはカスタムのみの機能です。今回はとりあえず動かすのが目的なのでデフォルトイメージを選びます。
Connect your device to this computer: The USB cable should plug into your board’s USB-C port. If your computer also has USB-C ports like the more recent MacBooks, you will need to use a USB hub. Otherwise the board won’t power on correctly. Once connected, press [Enter] to install Android Things on the device...
KitをPCに繋ぎます。type-cにはハブが必要、とまで丁寧にメッセージが出てきます。接続したらEnter。
Looking for devices… This can take up to 3 minutes. found device Unzipping image… Flashing Android Things. This will take a few minutes… *Do not disconnect or interrupt!*
デバイスが見つかると実際に焼き始めます。いじらずに待ちます。 この辺からLEDが光り始めます。結構眩しいので注意してください。
finished. total time: 124.358s Successfully flashed your imx7d. Successfully flashed Android Things… Would you like to set up Wi-Fi on this device? (y/n)
いずれ終わります。その後Wi-Fiの設定に入りますが、今回はAndroidのアプリを使って行うのでn
を選択します。
そして、AndroidのスマホでAndroid Things Toolkit
を起動します。
Kit側に4文字のコードが表示されるので、画面の指示に従って入力します。
その後、WiFiの設定も行います。
ここでKitの動作確認ができます。僕の場合、カメラモジュールが動きませんでした。 つらいので秋葉原で探してきます。
テストの結果にかかわらず、一通り設定が完了です。
Android Thingsのバージョンが、1.0になっていることを確認しましょう(2018/05/27現在)。このバージョンが低いと、codelabのアプリが動きません。 具体的には、rainbow HATのボタンをinitializeするところでコケるみたいです。
あとは自由にアプリを開発するだけです。最後に、参考になりそうなリンクをいくつか貼っておきます。