Skip to content

Commit eae94ac

Browse files
happyhumanbusunkim96
authored andcommitted
* Added the multi lingual audio file * Lint issues corrected.
1 parent 9ee4a3e commit eae94ac

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

packages/google-cloud-speech/samples/snippets/README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ To run this sample:
221221
222222
$ python beta_snippets.py
223223
224-
usage: beta_snippets.py [-h] command path
224+
usage: beta_snippets.py [-h] command path first second
225225
226226
Google Cloud Speech API sample that demonstrates enhanced models
227227
and recognition metadata.
@@ -232,10 +232,13 @@ To run this sample:
232232
python beta_snippets.py punctuation resources/commercial_mono.wav
233233
python beta_snippets.py diarization resources/commercial_mono.wav
234234
python beta_snippets.py multi-channel resources/commercial_mono.wav
235+
python beta_snippets.py multi-language resources/multi.wav en-US es
235236
236237
positional arguments:
237238
command
238239
path File for audio file to be recognized
240+
first First language in audio file to be recognized
241+
second Second language in audio file to be recognized
239242
240243
optional arguments:
241244
-h, --help show this help message and exit

packages/google-cloud-speech/samples/snippets/beta_snippets.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
python beta_snippets.py punctuation resources/commercial_mono.wav
2424
python beta_snippets.py diarization resources/commercial_mono.wav
2525
python beta_snippets.py multi-channel resources/commercial_mono.wav
26+
python beta_snippets.py multi-language resources/multi.wav en-US es
2627
"""
2728

2829
import argparse
@@ -205,13 +206,51 @@ def transcribe_file_with_multichannel(speech_file):
205206
# [END speech_transcribe_multichannel]
206207

207208

209+
def transcribe_file_with_multilanguage(speech_file, first_lang, second_lang):
210+
"""Transcribe the given audio file synchronously with
211+
multi language."""
212+
# [START speech_transcribe_multilanguage]
213+
from google.cloud import speech_v1p1beta1 as speech
214+
client = speech.SpeechClient()
215+
216+
# TODO(developer): Uncomment and set to a path to your audio file.
217+
# speech_file = 'path/to/file.wav'
218+
# first_lang = first language code, e,g, 'en-US'
219+
# second_lang = first language code, e,g, 'es'
220+
221+
with open(speech_file, 'rb') as audio_file:
222+
content = audio_file.read()
223+
224+
audio = speech.types.RecognitionAudio(content=content)
225+
226+
config = speech.types.RecognitionConfig(
227+
encoding=speech.enums.RecognitionConfig.AudioEncoding.LINEAR16,
228+
audio_channel_count=2,
229+
language_code=first_lang,
230+
alternative_language_codes=[second_lang])
231+
232+
print('Waiting for operation to complete...')
233+
response = client.recognize(config, audio)
234+
235+
for i, result in enumerate(response.results):
236+
alternative = result.alternatives[0]
237+
print('-' * 20)
238+
print('First alternative of result {}: {}'.format(i, alternative))
239+
print(u'Transcript: {}'.format(alternative.transcript))
240+
# [END speech_transcribe_multilanguage]
241+
242+
208243
if __name__ == '__main__':
209244
parser = argparse.ArgumentParser(
210245
description=__doc__,
211246
formatter_class=argparse.RawDescriptionHelpFormatter)
212247
parser.add_argument('command')
213248
parser.add_argument(
214249
'path', help='File for audio file to be recognized')
250+
parser.add_argument(
251+
'first', help='First language in audio file to be recognized')
252+
parser.add_argument(
253+
'second', help='Second language in audio file to be recognized')
215254

216255
args = parser.parse_args()
217256

@@ -225,3 +264,5 @@ def transcribe_file_with_multichannel(speech_file):
225264
transcribe_file_with_diarization(args.path)
226265
elif args.command == 'multi-channel':
227266
transcribe_file_with_multichannel(args.path)
267+
elif args.command == 'multi-language':
268+
transcribe_file_with_multilanguage(args.path, args.first, args.second)

packages/google-cloud-speech/samples/snippets/beta_snippets_test.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
transcribe_file_with_diarization,
1919
transcribe_file_with_enhanced_model,
2020
transcribe_file_with_metadata,
21-
transcribe_file_with_multichannel)
21+
transcribe_file_with_multichannel,
22+
transcribe_file_with_multilanguage)
2223

2324
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
2425

@@ -61,3 +62,11 @@ def test_transcribe_multichannel_file(capsys):
6162
out, err = capsys.readouterr()
6263

6364
assert 'OK Google stream stranger things from Netflix to my TV' in out
65+
66+
67+
def test_transcribe_multilanguage_file(capsys):
68+
transcribe_file_with_multilanguage(
69+
os.path.join(RESOURCES, 'multi.wav'), 'en-US', 'es')
70+
out, err = capsys.readouterr()
71+
72+
assert 'how are you doing estoy bien e tu' in out
Binary file not shown.

0 commit comments

Comments
 (0)