LoginSignup
0
0

More than 1 year has passed since last update.

Google Assitant SDKで自分のPCをGoogleアシスタントにしよう

Last updated at Posted at 2021-12-28

はじめに

この記事は、Aizu Advent Calander2021の19日目の記事です。
大遅刻です。

本編ここから

こんにちは。PythonParrotです。
ラズパイをGoogleアシスタントにしようとして、まずは自分のPCでやってみたのでその話です。
あと「OK, Google」で起動できるようにしたのでその話も。

やったこと

以下のサイトの通りにやった。

今回はPCだったのでイヤホンとマイクがあればOKだった

躓いたところ

OAuth2.0認証するところ

https://developers.google.com/assistant/sdk/guides/service/python/embed/install-sample
ここのセクションでOAuth認証が必要なのだが、なぜか403で拒否されてしまう(自分のプロジェクトなのに・・・)
OAuth同意画面のテストユーザーに自分を追加するとうまくいく。/

OK, Googleする対応

さて、以下のページにもある通り、GoogleアシスタントSDKには「Hands-free activation」すなわち「OK Google」で起動する機能が存在しない。(deprecatedなGoogle Assistant Libraryにはあるのに・・・)
ということで、Porcupineというワードエンジンを使ってこの機能を実装してみた。

コード

# add for Hotword detectation
import pvporcupine
from pvrecorder import PvRecorder
## 中略 ##
# Porcupineのアクセスキーを引数で指定できるようにする
@click.option('--access_key', default='', help='Access Key')
def main(api_endpoint, credentials, project_id,
         device_model_id, device_id, device_config,
         lang, display, verbose,
         audio_sample_rate, audio_sample_width,
         audio_iter_size, audio_block_size, audio_flush_size,
         grpc_deadline, once, access_key, *args, **kwargs):
### 中略 ###
    handle = pvporcupine.create(access_key=access_key, keywords=['ok google', 'hey google'])
    while True:
        ## ここで起動ワードを指定する
        recorder = PvRecorder(device_index=-1, frame_length=handle.frame_length)
        recorder.start()
        result = -1
        while result < 0:
            pcm = recorder.read()
            ## ここのresultが0以上なら指定したワードを聞き取ったことになる
            result = handle.process(pcm)
        recorder.stop()
        recorder.delete()
        audio_sink = audio_source = (
            audio_helpers.SoundDeviceStream(
                sample_rate=audio_sample_rate,
                sample_width=audio_sample_width,
                block_size=audio_block_size,
                flush_size=audio_flush_size
            )
        )
        # Create conversation stream with the given audio source and sink.
        conversation_stream = audio_helpers.ConversationStream(
            source=audio_source,
            sink=audio_sink,
            iter_size=audio_iter_size,
            sample_width=audio_sample_width,
        )
        with SampleAssistant(lang, device_model_id, device_id,
                             conversation_stream, display,
                             grpc_channel, grpc_deadline,
                             device_handler) as assistant:
            assistant.assist()

全ソースコードはこちらを参照。
https://github.com/rf-castle/google-assistant-sdk-hotwordDetect-sample/blob/master/src/talk_hotword.py

0
0
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
0
0