使用 Google Speech API 將語音檔案辨識為文字 這個東西不出意外的失效了,Google 養肥計畫成功了,現在必須使用 Google Cloud 服務
Pricing Cloud Speech-to-Text API 價目表,每月的前 60 分鐘免費,爾後 每 15 秒 $0.006 美元
某天發生問題之後,因為只有
--
繼續使用 curl 指令
官網說明上的流程
點選 → 選擇專案 → 下載 json
Install and initialize the Cloud SDK 安裝 SDK
啟用帳號
gcloud auth activate-service-account --key-file=[PATH]
例外整理的流程如下
安裝 Cloud SDK
登入綁定 Google Cloud 帳號
準備 curl 所需資訊一:存取 Token
curl 所需資訊二:語音查詢 Json
組合 curl 語法
--
存取 Token
在 Command Line 下執行以下指令,所以必須事先安裝 Cloud SDK
> gcloud auth print - access - token
正常情況下會得到類似 ya29.c.El-xxxxxxxxxxxxxxx 的字串,這就是 Token 有存取的時間限制
--
語音查詢 Json
基本架構為
{
"config" : {
"encoding" : "FLAC" ,
"sampleRateHertz" : 16000 ,
"languageCode" : "en-US" ,
"enableWordTimeOffsets" : false
} ,
"audio" : {
"uri" : "gs://cloud-samples-tests/speech/brooklyn.flac"
}
}
預設的檔案路徑存放在 Google Cloud Storage ,不想那麼麻煩,所以根據
修改 audio 為 content ,將 flac 音檔轉為 base64 即可,以下為範例
{
"config" : {
"encoding" : "FLAC" ,
"sampleRateHertz" : 44100 ,
"languageCode" : "zh-TW" ,
"enableWordTimeOffsets" : false
} ,
"audio" : {
"content" : "....................省略十萬字的 Base64 編碼 ....RWmXOmi5Eh0J5v87fSjxg=="
}
}
--
最後的 curl 指令
curl - s - H "Content-Type: application/json" - H "Authorization: Bearer ya29.c.El-xxxxxxxxxxxxxxx" https : //speech.googleapis.com/v1/speech:recognize -d @sync-request.json
回傳結果也是 Json 格式,和以前回傳的 key 略有不同
{
"results" : [
{
"alternatives" : [
{
"transcript" : "判斷回傳文字"
"confidence" : 0.93967086
}
]
}
]
}
--
Google developers console API activation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"error" : {
"code" : 403 ,
"message" : "Cloud Speech API has not been used in project 32555940559 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=32555940559 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry." ,
"status" : "PERMISSION_DENIED" ,
"details" : [
{
"@type" : "type.googleapis.com/google.rpc.Help" ,
"links" : [
{
"description" : "Google developers console API activation" ,
"url" : "https://console.developers.google.com/apis/api/speech.googleapis.com/overview?project=32555940559"
}
]
}
]
}
}
到 console.cloud.google.com/apis 的憑證,建立新的「服務帳戶金鑰」以及下載該金鑰 json 檔案後
使用以下指令啟用
gcloud auth activate - service - account -- key - file = xxxx . json
--
2,783 total views, 2 views today
文章導覽