1919import contextlib
2020import re
2121import signal
22+ import sys
2223import threading
2324
2425from google .cloud import credentials
@@ -131,13 +132,15 @@ def record_audio(rate, chunk):
131132# [END audio_stream]
132133
133134
134- def request_stream (data_stream , rate ):
135+ def request_stream (data_stream , rate , interim_results = True ):
135136 """Yields `StreamingRecognizeRequest`s constructed from a recording audio
136137 stream.
137138
138139 Args:
139140 data_stream: A generator that yields raw audio data to send.
140141 rate: The sampling rate in hertz.
142+ interim_results: Whether to return intermediate results, before the
143+ transcription is finalized.
141144 """
142145 # The initial request must contain metadata about the stream, so the
143146 # server knows how to interpret it.
@@ -146,12 +149,12 @@ def request_stream(data_stream, rate):
146149 # https://goo.gl/KPZn97 for the full list.
147150 encoding = 'LINEAR16' , # raw 16-bit signed LE samples
148151 sample_rate = rate , # the rate in hertz
149- # See
150- # https://g.co/cloud/speech/docs/best-practices#language_support
152+ # See http://g.co/cloud/speech/docs/languages
151153 # for a list of supported languages.
152154 language_code = 'en-US' , # a BCP-47 language tag
153155 )
154156 streaming_config = cloud_speech .StreamingRecognitionConfig (
157+ interim_results = interim_results ,
155158 config = recognition_config ,
156159 )
157160
@@ -164,21 +167,40 @@ def request_stream(data_stream, rate):
164167
165168
166169def listen_print_loop (recognize_stream ):
170+ num_chars_printed = 0
167171 for resp in recognize_stream :
168172 if resp .error .code != code_pb2 .OK :
169173 raise RuntimeError ('Server error: ' + resp .error .message )
170174
171- # Display the transcriptions & their alternatives
172- for result in resp .results :
173- print (result .alternatives )
175+ if not resp .results :
176+ continue
174177
175- # Exit recognition if any of the transcribed phrases could be
176- # one of our keywords.
177- if any (re .search (r'\b(exit|quit)\b' , alt .transcript , re .I )
178- for result in resp .results
179- for alt in result .alternatives ):
180- print ('Exiting..' )
181- break
178+ # Display the top transcription
179+ result = resp .results [0 ]
180+ transcript = result .alternatives [0 ].transcript
181+
182+ # Display interim results, but with a carriage return at the end of the
183+ # line, so subsequent lines will overwrite them.
184+ if not result .is_final :
185+ # If the previous result was longer than this one, we need to print
186+ # some extra spaces to overwrite the previous result
187+ overwrite_chars = ' ' * max (0 , num_chars_printed - len (transcript ))
188+
189+ sys .stdout .write (transcript + overwrite_chars + '\r ' )
190+ sys .stdout .flush ()
191+
192+ num_chars_printed = len (transcript )
193+
194+ else :
195+ print (transcript )
196+
197+ # Exit recognition if any of the transcribed phrases could be
198+ # one of our keywords.
199+ if re .search (r'\b(exit|quit)\b' , transcript , re .I ):
200+ print ('Exiting..' )
201+ break
202+
203+ num_chars_printed = 0
182204
183205
184206def main ():
0 commit comments