I want to be able to run several concurrent long recognition jobs at once, so I wrote the following Python code.
client = speech.SpeechClient()
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
language_code='en-US')
audio = {"Brooklyn": types.RecognitionAudio(uri='gs://cloud-samples-tests/speech/brooklyn.flac')}
jobs = {}
output = {}
for name, job in audio.items():
jobs[name] = client.long_running_recognize(config, job)
while len(jobs) > 0:
time.sleep(5)
for name, job in jobs.items():
if job.done() == False:
print(name + ' progress: ' + str(job.metadata.progress_percent))
else:
print(name + ' is done!')
output[name] = job
jobs.pop(name)
for name, result in output.items():
print(u'Transcript: {}'.format(result.alternatives[0].transcript))
print('Confidence: {}'.format(result.alternatives[0].confidence))
It runs fine, but when trying to print the transcript result on the second to last line, I get the error AttributeError: 'Operation' object has no attribute 'alternatives'
. I think I'm missing something fairly obvious in the way the attributes are nested within the client object, but I just can't figure it out.
Realized I needed to use import pdb; pdb.set_trace()
in the else to figure out what Google was actually returning to me. Turns out I was right, there was some extra nesting I wasn't aware of. print(u'Transcript: {}'.format(result._result.results[0].alternatives[0].transcript))
did the trick.