From 1f8889bdd8499ffe1c8fd2a7ccf26713a7c8019d Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 6 Feb 2019 12:55:38 -0800 Subject: [PATCH 1/5] add speech api multichannel samples --- .../cloud-client/transcribe_multichannel.py | 97 +++++++++++++++++++ .../transcribe_multichannel_test.py | 38 ++++++++ 2 files changed, 135 insertions(+) create mode 100644 speech/cloud-client/transcribe_multichannel.py create mode 100644 speech/cloud-client/transcribe_multichannel_test.py diff --git a/speech/cloud-client/transcribe_multichannel.py b/speech/cloud-client/transcribe_multichannel.py new file mode 100644 index 000000000000..61ae26261510 --- /dev/null +++ b/speech/cloud-client/transcribe_multichannel.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python + +# Copyright 2018 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Google Cloud Speech API sample that demonstrates enhanced models +and recognition metadata. + +Example usage: + python transcribe_multichannel.py resources/audio.raw + python transcribe_multichannel.py \ + gs://cloud-samples-tests/speech/Google_Gnome.wav +""" + +import argparse +import io + + +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 + 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] + + +def transcribe_gcs_with_multichannel(gcs_uri): + """Transcribe the given audio file on GCS with + multi channel.""" + # [START speech_transcribe_multichannel_gcs] + from google.cloud import speech + client = speech.SpeechClient() + + audio = speech.types.RecognitionAudio(uri=gcs_uri) + + 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_gcs] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument( + 'path', help='File or GCS path for audio file to be recognized') + args = parser.parse_args() + if args.path.startswith('gs://'): + transcribe_gcs_with_multichannel(args.path) + else: + transcribe_file_with_multichannel(args.path) diff --git a/speech/cloud-client/transcribe_multichannel_test.py b/speech/cloud-client/transcribe_multichannel_test.py new file mode 100644 index 000000000000..c4106756a1c2 --- /dev/null +++ b/speech/cloud-client/transcribe_multichannel_test.py @@ -0,0 +1,38 @@ +# Copyright 2018, Google, Inc. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +from transcribe_multichannel import ( + transcribe_file_with_multichannel, + transcribe_gcs_with_multichannel) + +RESOURCES = os.path.join(os.path.dirname(__file__), 'resources') + + +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 + + +def test_transcribe_multichannel_gcs(capsys): + transcribe_gcs_with_multichannel( + 'gs://cloud-samples-tests/speech/Google_Gnome.wav') + out, err = capsys.readouterr() + + assert 'OK Google stream stranger things from Netflix to my TV' in out + + From 941d86e5caff0306352bef722b20dca07ae3b1d0 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Wed, 6 Feb 2019 12:56:54 -0800 Subject: [PATCH 2/5] udpate copyright year --- speech/cloud-client/transcribe_multichannel.py | 2 +- speech/cloud-client/transcribe_multichannel_test.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/speech/cloud-client/transcribe_multichannel.py b/speech/cloud-client/transcribe_multichannel.py index 61ae26261510..a4620703145c 100644 --- a/speech/cloud-client/transcribe_multichannel.py +++ b/speech/cloud-client/transcribe_multichannel.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -# Copyright 2018 Google Inc. All Rights Reserved. +# Copyright 2019 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/speech/cloud-client/transcribe_multichannel_test.py b/speech/cloud-client/transcribe_multichannel_test.py index c4106756a1c2..8c13250d3a82 100644 --- a/speech/cloud-client/transcribe_multichannel_test.py +++ b/speech/cloud-client/transcribe_multichannel_test.py @@ -1,4 +1,4 @@ -# Copyright 2018, Google, Inc. +# Copyright 2019, Google, Inc. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at From 12fec5730ff1e5d0ea6fdaccb794ce94388c11be Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 11 Feb 2019 11:19:30 -0800 Subject: [PATCH 3/5] test with multichannel audio data --- speech/cloud-client/transcribe_multichannel.py | 12 ++++++------ speech/cloud-client/transcribe_multichannel_test.py | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/speech/cloud-client/transcribe_multichannel.py b/speech/cloud-client/transcribe_multichannel.py index a4620703145c..90a607a0cce8 100644 --- a/speech/cloud-client/transcribe_multichannel.py +++ b/speech/cloud-client/transcribe_multichannel.py @@ -18,9 +18,9 @@ and recognition metadata. Example usage: - python transcribe_multichannel.py resources/audio.raw + python transcribe_multichannel.py resources/multi.wav python transcribe_multichannel.py \ - gs://cloud-samples-tests/speech/Google_Gnome.wav + gs://cloud-samples-tests/speech/multi.wav """ import argparse @@ -41,9 +41,9 @@ def transcribe_file_with_multichannel(speech_file): config = speech.types.RecognitionConfig( encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, - sample_rate_hertz=16000, + sample_rate_hertz=44100, language_code='en-US', - audio_channel_count=1, + audio_channel_count=2, enable_separate_recognition_per_channel=True) response = client.recognize(config, audio) @@ -68,9 +68,9 @@ def transcribe_gcs_with_multichannel(gcs_uri): config = speech.types.RecognitionConfig( encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16, - sample_rate_hertz=16000, + sample_rate_hertz=44100, language_code='en-US', - audio_channel_count=1, + audio_channel_count=2, enable_separate_recognition_per_channel=True) response = client.recognize(config, audio) diff --git a/speech/cloud-client/transcribe_multichannel_test.py b/speech/cloud-client/transcribe_multichannel_test.py index 8c13250d3a82..ebf2eb98ff70 100644 --- a/speech/cloud-client/transcribe_multichannel_test.py +++ b/speech/cloud-client/transcribe_multichannel_test.py @@ -22,17 +22,17 @@ def test_transcribe_multichannel_file(capsys): transcribe_file_with_multichannel( - os.path.join(RESOURCES, 'Google_Gnome.wav')) + os.path.join(RESOURCES, 'multi.wav')) out, err = capsys.readouterr() - assert 'OK Google stream stranger things from Netflix to my TV' in out + assert 'how are you doing' in out def test_transcribe_multichannel_gcs(capsys): transcribe_gcs_with_multichannel( - 'gs://cloud-samples-tests/speech/Google_Gnome.wav') + 'gs://cloud-samples-data/speech/multi.wav') out, err = capsys.readouterr() - assert 'OK Google stream stranger things from Netflix to my TV' in out + assert 'how are you doing' in out From e1219e175fd80ea3a82ae4720d42af1ae265bb83 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Mon, 11 Feb 2019 12:09:18 -0800 Subject: [PATCH 4/5] flake --- speech/cloud-client/transcribe_enhanced_model.py | 7 +++---- speech/cloud-client/transcribe_multichannel.py | 1 - speech/cloud-client/transcribe_multichannel_test.py | 2 -- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/speech/cloud-client/transcribe_enhanced_model.py b/speech/cloud-client/transcribe_enhanced_model.py index 514d54812d03..1b233c52696c 100644 --- a/speech/cloud-client/transcribe_enhanced_model.py +++ b/speech/cloud-client/transcribe_enhanced_model.py @@ -22,16 +22,15 @@ """ import argparse -import io def transcribe_file_with_enhanced_model(path): """Transcribe the given audio file using an enhanced model.""" # [START speech_transcribe_enhanced_model] - import io - + import io + from google.cloud import speech - + client = speech.SpeechClient() # path = 'resources/commercial_mono.wav' diff --git a/speech/cloud-client/transcribe_multichannel.py b/speech/cloud-client/transcribe_multichannel.py index 90a607a0cce8..b603822053fd 100644 --- a/speech/cloud-client/transcribe_multichannel.py +++ b/speech/cloud-client/transcribe_multichannel.py @@ -24,7 +24,6 @@ """ import argparse -import io def transcribe_file_with_multichannel(speech_file): diff --git a/speech/cloud-client/transcribe_multichannel_test.py b/speech/cloud-client/transcribe_multichannel_test.py index ebf2eb98ff70..de9558629994 100644 --- a/speech/cloud-client/transcribe_multichannel_test.py +++ b/speech/cloud-client/transcribe_multichannel_test.py @@ -34,5 +34,3 @@ def test_transcribe_multichannel_gcs(capsys): out, err = capsys.readouterr() assert 'how are you doing' in out - - From 81d49f526d8bd86364fb7166e9602e1054c908f2 Mon Sep 17 00:00:00 2001 From: Yu-Han Liu Date: Fri, 22 Feb 2019 07:14:34 -0800 Subject: [PATCH 5/5] update comment --- speech/cloud-client/transcribe_multichannel.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/speech/cloud-client/transcribe_multichannel.py b/speech/cloud-client/transcribe_multichannel.py index b603822053fd..e84da59ad7b3 100644 --- a/speech/cloud-client/transcribe_multichannel.py +++ b/speech/cloud-client/transcribe_multichannel.py @@ -14,8 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Speech API sample that demonstrates enhanced models -and recognition metadata. +"""Google Cloud Speech API sample that demonstrates multichannel recognition. Example usage: python transcribe_multichannel.py resources/multi.wav