Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming bug fixes #434

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions speech/api/speech_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import division

import argparse
import contextlib
import re
import threading
Expand All @@ -26,6 +27,7 @@
from grpc.beta import implementations
import pyaudio


# Audio recording parameters
RATE = 16000
CHANNELS = 1
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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__':
Expand Down