|
| 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 that demonstrates word time offsets. |
| 18 | +
|
| 19 | +Example usage: |
| 20 | + python transcribe_word_time_offsets.py resources/audio.raw |
| 21 | + python transcribe_word_time_offsets.py \ |
| 22 | + gs://cloud-samples-tests/speech/vr.flac |
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | +import io |
| 27 | + |
| 28 | + |
| 29 | +def transcribe_file_with_word_time_offsets(speech_file): |
| 30 | + """Transcribe the given audio file synchronously and output the word time |
| 31 | + offsets.""" |
| 32 | + from google.cloud import speech |
| 33 | + from google.cloud.speech import enums |
| 34 | + from google.cloud.speech import types |
| 35 | + client = speech.SpeechClient() |
| 36 | + |
| 37 | + with io.open(speech_file, 'rb') as audio_file: |
| 38 | + content = audio_file.read() |
| 39 | + |
| 40 | + audio = types.RecognitionAudio(content=content) |
| 41 | + config = types.RecognitionConfig( |
| 42 | + encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, |
| 43 | + sample_rate_hertz=16000, |
| 44 | + language_code='en-US', |
| 45 | + enable_word_time_offsets=True) |
| 46 | + |
| 47 | + response = client.recognize(config, audio) |
| 48 | + |
| 49 | + alternatives = response.results[0].alternatives |
| 50 | + |
| 51 | + for alternative in alternatives: |
| 52 | + print('Transcript: {}'.format(alternative.transcript)) |
| 53 | + |
| 54 | + for word_info in alternative.words: |
| 55 | + word = word_info.word |
| 56 | + start_time = word_info.start_time |
| 57 | + end_time = word_info.end_time |
| 58 | + print('Word: {}, start_time: {}, end_time: {}'.format( |
| 59 | + word, |
| 60 | + start_time.seconds + start_time.nanos * 1e-9, |
| 61 | + end_time.seconds + end_time.nanos * 1e-9)) |
| 62 | + |
| 63 | + |
| 64 | +# [START def_transcribe_gcs] |
| 65 | +def transcribe_gcs_with_word_time_offsets(gcs_uri): |
| 66 | + """Transcribe the given audio file asynchronously and output the word time |
| 67 | + offsets.""" |
| 68 | + from google.cloud import speech |
| 69 | + from google.cloud.speech import enums |
| 70 | + from google.cloud.speech import types |
| 71 | + client = speech.SpeechClient() |
| 72 | + |
| 73 | + audio = types.RecognitionAudio(uri=gcs_uri) |
| 74 | + config = types.RecognitionConfig( |
| 75 | + encoding=enums.RecognitionConfig.AudioEncoding.FLAC, |
| 76 | + sample_rate_hertz=16000, |
| 77 | + language_code='en-US', |
| 78 | + enable_word_time_offsets=True) |
| 79 | + |
| 80 | + operation = client.long_running_recognize(config, audio) |
| 81 | + |
| 82 | + print('Waiting for operation to complete...') |
| 83 | + result = operation.result(timeout=90) |
| 84 | + |
| 85 | + alternatives = result.results[0].alternatives |
| 86 | + for alternative in alternatives: |
| 87 | + print('Transcript: {}'.format(alternative.transcript)) |
| 88 | + print('Confidence: {}'.format(alternative.confidence)) |
| 89 | + |
| 90 | + for word_info in alternative.words: |
| 91 | + word = word_info.word |
| 92 | + start_time = word_info.start_time |
| 93 | + end_time = word_info.end_time |
| 94 | + print('Word: {}, start_time: {}, end_time: {}'.format( |
| 95 | + word, |
| 96 | + start_time.seconds + start_time.nanos * 1e-9, |
| 97 | + end_time.seconds + end_time.nanos * 1e-9)) |
| 98 | +# [END def_transcribe_gcs] |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + parser = argparse.ArgumentParser( |
| 103 | + description=__doc__, |
| 104 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 105 | + parser.add_argument( |
| 106 | + 'path', help='File or GCS path for audio file to be recognized') |
| 107 | + args = parser.parse_args() |
| 108 | + if args.path.startswith('gs://'): |
| 109 | + transcribe_gcs_with_word_time_offsets(args.path) |
| 110 | + else: |
| 111 | + transcribe_file_with_word_time_offsets(args.path) |
0 commit comments