SpeechAsyncClient(*, credentials: typing.Optional[google.auth.credentials.Credentials] = None, transport: typing.Union[str, google.cloud.speech_v1p1beta1.services.speech.transports.base.SpeechTransport] = 'grpc_asyncio', client_options: typing.Optional[google.api_core.client_options.ClientOptions] = None, client_info: google.api_core.gapic_v1.client_info.ClientInfo = <google.api_core.gapic_v1.client_info.ClientInfo object>)
实施 Google Cloud Speech API 的服务。
属性
transport
返回客户端实例使用的传输方式。
返回结果 | |
---|---|
类型 | 说明 |
SpeechTransport | 客户端实例使用的传输。 |
方法
SpeechAsyncClient
SpeechAsyncClient(*, credentials: typing.Optional[google.auth.credentials.Credentials] = None, transport: typing.Union[str, google.cloud.speech_v1p1beta1.services.speech.transports.base.SpeechTransport] = 'grpc_asyncio', client_options: typing.Optional[google.api_core.client_options.ClientOptions] = None, client_info: google.api_core.gapic_v1.client_info.ClientInfo = <google.api_core.gapic_v1.client_info.ClientInfo object>)
实例化语音客户端。
参数 | |
---|---|
名称 | 说明 |
credentials |
Optional[google.auth.credentials.Credentials]
要附加到请求的授权凭据。这些凭据用于向服务标识应用;如果未指定任何凭据,客户端将尝试从环境中确定凭据。 |
transport |
Union[str,
要使用的传输。如果设置为 None,系统会自动选择传输方式。 |
client_options |
ClientOptions
客户端的自定义选项。如果提供了 |
例外情况 | |
---|---|
类型 | 说明 |
google.auth.exceptions.MutualTlsChannelError | 如果因任何原因导致相互 TLS 传输创建失败。 |
common_billing_account_path
common_billing_account_path(billing_account: str) -> str
返回完全限定的 billing_account 字符串。
common_folder_path
common_folder_path(folder: str) -> str
返回完全限定的文件夹字符串。
common_location_path
common_location_path(project: str, location: str) -> str
返回一个完全限定的位置字符串。
common_organization_path
common_organization_path(organization: str) -> str
返回完全限定的组织字符串。
common_project_path
common_project_path(project: str) -> str
返回完全限定的项目字符串。
custom_class_path
custom_class_path(project: str, location: str, custom_class: str) -> str
返回完全限定的 custom_class 字符串。
from_service_account_file
from_service_account_file(filename: str, *args, **kwargs)
使用提供的凭据文件创建此客户端的实例。
参数 | |
---|---|
名称 | 说明 |
filename |
str
服务账号私钥 JSON 文件的路径。 |
返回结果 | |
---|---|
类型 | 说明 |
SpeechAsyncClient | 构建的客户端。 |
from_service_account_info
from_service_account_info(info: dict, *args, **kwargs)
使用提供的凭据信息创建此客户端的实例。
参数 | |
---|---|
名称 | 说明 |
info |
dict
服务账号私钥信息。 |
返回结果 | |
---|---|
类型 | 说明 |
SpeechAsyncClient | 构建的客户端。 |
from_service_account_json
from_service_account_json(filename: str, *args, **kwargs)
使用提供的凭据文件创建此客户端的实例。
参数 | |
---|---|
名称 | 说明 |
filename |
str
服务账号私钥 JSON 文件的路径。 |
返回结果 | |
---|---|
类型 | 说明 |
SpeechAsyncClient | 构建的客户端。 |
get_mtls_endpoint_and_cert_source
get_mtls_endpoint_and_cert_source(
client_options: typing.Optional[
google.api_core.client_options.ClientOptions
] = None,
)
返回双向 TLS 的 API 端点和客户端证书源。
客户端证书来源按以下顺序确定:(1) 如果 GOOGLE_API_USE_CLIENT_CERTIFICATE
环境变量不是“true”,则客户端证书来源为 None。
(2) 如果提供了 client_options.client_cert_source
,则使用提供的 client_options.client_cert_source
;如果存在默认客户端证书来源,则使用默认客户端证书来源;否则,客户端证书来源为 None。
API 端点按以下顺序确定:(1) 如果提供了 client_options.api_endpoint
,则使用提供的端点。
(2) 如果 GOOGLE_API_USE_CLIENT_CERTIFICATE
环境变量为“always”,则使用默认 mTLS 端点;如果该环境变量为“never”,则使用默认 API 端点;否则,如果存在客户端证书源,则使用默认 mTLS 端点,否则使用默认 API 端点。
如需了解详情,请访问 https://google.aip.dev/auth/4114。
参数 | |
---|---|
名称 | 说明 |
client_options |
google.api_core.client_options.ClientOptions
客户端的自定义选项。此方法中只能使用 |
例外情况 | |
---|---|
类型 | 说明 |
google.auth.exceptions.MutualTLSChannelError | 如果发生任何错误。 |
返回结果 | |
---|---|
类型 | 说明 |
Tuple[str, Callable[[], Tuple[bytes, bytes]]] | 返回要使用的 API 端点和客户端证书来源。 |
get_operation
get_operation(
request: typing.Optional[
google.longrunning.operations_pb2.GetOperationRequest
] = None,
*,
retry: typing.Union[
google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
] = _MethodDefault._DEFAULT_VALUE,
timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.longrunning.operations_pb2.Operation
获取长时间运行的操作的最新状态。
参数 | |
---|---|
名称 | 说明 |
request |
请求对象。 |
retry |
google.api_core.retry.Retry
指定应重试哪些错误(如果有)。 |
timeout |
float
相应请求的超时时间。 |
metadata |
Sequence[Tuple[str, str]]
应随请求一起作为元数据发送的字符串。 |
返回结果 | |
---|---|
类型 | 说明 |
| Operation 对象。 |
get_transport_class
get_transport_class() -> (
typing.Type[google.cloud.speech_v1p1beta1.services.speech.transports.base.SpeechTransport]
)
返回合适的传输类。
list_operations
list_operations(
request: typing.Optional[
google.longrunning.operations_pb2.ListOperationsRequest
] = None,
*,
retry: typing.Union[
google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
] = _MethodDefault._DEFAULT_VALUE,
timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.longrunning.operations_pb2.ListOperationsResponse
列出与请求中指定的过滤条件匹配的操作。
参数 | |
---|---|
名称 | 说明 |
request |
请求对象。 |
retry |
google.api_core.retry.Retry
指定应重试哪些错误(如果有)。 |
timeout |
float
相应请求的超时时间。 |
metadata |
Sequence[Tuple[str, str]]
应随请求一起作为元数据发送的字符串。 |
返回结果 | |
---|---|
类型 | 说明 |
| ListOperations 方法的响应消息。 |
long_running_recognize
long_running_recognize(
request: typing.Optional[
typing.Union[
google.cloud.speech_v1p1beta1.types.cloud_speech.LongRunningRecognizeRequest, dict
]
] = None,
*,
config: typing.Optional[
google.cloud.speech_v1p1beta1.types.cloud_speech.RecognitionConfig
] = None,
audio: typing.Optional[
google.cloud.speech_v1p1beta1.types.cloud_speech.RecognitionAudio
] = None,
retry: typing.Union[
google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
] = _MethodDefault._DEFAULT_VALUE,
timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.api_core.operation_async.AsyncOperation
执行异步语音识别:通过 google.longrunning.Operations 接口接收结果。返回 Operation.error
或包含 LongRunningRecognizeResponse
消息的 Operation.response
。如需详细了解异步语音识别,请参阅 <https://cloud.google.com/speech-to-text/docs/async-recognize>
。
# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in:
# https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import speech_v1p1beta1
async def sample_long_running_recognize():
# Create a client
client = speech_v1p1beta1.SpeechAsyncClient()
# Initialize request argument(s)
config = speech_v1p1beta1.RecognitionConfig()
config.language_code = "language_code_value"
audio = speech_v1p1beta1.RecognitionAudio()
audio.content = b'content_blob'
request = speech_v1p1beta1.LongRunningRecognizeRequest(
config=config,
audio=audio,
)
# Make the request
operation = client.long_running_recognize(request=request)
print("Waiting for operation to complete...")
response = (await operation).result()
# Handle the response
print(response)
参数 | |
---|---|
名称 | 说明 |
request |
Optional[Union[google.cloud.speech_v1p1beta1.types.LongRunningRecognizeRequest, dict]]
请求对象。客户端为 |
config |
RecognitionConfig
必需。向识别器提供指定如何处理请求的信息。这对应于 |
audio |
RecognitionAudio
必需。要识别的音频数据。这对应于 |
retry |
google.api_core.retry.Retry
指定应重试哪些错误(如果有)。 |
timeout |
float
相应请求的超时时间。 |
metadata |
Sequence[Tuple[str, str]]
应随请求一起作为元数据发送的字符串。 |
返回结果 | |
---|---|
类型 | 说明 |
google.api_core.operation_async.AsyncOperation | 表示长时间运行的操作的对象。相应操作的结果类型将为 LongRunningRecognizeResponse 。LongRunningRecognize 方法返回给客户端的唯一消息。它包含零个或多个连续的 SpeechRecognitionResult 消息形式的结果。它包含在 google::longrunning::Operations 服务的 GetOperation 调用返回的 Operation 的 result.response 字段中。 |
parse_common_billing_account_path
parse_common_billing_account_path(path: str) -> typing.Dict[str, str]
将 billing_account 路径解析为组成部分。
parse_common_folder_path
parse_common_folder_path(path: str) -> typing.Dict[str, str]
将文件夹路径解析为组成部分。
parse_common_location_path
parse_common_location_path(path: str) -> typing.Dict[str, str]
将位置路径解析为组成部分。
parse_common_organization_path
parse_common_organization_path(path: str) -> typing.Dict[str, str]
将组织路径解析为组成部分。
parse_common_project_path
parse_common_project_path(path: str) -> typing.Dict[str, str]
将项目路径解析为组成部分。
parse_custom_class_path
parse_custom_class_path(path: str) -> typing.Dict[str, str]
将 custom_class 路径解析为它的组成部分。
parse_phrase_set_path
parse_phrase_set_path(path: str) -> typing.Dict[str, str]
将 phrase_set 路径解析为该方法的组成部分。
phrase_set_path
phrase_set_path(project: str, location: str, phrase_set: str) -> str
返回完全限定的 phrase_set 字符串。
recognize
recognize(
request: typing.Optional[
typing.Union[google.cloud.speech_v1p1beta1.types.cloud_speech.RecognizeRequest, dict]
] = None,
*,
config: typing.Optional[
google.cloud.speech_v1p1beta1.types.cloud_speech.RecognitionConfig
] = None,
audio: typing.Optional[
google.cloud.speech_v1p1beta1.types.cloud_speech.RecognitionAudio
] = None,
retry: typing.Union[
google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
] = _MethodDefault._DEFAULT_VALUE,
timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> google.cloud.speech_v1p1beta1.types.cloud_speech.RecognizeResponse
执行同步语音识别:在发送和处理完所有音频后接收结果。
# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in:
# https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import speech_v1p1beta1
async def sample_recognize():
# Create a client
client = speech_v1p1beta1.SpeechAsyncClient()
# Initialize request argument(s)
config = speech_v1p1beta1.RecognitionConfig()
config.language_code = "language_code_value"
audio = speech_v1p1beta1.RecognitionAudio()
audio.content = b'content_blob'
request = speech_v1p1beta1.RecognizeRequest(
config=config,
audio=audio,
)
# Make the request
response = await client.recognize(request=request)
# Handle the response
print(response)
参数 | |
---|---|
名称 | 说明 |
request |
Optional[Union[google.cloud.speech_v1p1beta1.types.RecognizeRequest, dict]]
请求对象。客户端为 |
config |
RecognitionConfig
必需。向识别器提供指定如何处理请求的信息。这对应于 |
audio |
RecognitionAudio
必需。要识别的音频数据。这对应于 |
retry |
google.api_core.retry.Retry
指定应重试哪些错误(如果有)。 |
timeout |
float
相应请求的超时时间。 |
metadata |
Sequence[Tuple[str, str]]
应随请求一起作为元数据发送的字符串。 |
返回结果 | |
---|---|
类型 | 说明 |
google.cloud.speech_v1p1beta1.types.RecognizeResponse | Recognize 方法返回给客户端的唯一消息。它包含零个或多个连续的 SpeechRecognitionResult 消息形式的结果。 |
streaming_recognize
streaming_recognize(
requests: typing.Optional[
typing.AsyncIterator[
google.cloud.speech_v1p1beta1.types.cloud_speech.StreamingRecognizeRequest
]
] = None,
*,
retry: typing.Union[
google.api_core.retry.Retry, google.api_core.gapic_v1.method._MethodDefault
] = _MethodDefault._DEFAULT_VALUE,
timeout: typing.Union[float, object] = _MethodDefault._DEFAULT_VALUE,
metadata: typing.Sequence[typing.Tuple[str, str]] = ()
) -> typing.Awaitable[
typing.AsyncIterable[
google.cloud.speech_v1p1beta1.types.cloud_speech.StreamingRecognizeResponse
]
]
执行双向流式语音识别:在发送音频时接收结果。此方法只能通过 gRPC API 使用,REST 不支持此方法。
# This snippet has been automatically generated and should be regarded as a
# code template only.
# It will require modifications to work:
# - It may require correct/in-range values for request initialization.
# - It may require specifying regional endpoints when creating the service
# client as shown in:
# https://googleapis.dev/python/google-api-core/latest/client_options.html
from google.cloud import speech_v1p1beta1
async def sample_streaming_recognize():
# Create a client
client = speech_v1p1beta1.SpeechAsyncClient()
# Initialize request argument(s)
streaming_config = speech_v1p1beta1.StreamingRecognitionConfig()
streaming_config.config.language_code = "language_code_value"
request = speech_v1p1beta1.StreamingRecognizeRequest(
streaming_config=streaming_config,
)
# This method expects an iterator which contains
# 'speech_v1p1beta1.StreamingRecognizeRequest' objects
# Here we create a generator that yields a single `request` for
# demonstrative purposes.
requests = [request]
def request_generator():
for request in requests:
yield request
# Make the request
stream = await client.streaming_recognize(requests=request_generator())
# Handle the response
async for response in stream:
print(response)
参数 | |
---|---|
名称 | 说明 |
requests |
AsyncIterator[
请求对象 AsyncIterator。客户端为 |
retry |
google.api_core.retry.Retry
指定应重试哪些错误(如果有)。 |
timeout |
float
相应请求的超时时间。 |
metadata |
Sequence[Tuple[str, str]]
应随请求一起作为元数据发送的字符串。 |
返回结果 | |
---|---|
类型 | 说明 |
AsyncIterable[google.cloud.speech_v1p1beta1.types.StreamingRecognizeResponse] | StreamingRecognizeResponse 是 StreamingRecognize 返回给客户端的唯一消息。一系列零个或零个以上 StreamingRecognizeResponse 消息将流式传输回客户端。如果没有可识别的音频,并且 single_utterance 设置为 false,则不会向客户端流式返回任何消息。以下示例展示了处理音频时可能返回的一系列 StreamingRecognizeResponse:1. results { alternatives { transcript: "tube" } stability: 0.01 } 2. results { alternatives { transcript: "to be a" } stability: 0.01 } 3. results { alternatives { transcript: "to be" } stability: 0.9 } results { alternatives { transcript: " or not to be" } stability: 0.01 } 4. results { alternatives { transcript: "to be or not to be" confidence: 0.92 } alternatives { transcript: "to bee or not to bee" } is_final: true } 5. results { alternatives { transcript: " that's" } stability: 0.01 } 6. results { alternatives { transcript: " that is" } stability: 0.9 } results { alternatives { transcript: " the question" } stability: 0.01 } 7. results { alternatives { transcript: " that is the question" confidence: 0.98 } alternatives { transcript: " that was the question" } is_final: true } 注意事项:- 上述响应中只有两个(第 4 个和第 7 个)包含最终结果;它们通过 is_final: true 指示。将这些响应串联在一起即可获得完整的转录内容:“to be or not to be that is the question”。- 其他响应包含临时结果。第 3 条和第 6 条包含两个临时 \results :第一部分稳定性高,不太可能改变;第二部分稳定性低,很可能会改变。界面设计师可能会选择仅显示高稳定性结果。- 上面显示的特定稳定性和置信度值仅用于说明目的。实际值可能会有所不同。- 在每个响应中,只会设置以下字段之一:error、speech_event_type 或一个或多个(重复)结果。 |