1
votes

My code is pretty simple. It runs long_running_recognize on a single channel FLAC audio file and stores the result using the Google Cloud Speech API. I'm trying to find a way to get the current progress of the long_running_recognize operation. I found some documentation on the topic but I'm having trouble making any sense of it.

client = speech.SpeechClient()
operation = client.long_running_recognize(
  audio = speech.types.RecognitionAudio(
    uri = str('gs://speech-clips/'+self.audio_fqid),
  ),
  config = speech.types.RecognitionConfig(
    encoding = enums.RecognitionConfig.AudioEncoding.FLAC,
    sample_rate_hertz = sample_rate,
    enable_word_time_offsets = True,
    language_code = 'en-US',
  ),
)
response = operation.result()

Here is some of the documentation I found:

Any help will be greatly appreciated.

1

1 Answers

0
votes

Because long audio recognition is a long process, the API will give you an token which is the name in the operation response, once the process finished the done in operation will turn to true. I used RESTful API to do the request, hopefully it could work for you:

Here is the pseudo code:

    import googleapiclient.discovery
    import time
    service = googleapiclient.discovery.build('speech', 'v1')
    service_request = service.speech().longrunningrecognize(
        body= {
            "config": {
                "encoding": "FLAC",
                "languageCode": "en-US",
                "enableWordTimeOffsets": True
            },
            "audio": {
                "uri": str('gs://speech-clips/'+self.audio_fqid)
            }
        }
    )
    operation = service_request.execute()

    name = operation['name']
    service_request = service.operations().get(name=name)
    while True:

        # Give the server a few seconds to process.

        print('Waiting for server processing...')

        time.sleep(1)

        # Get the long running operation with response.

        response = service_request.execute()

        if 'done' in response and response['done']:
            break
    return response