我正在尝试使用新的Beta alternative languages functionality,该版本允许在创建转录作业并提供检测到的语言以及该语言的转录结果时提供一组语言。
当我从文档页面(同步)运行代码示例时,一切运行正常,并且在结果中返回检测到的语言代码:
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()
speech_file = 'resources/multi.wav'
first_lang = 'en-US'
second_lang = 'es'
with open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio = speech.types.RecognitionAudio(content=content)
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
audio_channel_count=2,
language_code=first_lang,
alternative_language_codes=[second_lang])
print('Waiting for operation to complete...')
response = client.recognize(config, audio)
for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print(result.language_code) # this prints 'en-US'
print('-' * 20)
print('First alternative of result {}: {}'.format(i, alternative))
print(u'Transcript: {}'.format(alternative.transcript))
但是当我尝试异步模式时,语言代码不会与结果一起返回:
from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()
gs_url = 'gs://my-bucket-name/multi.wav'
first_lang = 'en-US'
second_lang = 'es'
audio = speech.types.RecognitionAudio(uri=gs_url)
config = speech.types.RecognitionConfig(
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=44100,
audio_channel_count=2,
language_code=first_lang,
alternative_language_codes=[second_lang])
print('Waiting for operation to complete...')
operation = client.long_running_recognize(config, audio)
response = operation.result(timeout=40)
for i, result in enumerate(response.results):
alternative = result.alternatives[0]
print(result.language_code) # this prints nothing! result.language_code is empty string
print('-' * 20)
print('First alternative of result {}: {}'.format(i, alternative))
print(u'Transcript: {}'.format(alternative.transcript))
尽管有documentation stating explicitly,此行为仍然发生:
语音到文本支持所有语音识别方法的替代语言代码:speech:recognize,speech:longrunningrecognize和Streaming。
关于如何也为异步转录请求获取检测到的语言代码的任何想法吗?