[英]Google Cloud Speech API python code sample has possible bug
I am working through the provided code snippets from the Google Speech API, found here . 我正在通过Google Speech API( 位于)中提供的代码段进行研究 。 The code should be enough to convert a .wav file into transcribed text.
该代码应足以将.wav文件转换为转录文本。
The block of interest is here: 感兴趣的块在这里:
def transcribe_file(speech_file):
"""Transcribe the given audio file."""
from google.cloud import speech
speech_client = speech.Client()
with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio_sample = speech_client.sample(
content=content,
source_uri=None,
encoding='LINEAR16',
sample_rate_hertz=16000)
alternatives = audio_sample.recognize('en-US')
for alternative in alternatives:
print('Transcript: {}'.format(alternative.transcript))
First, I think perhaps the code is old, and sample_rate_hertz=16000
had to be changed to sample_rate=16000
. 首先,我想也许代码是老了,
sample_rate_hertz=16000
,就必须改变以sample_rate=16000
。
After that, I got an error for this line: 在那之后,我得到了这一行的错误:
alternatives = audio_sample.recognize('en-US')
which read 哪个读
AttributeError: 'Sample' object has no attribute 'recognize'
I am curious about how to rectify this. 我很好奇如何纠正这一点。 I can't seem to find any documentation on this method.
我似乎找不到有关此方法的任何文档。 Maybe it needs to be replaced too.
也许也需要更换它。
You use the github quickstart.py example, so i wonder that's not in sync with the Documentation Google Cloud Speech API class sample . 您使用的是github quickstart.py示例,所以我想知道它与文档Google Cloud Speech API类示例不同步。 But it's still BETA .
但这仍然是BETA 。
Assuming isinstance(audio_sample, <class Sample(object)>) == True
, 假设
isinstance(audio_sample, <class Sample(object)>) == True
,
then .recognize
in your 然后
.recognize
你
alternatives = audio_sample.recognize('en-US')
should be one of 应该是其中之一
async_recognize, streaming_recognize, sync_recognize
You nead to read file as binary, then use service.speech().syncrecognize
with a body argument (dict), which contain all required arguments like : 您需要将文件读取为二进制文件,然后将
service.speech().syncrecognize
与body参数(dict)一起使用,其中包含所有必需的参数,例如:
May you try something like: 可以尝试以下方法:
with open(speech_file, 'rb') as speech:
speech_content = base64.b64encode(speech.read())
service = get_speech_service()
service_request = service.speech().syncrecognize(
body={
'config': {
'encoding': 'LINEAR16', # raw 16-bit signed LE samples
'sampleRate': 16000, # 16 khz
'languageCode': 'en-US', # a BCP-47 language tag
},
'audio': {
'content': speech_content.decode('UTF-8')
}
})
response = service_request.execute()
print(json.dumps(response))
Please take a look here , because there is a similar working example. 请在这里看看,因为有一个类似的工作示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.