diff --git a/speech/cloud-client/README.rst b/speech/cloud-client/README.rst index 70659594d75..01e73fb54c9 100644 --- a/speech/cloud-client/README.rst +++ b/speech/cloud-client/README.rst @@ -221,7 +221,7 @@ To run this sample: $ python beta_snippets.py - usage: beta_snippets.py [-h] command path + usage: beta_snippets.py [-h] command path first second Google Cloud Speech API sample that demonstrates enhanced models and recognition metadata. @@ -232,10 +232,13 @@ To run this sample: 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 + python beta_snippets.py multi-language resources/multi.wav en-US es positional arguments: command path File for audio file to be recognized + first First language in audio file to be recognized + second Second language in audio file to be recognized optional arguments: -h, --help show this help message and exit diff --git a/speech/cloud-client/beta_snippets.py b/speech/cloud-client/beta_snippets.py index e2702bbd3c4..87a6cac822b 100644 --- a/speech/cloud-client/beta_snippets.py +++ b/speech/cloud-client/beta_snippets.py @@ -23,6 +23,7 @@ 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 + python beta_snippets.py multi-language resources/multi.wav en-US es """ import argparse @@ -205,6 +206,40 @@ def transcribe_file_with_multichannel(speech_file): # [END speech_transcribe_multichannel] +def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang): + """Transcribe the given audio file synchronously with + multi language.""" + # [START speech_transcribe_multilanguage] + 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' + # first_lang = first language code, e,g, 'en-US' + # second_lang = first language code, e,g, 'es' + + 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, + audio_channel_count=2, + language_code=first_lang, + alternative_language_codes=[second_lang]) + + print('Waiting for operation to complete...') + 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, alternative)) + print(u'Transcript: {}'.format(alternative.transcript)) + # [END speech_transcribe_multilanguage] + + if __name__ == '__main__': parser = argparse.ArgumentParser( description=__doc__, @@ -212,6 +247,10 @@ def transcribe_file_with_multichannel(speech_file): parser.add_argument('command') parser.add_argument( 'path', help='File for audio file to be recognized') + parser.add_argument( + 'first', help='First language in audio file to be recognized') + parser.add_argument( + 'second', help='Second language in audio file to be recognized') args = parser.parse_args() @@ -225,3 +264,5 @@ def transcribe_file_with_multichannel(speech_file): transcribe_file_with_diarization(args.path) elif args.command == 'multi-channel': transcribe_file_with_multichannel(args.path) + elif args.command == 'multi-language': + transcribe_file_with_multilanguage(args.path, args.first, args.second) diff --git a/speech/cloud-client/beta_snippets_test.py b/speech/cloud-client/beta_snippets_test.py index 92cb602384b..10f0f4dba36 100644 --- a/speech/cloud-client/beta_snippets_test.py +++ b/speech/cloud-client/beta_snippets_test.py @@ -18,7 +18,8 @@ transcribe_file_with_diarization, transcribe_file_with_enhanced_model, transcribe_file_with_metadata, - transcribe_file_with_multichannel) + transcribe_file_with_multichannel, + transcribe_file_with_multilanguage) RESOURCES = os.path.join(os.path.dirname(__file__), 'resources') @@ -61,3 +62,11 @@ def test_transcribe_multichannel_file(capsys): out, err = capsys.readouterr() assert 'OK Google stream stranger things from Netflix to my TV' in out + + +def test_transcribe_multilanguage_file(capsys): + transcribe_file_with_multilanguage( + os.path.join(RESOURCES, 'multi.wav'), 'en-US', 'es') + out, err = capsys.readouterr() + + assert 'how are you doing estoy bien e tu' in out diff --git a/speech/cloud-client/resources/multi.wav b/speech/cloud-client/resources/multi.wav new file mode 100644 index 00000000000..7f71d74b951 Binary files /dev/null and b/speech/cloud-client/resources/multi.wav differ