Quantcast
Viewing all articles
Browse latest Browse all 684

[LattePanda] 使用Google Assistant玩互動語音助理

Image may be NSFW.
Clik here to view.

作者/攝影   袁佑緣
時間   1小時
成本
難度   * * * * *
材料表
  • LattePanda
  • 螢幕
  • 鍵盤
  • 滑鼠
  • 喇叭
  • 麥克風

本文說明如何使用 LattePanda搭配Google推出的Google Assistant服務,來打造一個智慧助手,一起來看看是如何完成的吧!

 

STEP0. 事前準備

為了要能夠使用語音服務,除了我們的小電腦LattePanda外,請額外再準備USB麥克風以及喇叭,另外本次的範例會需要連到Google 雲端的服務,所以請確認LattePanda能夠連到網路。

Image may be NSFW.
Clik here to view.

 

STEP1. 首先我們要到先設定一個Google Developer Project

請點選以下的網址到Google Cloud Platform,如下圖。

https://console.cloud.google.com/project

Image may be NSFW.
Clik here to view.

接著請點選”CREATE PROJECT”,建立一個新的專案,如下圖,建立一個新專案名稱為my-google-assistant。

Image may be NSFW.
Clik here to view.

初始設定完成後,要等幾分鐘才會建立完成,回到Platform的右上角可以看到正在建立新專案。

Image may be NSFW.
Clik here to view.

 

STEP2. 接著我們要完成Google Assistant API的設定

首先,請連到Google API的設定網頁(https://console.developers.google.com/apis/api/embeddedassistant.googleapis.com/overview),並把Google Assistant API設定為”ENABLE”。

Image may be NSFW.
Clik here to view.

接著,為了要取用Google雲端的API,我們必須先有一個認證的client端,所以請連到以下的網址來建立一組oauth client ID。

https://console.developers.google.com/apis/credentials/oauthclient

Image may be NSFW.
Clik here to view.

而如果還沒有設定過Consent Screen的話,系統會提示您填上必要的設定,請點選”Configure consent screen”,設定如下圖。

Image may be NSFW.
Clik here to view.

最後回到”Create client ID”這一步,在Application Type填上Other即可。

Image may be NSFW.
Clik here to view.

到這裡,我們就已經完成了Client ID的設定囉!

Image may be NSFW.
Clik here to view.

最後,請把這個Client ID資訊下載到電腦上(請點選右方的下載箭頭),待會在進行Google Assistant認證的時候會用到。

Image may be NSFW.
Clik here to view.

STEP3.設定Google Acitvity

 

在進到安裝Google Assistant 的程式之前,我們要先設定一下這個Google帳號的

Activity,請連到以下的網址進行設定。

 

https://myaccount.google.com/activitycontrols

 

至於什麼是Activity呢?簡單說就是允許Google能否取用某些個人資訊,例如聲音、App資訊等,為了要能夠讓Google助手能正常運作,請將以下三個Activity設為Enabled。

 

Web & App Activity

Image may be NSFW.
Clik here to view.

Device Information

Image may be NSFW.
Clik here to view.

Voice & Audio Activity

Image may be NSFW.
Clik here to view.

STEP4.安裝Google Assistant SDK Python函式庫

我們使用的Google Assistant SDK是基於Python這個程式語言下去實做的,所以必須先在電腦上安裝Python的執行環境。

請到Python的官網(https://www.python.org/downloads/windows/)下載最新版的安裝檔。

Image may be NSFW.
Clik here to view.

接下來執行下載的Python安裝檔,並記得將勾選”Add Python to PATH”的選項,讓Python的執行位址加到Windows的系統變數中。

Image may be NSFW.
Clik here to view.

再來,請按下Windows的快捷鍵<Win>+R,啟動Windows快速開啟,並打上cmd呼叫Windows Command Prompt。

Image may be NSFW.
Clik here to view.

請在開啟的Command Prompt中輸入以下指令

 

pip install –upgrade setuptools

 

呼叫Python的套件管理員更新setuptools這個套件。

Image may be NSFW.
Clik here to view.

接著再輸入以下指令

 

pip install –upgrade google-auth-oauthlib[tool]

 

把Google認證oauth client的工具安裝到系統中。

Image may be NSFW.
Clik here to view.

還記得我們在STEP2中下載的Client ID JSON檔嗎?請將下載的JSON檔的位址複製起來,並改到以下指令中的”path/to/client_secret_XXXXX.json”

 

在Command Prompt中輸入以下指令以完成認證。

 

google-oauthlib-tool –client-secrets path/to/client_secret_XXXXX.json –scope https://www.googleapis.com/auth/assistant-sdk-prototype –save

 

Image may be NSFW.
Clik here to view.

Google會自動提示您在瀏覽器中完成認證,認證完成如下圖。

Image may be NSFW.
Clik here to view.

認證完的credential.json會自動存在電腦的系統上,Windows環境中預設會存在AppData中,使用者以後在啟動Google Assistant時不用擔心需要再次認證。

Image may be NSFW.
Clik here to view.

最後我們看到Google Assistant SDK的source,前面有說過這個SDK是基於Python這款程式語言去實做的,使用上非常容易上手,而官方的github網址在這邊(https://github.com/googlesamples/assistant-sdk-python)。

Image may be NSFW.
Clik here to view.

裡面有紀錄如何安裝SDK,以及如何去使用提供的函式庫功能,另外還有一些現成的範例可以讓入門的使用者去做體驗,如果讀者想要擴充、修改自己的Google Assistant的功能,可以下載(Download ZIP)整個專案來去修改,有興趣的朋友不妨去嘗試看看!

 

而本文章為了快速的demo,在這邊直接使用包好的assistant-sdk範例套件,讓使用者可以省去下載、手動安裝的過程,所以請在Command Prompt中輸入以下指令

 

pip install google-assistant-sdk[samples]

 

Image may be NSFW.
Clik here to view.

最後我們輸入以下的指令來執行一個簡單的範例pushtotalk。

 

googlesamples-assistant-pushtotalk

 

Image may be NSFW.
Clik here to view.

這支pushtotalk會提示使用者按下Enter鍵並開啟Google Assistant的語音對談功能,請確定您的麥克風與喇叭都有接到LattePanda上,並確認前面的步驟都設定正確,接下來就可以來玩玩看Google Assistant有什麼功能吧!

 

 

相關文章:

 

 

 


Viewing all articles
Browse latest Browse all 684

Trending Articles