Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 784262f

Browse filesBrowse files
author
Jerjou Cheng
committed
Clean up continuous transcription.
1 parent c129c99 commit 784262f
Copy full SHA for 784262f

File tree

Expand file treeCollapse file tree

1 file changed

+34
-30
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+34
-30
lines changed

‎speech/grpc/transcribe_streaming_minute.py

Copy file name to clipboardExpand all lines: speech/grpc/transcribe_streaming_minute.py
+34-30Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
from __future__ import division
1818

19+
import argparse
1920
import contextlib
2021
import functools
22+
import logging
2123
import re
2224
import signal
2325
import sys
@@ -33,7 +35,7 @@
3335
import pyaudio
3436
from six.moves import queue
3537

36-
# Seconds to allow you to shut up
38+
# Seconds you have to wrap up your utterance
3739
WRAP_IT_UP_SECS = 55
3840

3941
# Audio recording parameters
@@ -172,9 +174,9 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
172174
the next result to overwrite it, until the response is a final one. For the
173175
final one, print a newline to preserve the finalized transcription.
174176
"""
175-
start_time = time.time()
177+
# What time should we switch to a new stream?
176178
time_to_switch = time.time() + max_recog_secs - wrap_it_up_secs
177-
wrap_it_up = False
179+
graceful_exit = False
178180
num_chars_printed = 0
179181
for resp in recognize_stream:
180182
if resp.error.code != code_pb2.OK:
@@ -183,12 +185,8 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
183185
if not resp.results:
184186
if resp.endpointer_type is resp.END_OF_SPEECH and (
185187
time.time() > time_to_switch):
186-
wrap_it_up = True
187-
resp = next(recognize_stream)
188-
if not resp.results:
189-
return True
190-
else:
191-
continue
188+
graceful_exit = True
189+
continue
192190

193191
# Display the top transcription
194192
result = resp.results[0]
@@ -213,20 +211,18 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
213211
# one of our keywords.
214212
if re.search(r'\b(exit|quit)\b', transcript, re.I):
215213
print('Exiting..')
216-
return False
214+
recognize_stream.cancel()
217215

218-
num_chars_printed = 0
216+
elif graceful_exit:
217+
break
219218

220-
if wrap_it_up:
221-
return True
219+
num_chars_printed = 0
222220

223221

224222
def main():
225223
service = cloud_speech_pb2.SpeechStub(
226224
make_channel('speech.googleapis.com', 443))
227225

228-
keep_going = True
229-
230226
# For streaming audio from the microphone, there are three threads.
231227
# First, a thread that collects audio data as it comes in
232228
with record_audio(RATE, CHUNK) as buff:
@@ -237,27 +233,35 @@ def main():
237233
requests, DEADLINE_SECS)
238234

239235
# Exit things cleanly on interrupt
240-
def handle_interrupt(*_):
241-
keep_going = False
242-
recognize_stream.cancel()
243-
signal.signal(signal.SIGINT, handle_interrupt)
236+
signal.signal(signal.SIGINT, lambda *_: recognize_stream.cancel())
244237

245238
# Now, put the transcription responses to use.
246-
while keep_going:
247-
print('==== Continuing... ====')
248-
keep_going = False
249-
try:
250-
keep_going = listen_print_loop(recognize_stream, WRAP_IT_UP_SECS)
239+
try:
240+
while True:
241+
listen_print_loop(recognize_stream, WRAP_IT_UP_SECS)
251242

243+
# Discard this stream and create a new one.
244+
# Note: calling .cancel() doesn't immediately raise an RpcError
245+
# - it only raises when the iterator's next() is requested
252246
recognize_stream.cancel()
253-
next(recognize_stream)
247+
248+
logging.debug('Starting new stream')
249+
requests = request_stream(_audio_data_generator(buff), RATE)
250+
recognize_stream = service.StreamingRecognize(
251+
requests, DEADLINE_SECS)
252+
253+
except grpc.RpcError, e:
254254
# This happens because of the interrupt handler
255-
except grpc.RpcError, e:
256-
if keep_going:
257-
requests = request_stream(_audio_data_generator(buff), RATE)
258-
recognize_stream = service.StreamingRecognize(
259-
requests, DEADLINE_SECS)
255+
pass
260256

261257

262258
if __name__ == '__main__':
259+
parser = argparse.ArgumentParser()
260+
parser.add_argument(
261+
'-v', '--verbose', help='increase output verbosity',
262+
action='store_true')
263+
args = parser.parse_args()
264+
if args.verbose:
265+
logging.basicConfig(level=logging.DEBUG)
266+
263267
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.