I have used this bit of code below to successfully parse a .wav file which contains speech, to text, using Google Speech.
But I want to access a different .wav file, which I have placed on Google Cloud Storage (publicly), instead of on my local hard drive. Why doesn't simply changing
speech_file = 'my/local/system/sample.wav'
to
speech_file = 'https://console.cloud.google.com/storage/browser/speech_proj_files/sample.wav'
work acceptably?
Here is my code:
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
I'm not sure why your approach isn't working, but I want to offer a quick suggestion.
Google Cloud Speech API natively supports Google Cloud Storage objects. Instead of downloading the whole object only to upload it back to the Cloud Speech API, just specify the object by swapping out this line:
One other suggestion. You may find the google-cloud Python library easier to use. Try this:
There are some great examples here: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client