【问题标题】:Problems with new Google Speech API新的 Google Speech API 的问题
【发布时间】: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

标签: python google-speech-api


【解决方案1】:

在 Pip 安装方面,卸载和重新安装似乎已经解决了问题。 Debian 默认的 Pip 版本显然很旧,这可能是问题所在。使用 apt-get 进行升级似乎不会升级 Pip。

在 Google API 方面,我发现他们用于构建 Python 代码的方法不起作用。在使用了他们的 API 测试器后,我最终能够修改我的原始代码,以便它可以与新的 API 一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多