我正在研究googlespeechapifound here中提供的代码片段。代码应该足以将.wav文件转换为转录文本。在
这里是您感兴趣的街区:
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))
首先,我认为代码可能很旧,sample_rate_hertz=16000
必须改为sample_rate=16000
。在
在那之后,我得到了一个关于这行的错误:alternatives = audio_sample.recognize('en-US')
上面写着AttributeError: 'Sample' object has no attribute 'recognize'
我很好奇如何纠正这一点。我似乎找不到任何关于这种方法的文档。也许它也需要更换。在
你用的是github快速入门.py例如,我想知道这与文档Google Cloud Speech API class sample不同步。但它仍然是BETA。在
假设
isinstance(audio_sample, <class Sample(object)>) == True
,然后
.recognize
在应该是
^{pr2}$您需要将文件读取为二进制文件,然后将
service.speech().syncrecognize
与正文参数(dict)一起使用,该参数包含所有必需的参数,如:你可以试试像:
请看一下here,因为有一个类似的工作示例。在
相关问题 更多 >
编程相关推荐