diff --git a/speech/api/speech_streaming.py b/speech/api/speech_streaming.py index d4bd86685af..2bf93c8a35d 100644 --- a/speech/api/speech_streaming.py +++ b/speech/api/speech_streaming.py @@ -16,6 +16,7 @@ from __future__ import division +import argparse import contextlib import re import threading @@ -26,6 +27,7 @@ from grpc.beta import implementations import pyaudio + # Audio recording parameters RATE = 16000 CHANNELS = 1 @@ -76,7 +78,7 @@ def record_audio(channels, rate, chunk): # [END audio_stream] -def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK): +def request_stream(stop_audio, single_utterance, channels=CHANNELS, rate=RATE, chunk=CHUNK): """Yields `StreamingRecognizeRequest`s constructed from a recording audio stream. @@ -105,7 +107,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK): # re-interprets audio in the context of subsequent audio. However, this # will give us quick results without having to tell the server when to # finalize a piece of audio. - interim_results=True, single_utterance=True + interim_results=True, single_utterance=single_utterance ) yield cloud_speech.StreamingRecognizeRequest( @@ -121,7 +123,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK): yield cloud_speech.StreamingRecognizeRequest(audio_content=data) -def listen_print_loop(recognize_stream): +def listen_print_loop(recognize_stream, single_utterance): for resp in recognize_stream: if resp.error.code != code_pb2.OK: raise RuntimeError('Server error: ' + resp.error.message) @@ -138,19 +140,33 @@ def listen_print_loop(recognize_stream): print('Exiting..') return + if (single_utterance and + resp.endpointer_type == + cloud_speech.StreamingRecognizeResponse.END_OF_UTTERANCE): + print ('End of utterance. Exiting.') + return + +parser = argparse.ArgumentParser() +parser.add_argument('-s', '--single-utterance', action='store_true', default=False) def main(): + args = parser.parse_args() + + single_utterance = args.single_utterance + stop_audio = threading.Event() with cloud_speech.beta_create_Speech_stub( make_channel('speech.googleapis.com', 443)) as service: try: listen_print_loop( service.StreamingRecognize( - request_stream(stop_audio), DEADLINE_SECS)) + request_stream(stop_audio, single_utterance), DEADLINE_SECS), single_utterance) finally: # Stop the request stream once we're done with the loop - otherwise # it'll keep going in the thread that the grpc lib makes for it.. stop_audio.set() + print ('Exiting.') + return if __name__ == '__main__':