使用用戶端程式庫將語音轉錄為文字
本頁說明如何使用Google Cloud 用戶端程式庫,以您偏好的程式設計語言將語音辨識要求傳送至 Cloud Speech-to-Text。
Cloud Speech-to-Text 可讓您將 Google 語音辨識技術輕鬆整合至開發人員應用程式。您可以將音訊資料傳送至 Cloud Speech-to-Text API,該 API 接著會傳回音訊檔案的文字轉錄稿。如要進一步瞭解這項服務,請參閱「Cloud STT 基礎知識」。
事前準備
您必須先完成下列動作,才能向 Cloud Speech-to-Text API 傳送要求。詳情請參閱「事前準備」頁面。
- 為 Google Cloud 專案啟用 Cloud Speech-to-Text。
- 確認已啟用 Cloud Speech-to-Text 的計費功能。
-
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 確認您具備所需的權限,可完成本指南中的操作。如果您是為本指南建立新專案,則已具備必要權限。
- (選用) 建立新的 Cloud Storage bucket 來儲存音訊資料。
必要的角色
如要取得語音轉文字所需的權限,請要求管理員授予您專案的服務使用情形消費者 (roles/serviceusage.serviceUsageConsumer) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
安裝用戶端程式庫
Go
go get cloud.google.com/go/speech/apiv1
Java
If you are using Maven, add
the following to your pom.xml file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code or IntelliJ, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Node.js
安裝程式庫前,請確認您已設定適當的 Node.js 開發環境。
npm install @google-cloud/speech
Python
安裝程式庫前,請確認您已設定適當的 Python 開發環境。
pip install --upgrade google-cloud-speech
提出音訊轉錄要求
您現在可以使用 Cloud STT 將音訊檔案轉錄成文字。請使用下列程式碼,將 recognize 要求傳送至 Cloud Speech-to-Text API。
恭喜!您已將第一個要求傳送至 Cloud STT。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
- 使用 Google Cloud console 刪除不需要的專案。
後續步驟
- 練習轉錄短音訊檔案。
- 瞭解如何批次處理長音訊檔案以進行語音辨識。
- 瞭解如何轉錄串流音訊,例如從麥克風轉錄。
- 使用 Cloud STT 用戶端程式庫,並以您選擇的語言開始使用 Cloud STT。
- 逐步演練範例應用程式。
- 如要獲得最佳效能、準確率與其他提示,請參閱最佳做法說明文件。