23
23
python beta_snippets.py punctuation resources/commercial_mono.wav
24
24
python beta_snippets.py diarization resources/commercial_mono.wav
25
25
python beta_snippets.py multi-channel resources/commercial_mono.wav
26
+ python beta_snippets.py multi-language resources/multi.wav en-US es
26
27
"""
27
28
28
29
import argparse
@@ -205,13 +206,51 @@ def transcribe_file_with_multichannel(speech_file):
205
206
# [END speech_transcribe_multichannel]
206
207
207
208
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
+
208
243
if __name__ == '__main__' :
209
244
parser = argparse .ArgumentParser (
210
245
description = __doc__ ,
211
246
formatter_class = argparse .RawDescriptionHelpFormatter )
212
247
parser .add_argument ('command' )
213
248
parser .add_argument (
214
249
'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' )
215
254
216
255
args = parser .parse_args ()
217
256
@@ -225,3 +264,5 @@ def transcribe_file_with_multichannel(speech_file):
225
264
transcribe_file_with_diarization (args .path )
226
265
elif args .command == 'multi-channel' :
227
266
transcribe_file_with_multichannel (args .path )
267
+ elif args .command == 'multi-language' :
268
+ transcribe_file_with_multilanguage (args .path , args .first , args .second )
0 commit comments