【发布时间】:2017-04-21 01:06:38
【问题描述】:
我已使用下面的这段代码成功地使用 Google Speech 将包含语音的 .wav 文件解析为文本。
但我想访问一个不同的 .wav 文件,该文件已(公开)放置在 Google Cloud Storage 上,而不是本地硬盘上。为什么不简单地改变
speech_file = 'my/local/system/sample.wav'
到speech_file = '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
【问题讨论】:
标签: audio google-cloud-storage google-speech-api