我正在通过谷歌语音API提供的代码片段found here。代码应该足以将.wav文件转换为转录文本。谷歌云语音API python代码示例有可能的bug
关注的块是在这里:
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'
我很好奇如何纠正这一点。我似乎无法找到有关此方法的任何文档。也许它也需要被替换。
请看看[这里](http://stackoverflow.com/questions/38703853/how-to-use-google-speech-recognition-api-in-python/38788928#38788928),因为有一个类似的工作例子 –