导入错误:无法导入名称“speech_v1p1beta1”

Or *_*Bar 4 python google-cloud-speech

我正在尝试在 python 中运行以下脚本,它将视频文件转换为文本:

from google.cloud import speech_v1p1beta1 as speech
client = speech.SpeechClient()

with open(speech_file, 'rb') as audio_file:
    content = audio_file.read()

audio = speech.types.RecognitionAudio(content=content)

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

response = client.recognize(config, audio)

for i, result in enumerate(response.results):
    alternative = result.alternatives[0]
    print('-' * 20)
    print('First alternative of result {}'.format(i))
    print(u'Transcript: {}'.format(alternative.transcript))
Run Code Online (Sandbox Code Playgroud)

当我运行此脚本时,出现以下错误:

导入错误:无法导入名称“speech_v1p1beta1”

对于如何解决这个问题,有任何的建议吗?

小智 8

尝试运行 $ pip install --upgrade google-cloud-speech以更新您的依赖项。