Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Added the multi lingual audio file

* Lint issues corrected.
  • Loading branch information
happyhuman authored and busunkim96 committed Sep 3, 2020
1 parent e165952 commit 1e00bf8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
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
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)
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 not shown.

0 comments on commit 1e00bf8

Please sign in to comment.