【发布时间】:2017-06-10 06:51:23
【问题描述】:
我正在尝试迁移到 Google 的新语音 API,因为测试版即将到期。然而,新的似乎没有工作。首先,这个安装似乎失败了:
pip install --upgrade google-cloud-speech
它会产生以下错误以及一大堆其他错误
ImportError: cannot import name IncompleteRead
尽管如此,我现在似乎可以使用他们的示例文件让它工作,但我自己的 base64 编码数据不起作用....尽管使用 beta API 的数据完全相同。有人遇到同样的问题吗?
我使用的代码如下,导致“语音 API 没有返回结果”。我看不到 API 在哪里提供解析错误消息的能力。如果我取消注释 3 行以使用 audio.raw 它工作正常,但这是谷歌提供的示例。我无法理解为什么我的文件无法正常工作,就好像我使用完全相同的 base64 编码方法和相同的测试文件一样,它可以在 beta API 中正常工作。
#!/usr/bin/env python
import io
import os
# Imports the Google Cloud client library
from google.cloud import speech
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "myfile.json"
def run_quickstart(speech_content):
# Instantiates a client
speech_client = speech.Client()
#file_name = os.path.join( os.path.dirname(__file__), 'audio.raw')
#audio_file = io.open(file_name, 'rb')
#speech_content = audio_file.read()
sample = speech_client.sample(
speech_content,
source_uri=None,
encoding='LINEAR16',
sample_rate_hertz=16000)
# Detects speech in the audio file
alternatives = sample.recognize('en-US')
for alternative in alternatives:
print('Transcript: {}'.format(alternative.transcript))
if __name__ == '__main__':
import base64
file = open('test.wav', 'rb')
data = file.read()
speech_content = base64.b64encode(data)
#print speech_content
run_quickstart(speech_content)
【问题讨论】:
-
你可以尝试像
pip install --upgrade pip这样升级pip