|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Cloud Speech API sample application using the REST API for async |
| 18 | +batch processing. |
| 19 | +
|
| 20 | +Example usage: python transcribe_async.py resources/audio.raw |
| 21 | +""" |
| 22 | + |
| 23 | +# [START import_libraries] |
| 24 | +import argparse |
| 25 | +import io |
| 26 | +import time |
| 27 | +# [END import_libraries] |
| 28 | + |
| 29 | + |
| 30 | +def main(speech_file): |
| 31 | + """Transcribe the given audio file asynchronously. |
| 32 | +
|
| 33 | + Args: |
| 34 | + speech_file: the name of the audio file. |
| 35 | + """ |
| 36 | + # [START authenticating] |
| 37 | + # Application default credentials provided by env variable |
| 38 | + # GOOGLE_APPLICATION_CREDENTIALS |
| 39 | + from google.cloud import speech |
| 40 | + speech_client = speech.Client() |
| 41 | + # [END authenticating] |
| 42 | + |
| 43 | + # [START construct_request] |
| 44 | + # Loads the audio into memory |
| 45 | + with io.open(speech_file, 'rb') as audio_file: |
| 46 | + content = audio_file.read() |
| 47 | + audio_sample = speech_client.sample( |
| 48 | + content, |
| 49 | + source_uri=None, |
| 50 | + encoding='LINEAR16', |
| 51 | + sample_rate=16000) |
| 52 | + # [END construct_request] |
| 53 | + |
| 54 | + # [START send_request] |
| 55 | + operation = speech_client.speech_api.async_recognize(audio_sample) |
| 56 | + |
| 57 | + retry_count = 100 |
| 58 | + while retry_count > 0 and not operation.complete: |
| 59 | + retry_count -= 1 |
| 60 | + time.sleep(2) |
| 61 | + operation.poll() |
| 62 | + |
| 63 | + if not operation.complete: |
| 64 | + print("Operation not complete and retry limit reached.") |
| 65 | + return |
| 66 | + |
| 67 | + alternatives = operation.results |
| 68 | + for alternative in alternatives: |
| 69 | + print('Transcript: {}'.format(alternative.transcript)) |
| 70 | + print('Confidence: {}'.format(alternative.confidence)) |
| 71 | + # [END send_request] |
| 72 | + |
| 73 | + |
| 74 | +# [START run_application] |
| 75 | +if __name__ == '__main__': |
| 76 | + parser = argparse.ArgumentParser( |
| 77 | + description=__doc__, |
| 78 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 79 | + parser.add_argument( |
| 80 | + 'speech_file', help='Full path of audio file to be recognized') |
| 81 | + args = parser.parse_args() |
| 82 | + main(args.speech_file) |
| 83 | + # [END run_application] |
0 commit comments