Sienna
Sienna

Reputation: 1699

AttributeError on transcript results from Google Speech API

  1. OS type and version: Windows 10, build 16199.1000

  2. Python version and virtual environment information python --version: Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32

  3. google-cloud-python version: google-cloud-speech==0.27.0

Stack Trace:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python27\Lib\threading.py", line 801, in __bootstrap_inner
    self.run()
  File "C:\Python27\Lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "E:/Programming/Python/untitled1/main.py", line 109, in get_transcript
    print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)
  File "E:/Programming/Python/untitled1/main.py", line 109, in <genexpr>
    print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)
AttributeError: 'SpeechRecognitionResult' object has no attribute 'alternative'

Steps to reproduce:

When I use this:

alternatives = operation.result().results[0].alternatives
    for alternative in alternatives:
        print('Transcript: {}'.format(alternative.transcript))
        print('Confidence: {}'.format(alternative.confidence))

It works as intended, but only prints the first transcript. When I use this:

res = operation.result().results
print(res, file=sys.stderr)
print('. '.join(resp.alternative.transcript for resp in res), file=sys.stderr)

I get the exception above. I have also tried print('. '.join(resp.transcript for resp in res), file=sys.stderr) and print('. '.join(resp.alternative for resp in res), file=sys.stderr), just as print debugging. Both throw an AttributeError on either attribute.

Complete working example: https://gist.github.com/mxplusb/8f487a6ff3c781689799bb7ce1dec3f3. It removes the audio from a video file using ffmpeg in the proper format, uploads it to GCS, and then performs an asynchronous speech-to-text recognition. I am trying to concatenate all the transcripts into one large text string.

Upvotes: 1

Views: 1119

Answers (1)

Ziyad Edher
Ziyad Edher

Reputation: 2150

I think you have a slight typo as according to the official documentation the field is alternatives not alternative.

The alternatives attribute is an array containing SpeechRecognitionAlternative objects, each with their own transcript, in your example you are iterating through the results, but not through each alternative; instead you are assuming only one alternative and I think that is why you chose to type out alternative instead of alternatives and iterating correctly throughout.

To fix this just change your resp.alternative to resp.alternatives and iterate correctly through every alternative printing out its transcript.

Upvotes: 1

Related Questions