Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion speech/cloud-client/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
41 changes: 41 additions & 0 deletions speech/cloud-client/beta_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -205,13 +206,51 @@ 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__,
formatter_class=argparse.RawDescriptionHelpFormatter)
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()

Expand All @@ -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)
11 changes: 10 additions & 1 deletion speech/cloud-client/beta_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Binary file added speech/cloud-client/resources/multi.wav
Binary file not shown.