私的AI研究会 > Assistant SDK

Google Assistant Service

前準備

「Google Cloud Platform」のドキュメントがほとんど英語なので、Chromeをインストールして英語のページを自動的に日本語で見れるようにする。

  • Chromeインストールの際、規定のWEBブラウザにはせずに、いつものブラウザで英語のオリジナルページを参照しながら、Chromeブラウザで日本語訳を確認すると便利。
  • コマンドなどの文は一部日本語に変換されることがあるので、カット&ペーストで入力する場合は必ず英語のオリジナルページからすること。

参考 → Chrome の言語の変更とウェブページの翻訳

Googleアシスタントを埋め込む

オフィシャルドキュメント「Google Assistantサービスの紹介」ページより手順を確認しながら実行する。

1. ハードウェアとネットワークアクセスのセットアップ

2. オーディオの構成とテスト

  1. 録音および再生デバイスを見る。
    a. キャプチャハードウェアデバイスのリストでUSBマイクを見つけ、カード番号とデバイス番号を書き留める。
    $ arecord -l
    b. 再生ハードウェアデバイスのリストでスピーカーを見つけ、カード番号とデバイス番号を書き留める。
     3.5mmジャックには通常、Analogまたはbcm2835 ALSA(bcm2835 IEC958/HDMIではない)というラベルが付いていることに注意。
    $ aplay -l
  2. ホームディレクトリ(~/)に .asoundrc の名前で新しいファイルを作成する。<card number>と<device number>を前のステップで書き留めた番号に変更する。
    $ vi .asoundrc
    
    pcm.!default {
      type asym
      capture.pcm "mic"
      playback.pcm "speaker"
    }
    pcm.mic {
      type plug
      slave {
        pcm "hw:<card number>,<device number>"
      }
    }
    pcm.speaker {
      type plug
      slave {
        pcm "hw:<card number>,<device number>"
      }
    }
  3. 記録と再生が機能することを確認
    a. 再生音量を調整。
    $ alsamixer
    または
    $ amixer sset PCM 80% -c0
    b. テストサウンドを再生。確認できたら、Ctrl + Cを押す。これを実行しても何も聞こえない場合は、スピーカーの接続を確認。
    $ speaker-test -t wav
    speaker-test 1.1.8
    
    再生デバイス: default
    ストリームパラメータ: 48000Hz, S16_LE, 1 チャネル
    WAV ファイル
    レート 48000Hz (要求値 48000Hz)
    バッファサイズ範囲 512 ? 65536
    ピリオドサイズ範囲 512 ? 65536
    最大バッファサイズ 65536 を使用
    ピリオド数 = 4
    period_size = 16384 で設定
    buffer_size = 65536 で設定
     0 - Front Left
    ピリオド時間 = 0.358402
     0 - Front Left
    ピリオド時間 = 1.371160
     0 - Front Left
    ピリオド時間 = 1.360027
     0 - Front Left
    ^C
    c. 短いオーディオクリップを録音。
    $ arecord --format=S16_LE --duration=5 --rate=16000 --file-type=raw out.raw
    d. 再生して記録を確認。何も聞こえない場合は、で録音ボリュームを確認する。
    $ aplay --format=S16_LE --rate=16000 out.raw
    録音と再生が機能している場合、オーディオの設定は完了。そうでない場合は、マイクとスピーカーが正しく接続されていることを確認する。

3. 開発者プロジェクトとアカウント設定を構成する

4. デバイスモデルを登録する

5. SDKとサンプルコードをインストールする

  1. 環境を構成する (python3 の場合)
    $ sudo apt-get update
      : 省略
    $ sudo apt-get install python3-dev python3-venv
      : 省略
    $ python3 -m venv env
    $ env/bin/python -m pip install --upgrade pip setuptools wheel
    Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
    Collecting pip
      Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
        100% |████████████████████████████████| 1.4MB 267kB/s 
    Collecting setuptools
      Downloading https://files.pythonhosted.org/packages/a0/df/635cdb901ee4a8a42ec68e480c49f85f4c59e8816effbf57d9e6ee8b3588/setuptools-46.1.3-py3-none-any.whl (582kB)
        100% |████████████████████████████████| 583kB 745kB/s 
    Collecting wheel
      Downloading https://files.pythonhosted.org/packages/8c/23/848298cccf8e40f5bbb59009b32848a4c38f4e7f3364297ab3c3e2e2cd14/wheel-0.34.2-py2.py3-none-any.whl
    Installing collected packages: pip, setuptools, wheel
      Found existing installation: pip 18.1
        Uninstalling pip-18.1:
          Successfully uninstalled pip-18.1
      Found existing installation: setuptools 40.8.0
        Uninstalling setuptools-40.8.0:
          Successfully uninstalled setuptools-40.8.0
    Successfully installed pip-20.0.2 setuptools-46.1.3 wheel-0.34.2
    $ source env/bin/activate
    (env) $ 
  2. パッケージを入手する
    (env) $ sudo apt-get install portaudio19-dev libffi-dev libssl-dev
    パッケージリストを読み込んでいます... 完了
    依存関係ツリーを作成しています                
    状態情報を読み取っています... 完了
    libssl-dev はすでに最新バージョン (1.1.1d-0+deb10u2+rpt1) です。
    libssl-dev は手動でインストールしたと設定されました。
    以下の追加パッケージがインストールされます:
      libasound2-dev libjack-jackd2-dev libportaudiocpp0
    提案パッケージ:
      libasound2-doc portaudio19-doc
    以下のパッケージが新たにインストールされます:
      libasound2-dev libffi-dev libjack-jackd2-dev libportaudiocpp0 portaudio19-dev
    アップグレード: 0 個、新規インストール: 5 個、削除: 0 個、保留: 1 個。
    512 kB のアーカイブを取得する必要があります。
    この操作後に追加で 1,697 kB のディスク容量が消費されます。
    続行しますか? [Y/n] y
    取得:1 http://archive.raspberrypi.org/debian buster/main armhf libasound2-dev armhf 1.1.8-1+rpt1 [145 kB]
    取得:2 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libffi-dev armhf 3.2.1-9 [159 kB]
    取得:3 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libjack-jackd2-dev armhf 1.9.12~dfsg-2 [92.9 kB]     
    取得:4 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf libportaudiocpp0 armhf 19.6.0-1 [16.8 kB]
    取得:5 http://ftp.jaist.ac.jp/pub/Linux/raspbian-archive/raspbian buster/main armhf portaudio19-dev armhf 19.6.0-1 [98.6 kB]
    512 kB を 3秒 で取得しました (179 kB/s)
    以前に未選択のパッケージ libasound2-dev:armhf を選択しています。
    (データベースを読み込んでいます ... 現在 153471 個のファイルとディレクトリがインストールされています。)
    .../libasound2-dev_1.1.8-1+rpt1_armhf.deb を展開する準備をしています ...
    libasound2-dev:armhf (1.1.8-1+rpt1) を展開しています...
    以前に未選択のパッケージ libffi-dev:armhf を選択しています。
    .../libffi-dev_3.2.1-9_armhf.deb を展開する準備をしています ...
    libffi-dev:armhf (3.2.1-9) を展開しています...
    以前に未選択のパッケージ libjack-jackd2-dev:armhf を選択しています。
    .../libjack-jackd2-dev_1.9.12~dfsg-2_armhf.deb を展開する準備をしています ...
    libjack-jackd2-dev:armhf (1.9.12~dfsg-2) を展開しています...
    以前に未選択のパッケージ libportaudiocpp0:armhf を選択しています。
    .../libportaudiocpp0_19.6.0-1_armhf.deb を展開する準備をしています ...
    libportaudiocpp0:armhf (19.6.0-1) を展開しています...
    以前に未選択のパッケージ portaudio19-dev:armhf を選択しています。
    .../portaudio19-dev_19.6.0-1_armhf.deb を展開する準備をしています ...
    portaudio19-dev:armhf (19.6.0-1) を展開しています...
    libjack-jackd2-dev:armhf (1.9.12~dfsg-2) を設定しています ...
    libffi-dev:armhf (3.2.1-9) を設定しています ...
    libportaudiocpp0:armhf (19.6.0-1) を設定しています ...
    libasound2-dev:armhf (1.1.8-1+rpt1) を設定しています ...
    portaudio19-dev:armhf (19.6.0-1) を設定しています ...
    libc-bin (2.28-10+rpi1) のトリガを処理しています ...
    man-db (2.8.5-2) のトリガを処理しています ...
    install-info (6.5.0.dfsg.1-4+b1) のトリガを処理しています ...
    
    (env) $ python -m pip install --upgrade google-assistant-sdk[samples]
    Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
    Collecting google-assistant-sdk[samples]
      Downloading google_assistant_sdk-0.6.0-py2.py3-none-any.whl (31 kB)
    Collecting google-auth-oauthlib[tool]>=0.1.0
      Downloading google_auth_oauthlib-0.4.1-py2.py3-none-any.whl (18 kB)
    Collecting click<7,>=6.7; extra == "samples"
      Downloading click-6.7-py2.py3-none-any.whl (71 kB)
         |████████████████████████████████| 71 kB 1.1 MB/s 
    Collecting tenacity<5,>=4.1.0; extra == "samples"
      Downloading tenacity-4.12.0-py2.py3-none-any.whl (25 kB)
    Collecting sounddevice<0.4,>=0.3.7; extra == "samples"
      Downloading sounddevice-0.3.15-py2.py3-none-any.whl (30 kB)
    Collecting google-assistant-grpc==0.2.1; extra == "samples"
      Downloading google_assistant_grpc-0.2.1-py2.py3-none-any.whl (21 kB)
    Collecting urllib3[secure]<2,>=1.21; extra == "samples"
      Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
         |████████████████████████████████| 125 kB 1.9 MB/s 
    Collecting futures<4,>=3.1.1; extra == "samples"
      Downloading futures-3.1.1-py3-none-any.whl (2.8 kB)
    Collecting pathlib2<3,>=2.3.0; extra == "samples"
      Downloading pathlib2-2.3.5-py2.py3-none-any.whl (18 kB)
    Collecting requests-oauthlib>=0.7.0
      Downloading requests_oauthlib-1.3.0-py2.py3-none-any.whl (23 kB)
    Collecting google-auth
      Downloading google_auth-1.13.1-py2.py3-none-any.whl (87 kB)
         |████████████████████████████████| 87 kB 1.5 MB/s 
    Collecting six>=1.9.0
      Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
    Collecting CFFI>=1.0
      Downloading https://www.piwheels.org/simple/cffi/cffi-1.14.0-cp37-cp37m-linux_armv7l.whl (344 kB)
         |████████████████████████████████| 344 kB 137 kB/s 
    Collecting googleapis-common-protos>=1.5.2
      Downloading https://www.piwheels.org/simple/googleapis-common-protos/googleapis_common_protos-1.51.0-py3-none-any.whl (74 kB)
         |████████████████████████████████| 74 kB 1.5 MB/s 
    Collecting grpcio>=1.3.5
      Downloading grpcio-1.28.1.tar.gz (19.5 MB)
         |████████████████████████████████| 19.5 MB 3.0 MB/s 
    Collecting idna>=2.0.0; extra == "secure"
      Downloading idna-2.9-py2.py3-none-any.whl (58 kB)
         |████████████████████████████████| 58 kB 1.5 MB/s 
    Collecting pyOpenSSL>=0.14; extra == "secure"
      Downloading pyOpenSSL-19.1.0-py2.py3-none-any.whl (53 kB)
         |████████████████████████████████| 53 kB 618 kB/s 
    Collecting cryptography>=1.3.4; extra == "secure"
      Downloading cryptography-2.9.tar.gz (517 kB)
         |████████████████████████████████| 517 kB 3.4 MB/s 
      Installing build dependencies ... done
      Getting requirements to build wheel ... done
        Preparing wheel metadata ... done
    Collecting certifi; extra == "secure"
      Downloading certifi-2019.11.28-py2.py3-none-any.whl (156 kB)
         |████████████████████████████████| 156 kB 1.7 MB/s 
    Collecting requests>=2.0.0
      Downloading requests-2.23.0-py2.py3-none-any.whl (58 kB)
         |████████████████████████████████| 58 kB 2.2 MB/s 
    Collecting oauthlib>=3.0.0
      Downloading oauthlib-3.1.0-py2.py3-none-any.whl (147 kB)
         |████████████████████████████████| 147 kB 1.2 MB/s 
    Collecting cachetools<5.0,>=2.0.0
      Downloading cachetools-4.0.0-py3-none-any.whl (10 kB)
    Collecting pyasn1-modules>=0.2.1
      Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
         |████████████████████████████████| 155 kB 3.0 MB/s 
    Requirement already satisfied, skipping upgrade: setuptools>=40.3.0 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]>=0.1.0->google-assistant-sdk[samples]) (46.1.3)
    Collecting rsa<4.1,>=3.1.4
      Downloading rsa-4.0-py2.py3-none-any.whl (38 kB)
    Collecting pycparser
      Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
    Collecting protobuf>=3.6.0
      Downloading protobuf-3.11.3-py2.py3-none-any.whl (434 kB)
         |████████████████████████████████| 434 kB 2.0 MB/s 
    Collecting chardet<4,>=3.0.2
      Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
         |████████████████████████████████| 133 kB 3.3 MB/s 
    Collecting pyasn1<0.5.0,>=0.4.6
      Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
         |████████████████████████████████| 77 kB 1.3 MB/s 
    Building wheels for collected packages: grpcio, cryptography
      Building wheel for grpcio (setup.py) ... done
      Created wheel for grpcio: filename=grpcio-1.28.1-cp37-cp37m-linux_armv7l.whl size=23101245 sha256=680e20fbed176c24b18d3a607e0d846a38e2369488bb232c3b428ebe3ac8d55e
      Stored in directory: /home/pi/.cache/pip/wheels/1a/57/84/d91cbd4882f6ae0adc3acba56cbf0c2c75e0263118614d1fcf
      Building wheel for cryptography (PEP 517) ... done
      Created wheel for cryptography: filename=cryptography-2.9-cp37-cp37m-linux_armv7l.whl size=832556 sha256=20ebd74ff5e6b4632a09ca616bd50e3aadf058f6ab1c2317e126af6d6d258c97
      Stored in directory: /home/pi/.cache/pip/wheels/31/c6/89/e3fb1c0007eecd5b788d4a379c6a5602aec61dba2f10597c15
    Successfully built grpcio cryptography
    Installing collected packages: chardet, idna, six, pycparser, CFFI, cryptography, pyOpenSSL, certifi, urllib3, requests, oauthlib, requests-oauthlib, cachetools, pyasn1, pyasn1-modules, rsa, google-auth, click, google-auth-oauthlib, tenacity, sounddevice, protobuf, googleapis-common-protos, grpcio, google-assistant-grpc, futures, pathlib2, google-assistant-sdk
    Successfully installed CFFI-1.14.0 cachetools-4.0.0 certifi-2019.11.28 chardet-3.0.4 click-6.7 cryptography-2.9 futures-3.1.1 google-assistant-grpc-0.2.1 google-assistant-sdk-0.6.0 google-auth-1.13.1 google-auth-oauthlib-0.4.1 googleapis-common-protos-1.51.0 grpcio-1.28.1 idna-2.9 oauthlib-3.1.0 pathlib2-2.3.5 protobuf-3.11.3 pyOpenSSL-19.1.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 pycparser-2.20 requests-2.23.0 requests-oauthlib-1.3.0 rsa-4.0 six-1.14.0 sounddevice-0.3.15 tenacity-4.12.0 urllib3-1.25.8
  3. 資格情報を生成する
    (env) $ ls
    Desktop  Documents  Downloads  MagPi  Music  Pictures  Public  Templates  Videos  client_secret_1063710001909-4c3g9b9900le8dke9agqdq7i7p3c02vf.apps.googleusercontent.com.json  env
    
    (env) $ python -m pip install --upgrade google-auth-oauthlib[tool]
    Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
    Requirement already up-to-date: google-auth-oauthlib[tool] in ./env/lib/python3.7/site-packages (0.4.1)
    Requirement already satisfied, skipping upgrade: google-auth in ./env/lib/python3.7/site-packages (from google-auth-oauthlib[tool]) (1.13.1)
    Requirement already satisfied, skipping upgrade: requests-oauthlib>=0.7.0 in ./env/lib/python3.7/site-packages (from google-auth-oauthlib[tool]) (1.3.0)
    Requirement already satisfied, skipping upgrade: click; extra == "tool" in ./env/lib/python3.7/site-packages (from google-auth-oauthlib[tool]) (6.7)
    Requirement already satisfied, skipping upgrade: pyasn1-modules>=0.2.1 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]) (0.2.8)
    Requirement already satisfied, skipping upgrade: rsa<4.1,>=3.1.4 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]) (4.0)
    Requirement already satisfied, skipping upgrade: six>=1.9.0 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]) (1.14.0)
    Requirement already satisfied, skipping upgrade: cachetools<5.0,>=2.0.0 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]) (4.0.0)
    Requirement already satisfied, skipping upgrade: setuptools>=40.3.0 in ./env/lib/python3.7/site-packages (from google-auth->google-auth-oauthlib[tool]) (46.1.3)
    Requirement already satisfied, skipping upgrade: oauthlib>=3.0.0 in ./env/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (3.1.0)
    Requirement already satisfied, skipping upgrade: requests>=2.0.0 in ./env/lib/python3.7/site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (2.23.0)
    Requirement already satisfied, skipping upgrade: pyasn1<0.5.0,>=0.4.6 in ./env/lib/python3.7/site-packages (from pyasn1-modules>=0.2.1->google-auth->google-auth-oauthlib[tool]) (0.4.8)
    Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in ./env/lib/python3.7/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (3.0.4)
    Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./env/lib/python3.7/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (1.25.8)
    Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in ./env/lib/python3.7/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (2.9)
    Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in ./env/lib/python3.7/site-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib[tool]) (2019.11.28)
    
    (env) $ google-oauthlib-tool --scope https://www.googleapis.com/auth/assistant-sdk-prototype --save --headless --client-secrets client_secret_1063710001909-4c3g9b9900le8dke9agqdq7i7p3c02vf.apps.googleusercontent.com.json
    Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=1063710001909-4c3g9b9900le8dke9agqdq7i7p3c02vf.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fassistant-sdk-prototype&state=a7Kdg4uOkW1M2IePjORa1PBEfdvhiV&prompt=consent&access_type=offline
    Enter the authorization code: 
    ブラウザから URLをアクセスして得られた認証コードを入力する。
    Enter the authorization code: 4/yQGlAv-qqDU08U5xA8OZXsT8-PdcK9G--Wq5a-Mg-SjL0k63-gQB7qI
    credentials saved: /home/pi/.config/google-oauthlib-tool/credentials.json

6. サンプルコードを実行する

冒頭に出る WARNING: については、ドキュメント「初回にデバイスインスタンスIDを見つける」の項目によれば

6-1. Google Assistant を日本語化する

スマートスピーカーのカスタマイズ

 ハードウェアボタン(タクトSW)とLEDを紐づけて、ボタンを押して「Push to Talk」を起動させるようにする。
LEDタクトSWの接続回路は右記のとおり。

7. Button To Talk プログラムの作成

7-1. 独自コマンド Traits 追加

Python 実行時のエラー

参考 → Pythonエラー一覧(日本語)

WARNING:root:SoundDeviceStream write underflow について

7-1. 項の問題とは無関係と結論。--audio-block-size はデフォールトのままとする。

参考サイト:Python SoundDeviceStream write/read underflow/overflow errors

 

Last-modified: 2021-06-06 (日) 16:24:12