首页 > 解决方案 > 你如何从谷歌云存储桶中获取字节对象

问题描述

我在 Github https://github.com/googleapis/python-speech/issues/52上的问题 已经活跃了 9 天,仅有的两个尝试回答的人都失败了,但现在我认为有人可能会这样做回答这个问题,即使他们不了解 Google 的 Speech Api 的工作原理,谁也了解 Google Cloud Buckets 的工作原理。为了将长音频文件转换为文本,首先必须将它们上传到云端。我使用的一些语法现在似乎被破坏了,以下语法可能有效,只是谷歌没有解释如何使用此代码与上传到云的文件协调。因此,在下面发布的代码中:

https://cloud.google.com/speech-to-text/docs/async-recognize#speech_transcribe_async-python

content对象必须位于云上,并且必须是字节对象。假设对象的地址是:gs://audio_files/cool_audio

我将使用什么语法使内容对象引用字节对象?

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
client = speech.SpeechClient()

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

operation = client.long_running_recognize(config, audio)

print('Waiting for operation to complete...')
response = operation.result(timeout=90)

标签: google-cloud-storagegoogle-speech-to-text-api

解决方案


我之前的回答并没有真正解决你的问题。让我再尝试一次:

请试试这个:

audio = types.RecognitionAudio(content=bytes(content, 'utf-8'))

推荐阅读