运行脚本时出错:Google Speech API

时间:2018-11-15 06:35:35

标签: python raspberry-pi google-speech-api

我一直试图从this link运行以下代码。

import io
import os

# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

# Instantiates a client
client = speech.SpeechClient()

# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    'audio.raw')

# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
    content = audio_file.read()
    audio = types.RecognitionAudio(content=content)

config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=16000,
    language_code='en-US')

# Detects speech in the audio file
response = client.recognize(config, audio)

for result in response.results:
    print('Transcript: {}'.format(result.alternatives[0].transcript))

但是,当我在树莓派上使用python 3.4.2运行它时,出现以下错误。

Traceback (most recent call last):
  File "script.py", line 10, in <module>
    client = speech.SpeechClient()
  File "/usr/local/lib/python3.4/dist-   packages/google/cloud/speech_v1/gapic/speech_client.py", line 137, in __init__
credentials=credentials,
  File "/usr/local/lib/python3.4/dist-packages/google/cloud/speech_v1/gapic/transports/speech_grpc_transport.py", line 76, in __init__
channel)
  File "/usr/local/lib/python3.4/dist- packages/google/api_core/operations_v1/operations_client.py", line 59, in     __init__
    self.operations_stub = operations_pb2.OperationsStub(channel)
AttributeError: 'module' object has no attribute 'OperationsStub'

我已经尝试解决了大约3天,但没有找到任何解决方案。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

不确定是否仍然存在问题。今天早上我经历了同样的事情。

很明显,我似乎安装了googleapis-common-protos版本,该版本是在没有GRPC支持(1.6.0b6)的情况下构建的。我将其降级为1.5.3,重新启动了jupyter服务器,并且现在可以正常工作了。请参考此链接。 Google Cloud Github