使用用戶端程式庫將語音轉錄為文字
本頁面說明如何使用 Google Cloud 用戶端程式庫,以您偏好的程式設計語言將語音辨識要求傳送至 Speech-to-Text。
Speech-to-Text 可讓您將 Google 語音辨識技術輕鬆整合至開發人員應用程式。您可將音訊資料傳送至 Speech-to-Text API,然後 API 會傳回該音訊檔案的文字轉錄結果。如要進一步瞭解這項服務,請參閱語音轉文字基本概念。
事前準備
您必須先完成下列動作,才能將要求傳送至 Speech-to-Text API。詳情請參閱「事前準備」頁面。
- 在 Google Cloud 專案中啟用 Speech-to-Text。
- 確認已啟用 Speech-to-Text 的計費功能。
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
- (選用) 建立新的 Google Cloud Storage 值區,以儲存音訊資料。
安裝用戶端程式庫
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, IntelliJ, or Eclipse, 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
提出音訊轉錄要求
現在您可以使用 Speech-to-Text 將音訊檔案轉錄成文字。請使用下列程式碼將 recognize
要求傳送至 Speech-to-Text API。
恭喜!您已將您的第一個要求傳送至 Speech-to-Text。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
- 使用 Google Cloud console 刪除您不需要的專案。
後續步驟
- 練習轉錄短音訊檔案。
- 瞭解如何批次處理長音訊檔案以進行語音辨識。
- 瞭解如何轉錄串流音訊,例如從麥克風轉錄。
- 使用 Speech-to-Text 用戶端程式庫,以您選擇的語言開始使用 Speech-to-Text。
- 逐步演練範例應用程式。
- 如要獲得最佳效能、準確率與其他提示,請參閱最佳做法說明文件。