2121 python beta_snippets.py enhanced-model resources/commercial_mono.wav
2222 python beta_snippets.py metadata resources/commercial_mono.wav
2323 python beta_snippets.py punctuation resources/commercial_mono.wav
24+ python beta_snippets.py diarization resources/commercial_mono.wav
2425"""
2526
2627import argparse
@@ -126,6 +127,36 @@ def transcribe_file_with_auto_punctuation(path):
126127# [END speech_transcribe_file_with_auto_punctuation]
127128
128129
130+ # [START speech_transcribe_diarization]
131+ def transcribe_file_with_diarization (path ):
132+ """Transcribe the given audio file synchronously with diarization."""
133+ client = speech .SpeechClient ()
134+
135+ with open (path , 'rb' ) as audio_file :
136+ content = audio_file .read ()
137+
138+ audio = speech .types .RecognitionAudio (content = content )
139+
140+ config = speech .types .RecognitionConfig (
141+ encoding = speech .enums .RecognitionConfig .AudioEncoding .LINEAR16 ,
142+ sample_rate_hertz = 16000 ,
143+ language_code = 'en-US' ,
144+ enable_speaker_diarization = True ,
145+ diarization_speaker_count = 2 )
146+
147+ print ('Waiting for operation to complete...' )
148+ response = client .recognize (config , audio )
149+
150+ for i , result in enumerate (response .results ):
151+ alternative = result .alternatives [0 ]
152+ print ('-' * 20 )
153+ print ('First alternative of result {}: {}'
154+ .format (i , alternative .transcript ))
155+ print ('Speaker Tag for the first word: {}'
156+ .format (alternative .words [0 ].speaker_tag ))
157+ # [END speech_transcribe_diarization]
158+
159+
129160if __name__ == '__main__' :
130161 parser = argparse .ArgumentParser (
131162 description = __doc__ ,
@@ -142,3 +173,5 @@ def transcribe_file_with_auto_punctuation(path):
142173 transcribe_file_with_metadata (args .path )
143174 elif args .command == 'punctuation' :
144175 transcribe_file_with_auto_punctuation (args .path )
176+ elif args .command == 'diarization' :
177+ transcribe_file_with_diarization (args .path )
0 commit comments