From bbb8a5cc7f96dde4716f1efcdfaa6052813835d9 Mon Sep 17 00:00:00 2001 From: happyhuman Date: Thu, 12 Jul 2018 14:51:10 -0700 Subject: [PATCH 1/5] Implemented the multi channel sample --- speech/cloud-client/README.rst | 1 + speech/cloud-client/beta_snippets.py | 32 +++++++++++++++++++++++ speech/cloud-client/beta_snippets_test.py | 11 +++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/speech/cloud-client/README.rst b/speech/cloud-client/README.rst index 8efaa01472b..70659594d75 100644 --- a/speech/cloud-client/README.rst +++ b/speech/cloud-client/README.rst @@ -231,6 +231,7 @@ To run this sample: python beta_snippets.py metadata resources/commercial_mono.wav python beta_snippets.py punctuation resources/commercial_mono.wav python beta_snippets.py diarization resources/commercial_mono.wav + python beta_snippets.py multi-channel resources/commercial_mono.wav positional arguments: command diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index a518307f787..f1783df2787 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -22,6 +22,7 @@ python beta_snippets.py metadata resources/commercial_mono.wav python beta_snippets.py punctuation resources/commercial_mono.wav python beta_snippets.py diarization resources/commercial_mono.wav + python beta_snippets.py multi-channel resources/commercial_mono.wav """ import argparse @@ -157,6 +158,35 @@ def transcribe_file_with_diarization(path): # [END speech_transcribe_diarization] +# [START speech_transcribe_multichannel] +def transcribe_file_with_multichannel(speech_file): + """Transcribe the given audio file synchronously with + multi channel.""" + client = speech.SpeechClient() + + with open(speech_file, 'rb') as audio_file: + content = audio_file.read() + + audio = speech.types.RecognitionAudio(content=content) + + config = speech.types.RecognitionConfig( + encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, + sample_rate_hertz=16000, + language_code='en-US', + audio_channel_count=1, + enable_separate_recognition_per_channel=True) + + response = client.recognize(config, audio) + + for i, result in enumerate(response.results): + alternative = result.alternatives[0] + print('-' * 20) + print('First alternative of result {}'.format(i)) + print(u'Transcript: {}'.format(alternative.transcript)) + print(u'Channel Tag: {}'.format(result.channel_tag)) +# [END speech_transcribe_multichannel] + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, @@ -175,3 +205,5 @@ def transcribe_file_with_diarization(path): transcribe_file_with_auto_punctuation(args.path) elif args.command == 'diarization': transcribe_file_with_diarization(args.path) + elif args.command == 'multi-channel': + transcribe_file_with_multichannel(args.path) diff --git a/speech/cloud-client/beta_snippets_test.py b/speech/cloud-client/beta_snippets_test.py index ef78f941d67..92cb602384b 100644 --- a/speech/cloud-client/beta_snippets_test.py +++ b/speech/cloud-client/beta_snippets_test.py @@ -17,7 +17,8 @@ transcribe_file_with_auto_punctuation, transcribe_file_with_diarization, transcribe_file_with_enhanced_model, - transcribe_file_with_metadata) + transcribe_file_with_metadata, + transcribe_file_with_multichannel) RESOURCES = os.path.join(os.path.dirname(__file__), 'resources') @@ -52,3 +53,11 @@ def test_transcribe_diarization(capsys): out, err = capsys.readouterr() assert 'OK Google stream stranger things from Netflix to my TV' in out + + +def test_transcribe_multichannel_file(capsys): + transcribe_file_with_multichannel( + os.path.join(RESOURCES, 'Google_Gnome.wav')) + out, err = capsys.readouterr() + + assert 'OK Google stream stranger things from Netflix to my TV' in out From a8021d4247f13c629db38b330cb4d2a98971fd7b Mon Sep 17 00:00:00 2001 From: happyhuman Date: Fri, 13 Jul 2018 14:05:47 -0700 Subject: [PATCH 2/5] Added parameter comment --- speech/cloud-client/beta_snippets.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index f1783df2787..094141525ce 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -133,6 +133,9 @@ def transcribe_file_with_diarization(path): """Transcribe the given audio file synchronously with diarization.""" client = speech.SpeechClient() + # TODO(developer): Uncomment and set to a path to your audio file. + # speech_file = 'path/to/file.wav' + with open(path, 'rb') as audio_file: content = audio_file.read() @@ -164,6 +167,9 @@ def transcribe_file_with_multichannel(speech_file): multi channel.""" client = speech.SpeechClient() + # TODO(developer): Uncomment and set to a path to your audio file. + # speech_file = 'path/to/file.wav' + with open(speech_file, 'rb') as audio_file: content = audio_file.read() From bc7272fb27cb11f286c92fd2735a94951163551e Mon Sep 17 00:00:00 2001 From: happyhuman Date: Fri, 13 Jul 2018 15:27:01 -0700 Subject: [PATCH 3/5] Moved region tags inside the functions --- speech/cloud-client/beta_snippets.py | 37 +++++++++++++++++++--------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index 094141525ce..248d705e8dc 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -28,14 +28,16 @@ import argparse import io -from google.cloud import speech_v1p1beta1 as speech - -# [START speech_transcribe_file_with_enhanced_model] def transcribe_file_with_enhanced_model(path): """Transcribe the given audio file using an enhanced model.""" + # [START speech_transcribe_file_with_enhanced_model] + from google.cloud import speech_v1p1beta1 as speech client = speech.SpeechClient() + # TODO(developer): Uncomment and set to a path to your audio file. + # speech_file = 'path/to/file.wav' + with io.open(path, 'rb') as audio_file: content = audio_file.read() @@ -57,14 +59,18 @@ def transcribe_file_with_enhanced_model(path): print('-' * 20) print('First alternative of result {}'.format(i)) print('Transcript: {}'.format(alternative.transcript)) -# [END speech_transcribe_file_with_enhanced_model] + # [END speech_transcribe_file_with_enhanced_model] -# [START speech_transcribe_file_with_metadata] def transcribe_file_with_metadata(path): """Send a request that includes recognition metadata.""" + # [START speech_transcribe_file_with_metadata] + from google.cloud import speech_v1p1beta1 as speech client = speech.SpeechClient() + # TODO(developer): Uncomment and set to a path to your audio file. + # speech_file = 'path/to/file.wav' + with io.open(path, 'rb') as audio_file: content = audio_file.read() @@ -99,14 +105,19 @@ def transcribe_file_with_metadata(path): print('-' * 20) print('First alternative of result {}'.format(i)) print('Transcript: {}'.format(alternative.transcript)) -# [END speech_transcribe_file_with_metadata] + # [END speech_transcribe_file_with_metadata] + -# [START speech_transcribe_file_with_auto_punctuation] def transcribe_file_with_auto_punctuation(path): """Transcribe the given audio file with auto punctuation enabled.""" + # [START speech_transcribe_file_with_auto_punctuation] + from google.cloud import speech_v1p1beta1 as speech client = speech.SpeechClient() + # TODO(developer): Uncomment and set to a path to your audio file. + # speech_file = 'path/to/file.wav' + with io.open(path, 'rb') as audio_file: content = audio_file.read() @@ -125,12 +136,13 @@ def transcribe_file_with_auto_punctuation(path): print('-' * 20) print('First alternative of result {}'.format(i)) print('Transcript: {}'.format(alternative.transcript)) -# [END speech_transcribe_file_with_auto_punctuation] + # [END speech_transcribe_file_with_auto_punctuation] -# [START speech_transcribe_diarization] def transcribe_file_with_diarization(path): """Transcribe the given audio file synchronously with diarization.""" + # [START speech_transcribe_diarization] + from google.cloud import speech_v1p1beta1 as speech client = speech.SpeechClient() # TODO(developer): Uncomment and set to a path to your audio file. @@ -158,13 +170,14 @@ def transcribe_file_with_diarization(path): .format(i, alternative.transcript)) print('Speaker Tag for the first word: {}' .format(alternative.words[0].speaker_tag)) -# [END speech_transcribe_diarization] + # [END speech_transcribe_diarization] -# [START speech_transcribe_multichannel] def transcribe_file_with_multichannel(speech_file): """Transcribe the given audio file synchronously with multi channel.""" + # [START speech_transcribe_multichannel] + from google.cloud import speech_v1p1beta1 as speech client = speech.SpeechClient() # TODO(developer): Uncomment and set to a path to your audio file. @@ -190,7 +203,7 @@ def transcribe_file_with_multichannel(speech_file): print('First alternative of result {}'.format(i)) print(u'Transcript: {}'.format(alternative.transcript)) print(u'Channel Tag: {}'.format(result.channel_tag)) -# [END speech_transcribe_multichannel] + # [END speech_transcribe_multichannel] if __name__ == '__main__': From 02d13edad365d098d2f89852660dca7b0a3e81a8 Mon Sep 17 00:00:00 2001 From: happyhuman Date: Fri, 13 Jul 2018 15:35:24 -0700 Subject: [PATCH 4/5] Deleted the extra line --- speech/cloud-client/beta_snippets.py | 1 - 1 file changed, 1 deletion(-) diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index 248d705e8dc..8988e27aaf8 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -108,7 +108,6 @@ def transcribe_file_with_metadata(path): # [END speech_transcribe_file_with_metadata] - def transcribe_file_with_auto_punctuation(path): """Transcribe the given audio file with auto punctuation enabled.""" # [START speech_transcribe_file_with_auto_punctuation] From b165d37c0db0db09b63a1332800b26b614fd810b Mon Sep 17 00:00:00 2001 From: happyhuman Date: Fri, 13 Jul 2018 15:47:39 -0700 Subject: [PATCH 5/5] Fixing typos --- speech/cloud-client/beta_snippets.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index 8988e27aaf8..e2702bbd3c4 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -29,7 +29,7 @@ import io -def transcribe_file_with_enhanced_model(path): +def transcribe_file_with_enhanced_model(speech_file): """Transcribe the given audio file using an enhanced model.""" # [START speech_transcribe_file_with_enhanced_model] from google.cloud import speech_v1p1beta1 as speech @@ -38,7 +38,7 @@ def transcribe_file_with_enhanced_model(path): # TODO(developer): Uncomment and set to a path to your audio file. # speech_file = 'path/to/file.wav' - with io.open(path, 'rb') as audio_file: + with io.open(speech_file, 'rb') as audio_file: content = audio_file.read() audio = speech.types.RecognitionAudio(content=content) @@ -62,7 +62,7 @@ def transcribe_file_with_enhanced_model(path): # [END speech_transcribe_file_with_enhanced_model] -def transcribe_file_with_metadata(path): +def transcribe_file_with_metadata(speech_file): """Send a request that includes recognition metadata.""" # [START speech_transcribe_file_with_metadata] from google.cloud import speech_v1p1beta1 as speech @@ -71,7 +71,7 @@ def transcribe_file_with_metadata(path): # TODO(developer): Uncomment and set to a path to your audio file. # speech_file = 'path/to/file.wav' - with io.open(path, 'rb') as audio_file: + with io.open(speech_file, 'rb') as audio_file: content = audio_file.read() # Here we construct a recognition metadata object. @@ -108,7 +108,7 @@ def transcribe_file_with_metadata(path): # [END speech_transcribe_file_with_metadata] -def transcribe_file_with_auto_punctuation(path): +def transcribe_file_with_auto_punctuation(speech_file): """Transcribe the given audio file with auto punctuation enabled.""" # [START speech_transcribe_file_with_auto_punctuation] from google.cloud import speech_v1p1beta1 as speech @@ -117,7 +117,7 @@ def transcribe_file_with_auto_punctuation(path): # TODO(developer): Uncomment and set to a path to your audio file. # speech_file = 'path/to/file.wav' - with io.open(path, 'rb') as audio_file: + with io.open(speech_file, 'rb') as audio_file: content = audio_file.read() audio = speech.types.RecognitionAudio(content=content) @@ -138,7 +138,7 @@ def transcribe_file_with_auto_punctuation(path): # [END speech_transcribe_file_with_auto_punctuation] -def transcribe_file_with_diarization(path): +def transcribe_file_with_diarization(speech_file): """Transcribe the given audio file synchronously with diarization.""" # [START speech_transcribe_diarization] from google.cloud import speech_v1p1beta1 as speech @@ -147,7 +147,7 @@ def transcribe_file_with_diarization(path): # TODO(developer): Uncomment and set to a path to your audio file. # speech_file = 'path/to/file.wav' - with open(path, 'rb') as audio_file: + with open(speech_file, 'rb') as audio_file: content = audio_file.read() audio = speech.types.RecognitionAudio(content=content)