LoginSignup
4
5

More than 5 years have passed since last update.

googleのAIY Project VoiceKitを音声起動する

Posted at

概要

年末帰省するとgoogleのAIYProject VoiceKitが導入されていた。
https://aiyprojects.withgoogle.com/voice#list-of-materials

自分で購入したわけではないので詳細は不明だが、
最近流行りのスマートスピーカーの派生製品で、
出来合いの製品とは異なりsdkによる実装で任意の動作をプログラムできるらしい。

家を出る直前だったが電気とかつけさせてほしいとのことだったので簡易なスクリプトを書いてみた。
ほぼデモアプリのまま、任意の日本語音声をトリガーに処理を実行できるところまでを実装した。

実装

基本的にはcloud_speech_demo.pyのチュートリアルどおりに起動できるまでをやる。

日本語対応も下記サイトを参考に実装
https://kureuetan.com/web/iot/4638/

詰まったのがスクリプト起動時に ImportError: No module named 'google' でエラーになる。
voicekitにssh経由でログインしていたためvirtualenvに入れてないのが原因らしいので下記で解消。
https://azriton.github.io/2017/12/13/ラズパイの簡易スマート・スピーカーGoogle-AIYでVoice-Recognizerを作る/

日本語に反応するまではここまでで良いが、
デモで用意されているcloud_speech_demo.pyではvoicekitのボタンを押している間音声認識を働かせる実装になっている。
「OKgoogle」という音声をキックに指示を聞いてくれるようにしてほしいとのこと。

googleのapiとしてgoogleアシスタンスの機能とcloudspeechの機能は別物として用意されているらしく、(アシスタンスはOKgoogleに反応して天気とか聞くと答えてくれる、cloudspeehは単なる音声認識機能のみ)googleアシスタンスに任意の命令を紐付ける方法がよくわからず時間もなかったので、簡易にスクリプトを修正して実現してみた。

cloudspeechのapiに「OKgoogle」を聞かせるとlisten状態を制御しただけ。

#!/usr/bin/env python3
import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
aiy.i18n.set_language_code('ja-JP')


def main():
    is_able_to_listening = False
    recognizer = aiy.cloudspeech.get_recognizer()
    recognizer.expect_phrase('電気をつけて')
    recognizer.expect_phrase('電気を消して')
    recognizer.expect_phrase('チカチカ')

    status_ui = aiy.voicehat.get_status_ui()
    status_ui.status('ready')

    led = aiy.voicehat.get_led()
    aiy.audio.get_recorder().start()

    while True:
        print('Press the button and speak')
        print('Listening...')
        text = recognizer.recognize()
        if text is None:
            print('Sorry, I did not hear you.')
        else:
            print('You said "', text, '"')
            if not is_able_to_listening and 'Ok Google' in text:
                print('change status to listening')
                status_ui.status('listening')
                is_able_to_listening = True
            elif is_able_to_listening:
                status_ui.status('ready')
                is_able_to_listening = False
                if '電気をつけて' in text:
                    led.set_state(aiy.voicehat.LED.ON)
                elif '電気を消して' in text:
                    led.set_state(aiy.voicehat.LED.OFF)
                elif 'チカチカ' in text:
                    led.set_state(aiy.voicehat.LED.BLINK)
                elif 'goodbye' in text:
                    break


if __name__ == '__main__':
    main()

結果

実行するとコンソール上に結果を出力しながらvoicekitの頭が光ったり光らなかったりする。
スクリーンショット 2018-01-06 1.00.51.png

感想

簡易な文章であれば日本語でもapiで結構な精度で当てれれるのすごい。
全然公式ドキュメントとか見てないのでなんとも言えないが電気つける程度ならgoogleアシスタンス経由で実行できる気がする。
googleアシスタンス自体は無料?っぽいのでトリガーにするのはそっちが正しい気がするけど、googleアシスタンス経由にすると勝手に喋ったりするから邪魔な気がする。
この実装だと常にcloudspeechのapi叩き続けるのですごいお金かかるしなんか別にいい方法あるんでしょうね。
もう実家出て手元にないのでなんともしませんが、、、

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5