本页面演示了如何使用 Speech-to-Text API 和异步语音识别将长音频文件(时长超过一分钟)转写为文字。
异步语音识别简介
批量语音识别会启动一项长时间运行的音频处理操作。使用异步语音识别转写超过 60 秒的音频。对于较短的音频,同步语音识别更为简单快捷。异步语音识别的上限为 480 分钟(8 小时)。
批量语音识别功能只能转写 Cloud Storage 中存储的音频。转写输出可以在响应中以内嵌方式提供(对于单文件批量识别请求),也可以写入 Cloud Storage。
批量识别请求会返回一个 Operation,其中包含正在进行的请求识别处理的相关信息。您可以轮询操作,以了解操作何时完成以及转写功能何时可用。
准备工作
- 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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
Install the Google Cloud CLI.
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Speech-to-Text APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
Install the Google Cloud CLI.
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
如需初始化 gcloud CLI,请运行以下命令:
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.
客户端库可以使用应用默认凭据轻松进行 Google API 身份验证,并向这些 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][speech-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 政策,请参阅 [管理对项目、文件夹和组织的访问权限][manage-access]。
您还可以通过向服务账号授予对特定 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 文档中的 [创建和管理访问控制列表][buckets-manage-acl]。
使用内嵌结果执行批量识别
以下示例演示了如何对 Cloud Storage 中的音频文件执行批量语音识别,并从响应中读取内嵌的转写结果:
Python
import os
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
def transcribe_batch_gcs_input_inline_output_v2(
audio_uri: str,
) -> cloud_speech.BatchRecognizeResults:
"""Transcribes audio from a Google Cloud Storage URI using the Google Cloud Speech-to-Text API.
The transcription results are returned inline in the response.
Args:
audio_uri (str): The Google Cloud Storage URI of the input audio file.
Such as gs://[BUCKET]/[FILE]
Returns:
cloud_speech.BatchRecognizeResults: The response containing the transcription results.
"""
# Instantiates a client
client = SpeechClient()
config = cloud_speech.RecognitionConfig(
auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
language_codes=["en-US"],
model="chirp_3",
)
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=audio_uri)
request = cloud_speech.BatchRecognizeRequest(
recognizer=f"projects/{PROJECT_ID