nyabot’s diary

電気猫の夢を見るお話

任意のホットワードでGoogleAssistantを起動させる

前回、前々回でRaspberry PiGoogle AssistantとSnowboyを入れた。

nyabot.hatenablog.com

nyabot.hatenablog.com

今回は、Snowboyを用いて自分の作成したホットワードでGoogleAssistantを起動させる。
方法については、以下の記事を参考にさせていただきました。めっちゃ感謝。

https://blog.mi.hdm-stuttgart.de/index.php/2017/08/18/2576/

SnowboyからGoogleAssistantのpushtotalkを呼び出す

/snowboy/examples/Python3/ ディレクトリにあるdemo.pyを複製して、ohayo.pyを作成。
記述を以下のように一部変更。

import snowboydecoder
import sys
import signal
from googlesamples.assistant.grpc.pushtotalk import main
interrupted = False

def signal_handler(signal, frame):
    global interrupted
    interrupted = True

def interrupt_callback():
    global interrupted
    return interrupted

model = "resources/models/ohayo.pmdl"

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
print('Listening... Press Ctrl+C to exit')

# main loop
detector.start(detected_callback=main,
               interrupt_check=interrupt_callback,
               sleep_time=0.03)

detector.terminate()

model = "resources/models/ohayo.pmdl"の部分は自分の作成したpmdlファイルに合わせて適宜書き換えてください。
僕の場合は「おはよう」を登録した「ohayo.pmdl」を作成しました(過去記事参照)。

GoogleAssistantのpushtotalk.pyを書き換える

次に、以下のディレクトリにあるpushtotalk.pyを書き換える。
/home/pi/env/nyabot/lib/python3.5/site-packages/googlesamples/assistant/grpc
ディレクトリ名「nyabot」は仮想環境名によって変わるので注意。適宜変更されたし。)

  • リピートしないようにする
  • キー入力を待たず、すぐに待受状態にする

書き換え箇所は、ファイルの最後の方にある以下の部分。

wait_for_user_trigger = not once
while True:
    if wait_for_user_trigger:
        click.pause(info='Press Enter to send a new request...')
    continue_conversation = assistant.converse()

上記の部分を以下の内容に書き換え。

wait_for_user_trigger = once
while True:
    if wait_for_user_trigger:
        return
    continue_conversation = assistant.converse()

pulseaudioをインストールする

これだけだと、環境によってはエラーが発生する模様。した。
pulseaudioをインストールすると解決するらしい。

sounddevice.PortAudioError: Error opening RawStream: Device unavailable [PaErrorCode -9985] in the grpc sample · Issue #219 · googlesamples/assistant-sdk-python · GitHub

pulseaudioは古いラズパイにはデフォルトで入っていたみたい?だけど、僕のにはなかったのでインストール。

$ sudo apt update
$ sudo apt install pulseaudio

任意のホットワードでGoogleAssistantが起動する!

これで、自分の作成したホットワードでGoogleAssistantが起動するようになった。

$ source env/nyabot/bin/activate
(nyabot)$ python snowboy/examples/Python3/ohayo.py

「おはよう(←ホットワード)、明日の東京の天気は?」
「明日の東京の天気は晴れです。」

あとはホットワードを変えれば、彼または彼女は、HAL9000にもドラえもんにもなってくれる。
きっとこんな感じ。

「HAL(←ホットワード)、君の名前は?」
「私はGoogleアシスタントです。」

なるほど。課題は多い。今回はここまで。
つづく。

追記

君の本当の名前を知りたかったよ……。

nyabot.hatenablog.com