這個頁面說明如何使用同步語音辨識功能,將短音訊檔案轉錄為文字內容。
同步語音辨識會針對短音訊 (少於 60 秒) 傳回辨識出的文字。
您可以從本機檔案將音訊內容直接傳送至 Speech-to-Text,Speech-to-Text 也可以處理儲存在 Cloud Storage 值區中的音訊內容。 如要瞭解同步語音辨識要求的限制,請參閱配額與限制頁面。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
-
Make sure that you have the following role or roles on the project: Cloud Speech Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「IAM」頁面 - 選取專案。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
Install the Google Cloud CLI.
-
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
-
Make sure that you have the following role or roles on the project: Cloud Speech Administrator
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往「IAM」頁面 - 選取專案。
- 按一下「授予存取權」 。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
Install the Google Cloud CLI.
-
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
- 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.
用戶端程式庫可以使用應用程式預設憑證,輕鬆向 Google API 進行驗證,然後傳送要求給這些 API。使用應用程式預設憑證,您可以在本機測試及部署應用程式,不必變更基礎程式碼。詳情請參閱「 驗證以使用用戶端程式庫」。
此外,請確認您已安裝用戶端程式庫。
對本機檔案執行同步語音辨識
以下是對本機音訊檔案執行同步語音辨識的範例:
Python
對遠端檔案執行同步語音辨識
為方便起見,Speech-to-Text API 可以對位於 Cloud Storage 中的音訊檔案直接執行同步語音辨識,您無需在要求內容中傳送音訊檔案的內容。
Speech-to-Text 會使用服務帳戶存取 Cloud Storage 中的檔案。根據預設,服務帳戶可以存取同一個專案中的 Cloud Storage 檔案。
服務帳戶電子郵件地址如下:
service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com
如要轉錄其他專案中的 Cloud Storage 檔案,您可以授予該服務帳戶在其他專案中的 Speech-to-Text Service Agent 角色:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
--role=roles/speech.serviceAgent
如要進一步瞭解專案 IAM 政策,請參閱「管理專案、資料夾和機構的存取權」一文。
您也可以授予服務帳戶特定 Cloud Storage 值區的權限,提供更精細的存取權:
gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
--member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-speech.iam.gserviceaccount.com \
--role=roles/storage.admin
如要進一步瞭解如何管理 Cloud Storage 存取權,請參閱 Cloud Storage 說明文件中的「建立及管理存取權控管清單」。
以下是對位於 Cloud Storage 中的檔案執行同步語音辨識的範例:
Python
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
控制台
gcloud
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID