本教學課程說明如何使用 Cloud Speech-to-Text 轉錄用手機錄製的音訊。
音訊檔案可能來自許多不同的來源。音訊資料可能來自手機 (例如語音留言),也可能來自影片檔案中的音軌。
Cloud STT 可以使用數種機器學習模型的其中一種來轉錄音訊檔案,完美配合音訊的原始來源。透過指定原始音訊的來源,即可獲得更好的語音內容轉錄結果。指定來源可以讓 Cloud STT 透過使用類似您音訊檔案的資料訓練的機器學習模型,處理您的音訊檔案。
目標
- 將使用手機錄製的音訊 (例如語音留言) 的轉錄要求傳送至 Cloud STT。
- 針對音訊轉錄要求,指定強化版語音辨識模型。
費用
本教學課程使用的 Cloud Platform 可計費元件包括:
- Cloud Speech-to-Text
使用 Pricing Calculator 可根據您的預測使用量來產生預估費用。
事前準備
本教學課程有幾項必備條件:
- 您已在 Google Cloud 控制台中設定 Cloud Speech-to-Text 專案。
- 您已在 Google Cloud 控制台使用應用程式預設憑證設定環境。
- 您已針對所選程式設計語言建立開發環境。
- 您已針對您選擇的程式設計語言安裝 Google Cloud 用戶端程式庫。
傳送要求
為了成功轉錄使用手機擷取的音訊 (例如通話或語音留言),請將 model 酬載中的 RecognitionConfig 欄位設為 phone_call。model 欄位可告知 Cloud Speech-to-Text API 要使用哪個語音辨識模型來處理轉錄要求。
您可以使用強化版模型改善手機音訊轉錄的結果。如要使用強化版模型,請將 useEnhanced 酬載中的 RecognitionConfig 欄位設為 true。
下列程式碼範例示範如何在呼叫 Cloud STT 時選取特定轉錄模型。
通訊協定
如需完整資訊,請參閱 speech:recognize API 端點。
如要執行同步語音辨識,請提出 POST 要求並提供適當的要求內容。以下為使用 curl 的 POST 要求範例,這個範例使用 Google Cloud CLI 產生存取權杖。如需 gcloud CLI 的安裝操作說明,請參閱快速入門導覽課程。
curl -s -H "Content-Type: application/json" \ -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ https://speech.googleapis.com/v1/speech:recognize \ --data '{ "config": { "encoding": "LINEAR16", "languageCode": "en-US", "enableWordTimeOffsets": false, "enableAutomaticPunctuation": true, "model": "phone_call", "useEnhanced": true }, "audio": { "uri": "gs://cloud-samples-tests/speech/commercial_mono.wav" } }'
如要進一步瞭解如何設定要求內容,請參閱 RecognitionConfig 參考說明文件。
如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼與 JSON 格式的回應:
{ "results": [ { "alternatives": [ { "transcript": "Hi, I'd like to buy a Chromecast. I was wondering whether you could help me with that.", "confidence": 0.8930228 } ], "resultEndTime": "5.640s" }, { "alternatives": [ { "transcript": " Certainly, which color would you like? We are blue black and red.", "confidence": 0.9101991 } ], "resultEndTime": "10.220s" }, { "alternatives": [ { "transcript": " Let's go with the black one.", "confidence": 0.8818244 } ], "resultEndTime": "13.870s" }, { "alternatives": [ { "transcript": " Would you like the new Chromecast Ultra model or the regular Chromecast?", "confidence": 0.94733626 } ], "resultEndTime": "18.460s" }, { "alternatives": [ { "transcript": " Regular Chromecast is fine. Thank you. Okay. Sure. Would you like to ship it regular or Express?", "confidence": 0.9519095 } ], "resultEndTime": "25.930s" }, { "alternatives": [ { "transcript": " Express, please.", "confidence": 0.9101229 } ], "resultEndTime": "28.260s" }, { "alternatives": [ { "transcript": " Terrific. It's on the way. Thank you. Thank you very much. Bye.", "confidence": 0.9321616 } ], "resultEndTime": "34.150s" } ] }
Go
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Go API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Java API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Node.js API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Cloud STT 的用戶端程式庫,請參閱「Cloud STT 用戶端程式庫」。詳情請參閱「Cloud STT Python API 參考文件」。
如要向 Cloud STT 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他語言
C#:請按照用戶端程式庫頁面上的 C# 設定操作說明完成相關步驟,然後參閱「.NET 適用的 Cloud STT 參考文件」。
PHP:請按照用戶端程式庫頁面上的 PHP 設定操作說明完成相關步驟,然後參閱「PHP 適用的 Cloud STT 參考文件」。
Ruby:請按照用戶端程式庫頁面上的 Ruby 設定操作說明完成相關步驟,然後參閱「Ruby 適用的 Cloud STT 參考文件」。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。
如要刪除專案:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
刪除執行個體
如要刪除 Compute Engine 執行個體:
- In the Google Cloud console, go to the VM instances page.
- Select the checkbox for the instance that you want to delete.
- To delete the instance, click More actions, click Delete, and then follow the instructions.
刪除預設網路的防火牆規則
刪除防火牆規則:
- In the Google Cloud console, go to the Firewall page.
- Select the checkbox for the firewall rule that you want to delete.
- To delete the firewall rule, click Delete.