Audio 使用Google语音时从Google云存储访问音频文件
我使用下面的代码成功地使用Google speech将包含语音的.wav文件解析为文本 但是我想访问一个不同的.wav文件,我已经将它放在谷歌云存储(公开)上,而不是本地硬盘上。为什么不简单地改变Audio 使用Google语音时从Google云存储访问音频文件,audio,google-cloud-storage,google-speech-api,Audio,Google Cloud Storage,Google Speech Api,我使用下面的代码成功地使用Google speech将包含语音的.wav文件解析为文本 但是我想访问一个不同的.wav文件,我已经将它放在谷歌云存储(公开)上,而不是本地硬盘上。为什么不简单地改变 speech\u文件='my/local/system/sample.wav' 到 语音文件https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav“ 工作可以接受吗 这是我的密码: speech_fil
speech\u文件='my/local/system/sample.wav'
到语音文件https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav“ 工作可以接受吗 这是我的密码:
speech_file = 'https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav'
DISCOVERY_URL = ('https://{api}.googleapis.com/$discovery/rest?'
'version={apiVersion}')
def get_speech_service():
credentials = GoogleCredentials.get_application_default().create_scoped(
['https://www.googleapis.com/auth/cloud-platform'])
http = htt|plib2.Http()
credentials.authorize(http)
return discovery.build(
'speech', 'v1beta1', http=http, discoveryServiceUrl=DISCOVERY_URL)
def main(speech_file):
"""Transcribe the given audio file.
Args:
speech_file: the name of the audio file.
"""
with open(speech_file, 'rb') as speech:
speech_content = base64.b64encode(speech.read())
service = get_speech_service()
service_request = service.speech().syncrecognize(
body={
'config': {
'encoding': 'LINEAR16', # raw 16-bit signed LE samples
'sampleRate': 44100, # 16 khz
'languageCode': 'en-US', # a BCP-47 language tag
},
'audio': {
'content': speech_content.decode('UTF-8')
}
})
response = service_request.execute()
return response
我不确定你的方法为什么不起作用,但我想提供一个快速的建议 谷歌云语音API本机支持谷歌云存储对象。与其下载整个对象只是为了将其上传回Cloud Speech API,只需通过交换以下行来指定对象:
'audio': {
# Remove this: 'content': speech_content.decode('UTF-8')
'uri': 'gs://speech_proj_files/sample.wav' # Do this!
}
还有一个建议。您可能会发现Python库更易于使用。试试这个:
from google.cloud import speech
speech_client = speech.Client()
audio_sample = speech_client.sample(
content=None,
source_uri='gs://speech_proj_files/sample.wav',
encoding='LINEAR16',
sample_rate_hertz= 44100)
results_list = audio_sample.sync_recognize(language_code='en-US')
这里有一些很好的例子:第二块代码似乎工作得很好,我只是在访问
结果\u列表中存储的任何内容时遇到了问题。它是一个列表以外的对象,这是肯定的…看起来不像json…它是什么,我如何打破它<代码>结果列表。响应
为空。也许它实际上不起作用。我一整天都在搞砸这件事。我最后问了另一个问题。再次感谢你在这方面的帮助。这意味着很多。@BrandonYarbrough我想Monica想知道的是如何在不公开对象的情况下访问她桶中的对象。