19
19
import contextlib
20
20
import re
21
21
import signal
22
+ import sys
22
23
import threading
23
24
24
25
from google .cloud import credentials
@@ -131,13 +132,15 @@ def record_audio(rate, chunk):
131
132
# [END audio_stream]
132
133
133
134
134
- def request_stream (data_stream , rate ):
135
+ def request_stream (data_stream , rate , interim_results = True ):
135
136
"""Yields `StreamingRecognizeRequest`s constructed from a recording audio
136
137
stream.
137
138
138
139
Args:
139
140
data_stream: A generator that yields raw audio data to send.
140
141
rate: The sampling rate in hertz.
142
+ interim_results: Whether to return intermediate results, before the
143
+ transcription is finalized.
141
144
"""
142
145
# The initial request must contain metadata about the stream, so the
143
146
# server knows how to interpret it.
@@ -146,12 +149,12 @@ def request_stream(data_stream, rate):
146
149
# https://goo.gl/KPZn97 for the full list.
147
150
encoding = 'LINEAR16' , # raw 16-bit signed LE samples
148
151
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
151
153
# for a list of supported languages.
152
154
language_code = 'en-US' , # a BCP-47 language tag
153
155
)
154
156
streaming_config = cloud_speech .StreamingRecognitionConfig (
157
+ interim_results = interim_results ,
155
158
config = recognition_config ,
156
159
)
157
160
@@ -164,21 +167,40 @@ def request_stream(data_stream, rate):
164
167
165
168
166
169
def listen_print_loop (recognize_stream ):
170
+ num_chars_printed = 0
167
171
for resp in recognize_stream :
168
172
if resp .error .code != code_pb2 .OK :
169
173
raise RuntimeError ('Server error: ' + resp .error .message )
170
174
171
- # Display the transcriptions & their alternatives
172
- for result in resp .results :
173
- print (result .alternatives )
175
+ if not resp .results :
176
+ continue
174
177
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
182
204
183
205
184
206
def main ():
0 commit comments