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 af32d48

Browse filesBrowse files
committed
Add single_utterance flag as an argument to main.
1 parent fbf4141 commit af32d48
Copy full SHA for af32d48

File tree

Expand file treeCollapse file tree

1 file changed

+17
-8
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+17
-8
lines changed

‎speech/api/speech_streaming.py

Copy file name to clipboardExpand all lines: speech/api/speech_streaming.py
+17-8Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import division
1818

19+
import argparse
1920
import contextlib
2021
import re
2122
import threading
@@ -25,7 +26,7 @@
2526
from google.rpc import code_pb2
2627
from grpc.beta import implementations
2728
import pyaudio
28-
from google.cloud.speech.v1beta1.cloud_speech_pb2 import StreamingRecognizeResponse
29+
2930

3031
# Audio recording parameters
3132
RATE = 16000
@@ -35,7 +36,6 @@
3536
# Keep the request alive for this many seconds
3637
DEADLINE_SECS = 8 * 60 * 60
3738
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'
38-
SINGLE_UTTERANCE = False
3939

4040

4141
def make_channel(host, port):
@@ -78,7 +78,7 @@ def record_audio(channels, rate, chunk):
7878
# [END audio_stream]
7979

8080

81-
def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
81+
def request_stream(stop_audio, single_utterance, channels=CHANNELS, rate=RATE, chunk=CHUNK):
8282
"""Yields `StreamingRecognizeRequest`s constructed from a recording audio
8383
stream.
8484
@@ -107,7 +107,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
107107
# re-interprets audio in the context of subsequent audio. However, this
108108
# will give us quick results without having to tell the server when to
109109
# finalize a piece of audio.
110-
interim_results=True, single_utterance=SINGLE_UTTERANCE
110+
interim_results=True, single_utterance=single_utterance
111111
)
112112

113113
yield cloud_speech.StreamingRecognizeRequest(
@@ -123,7 +123,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
123123
yield cloud_speech.StreamingRecognizeRequest(audio_content=data)
124124

125125

126-
def listen_print_loop(recognize_stream):
126+
def listen_print_loop(recognize_stream, single_utterance):
127127
for resp in recognize_stream:
128128
if resp.error.code != code_pb2.OK:
129129
raise RuntimeError('Server error: ' + resp.error.message)
@@ -140,18 +140,27 @@ def listen_print_loop(recognize_stream):
140140
print('Exiting..')
141141
return
142142

143-
if SINGLE_UTTERANCE and resp.endpointer_type == StreamingRecognizeResponse.END_OF_UTTERANCE:
144-
print ('End of utterance. Exiting...')
143+
if (single_utterance and
144+
resp.endpointer_type ==
145+
cloud_speech.StreamingRecognizeResponse.END_OF_UTTERANCE):
146+
print ('End of utterance. Exiting.')
145147
return
146148

149+
parser = argparse.ArgumentParser()
150+
parser.add_argument('-s', '--single-utterance', action='store_true', default=False)
151+
147152
def main():
153+
args = parser.parse_args()
154+
155+
single_utterance = args.single_utterance
156+
148157
stop_audio = threading.Event()
149158
with cloud_speech.beta_create_Speech_stub(
150159
make_channel('speech.googleapis.com', 443)) as service:
151160
try:
152161
listen_print_loop(
153162
service.StreamingRecognize(
154-
request_stream(stop_audio), DEADLINE_SECS))
163+
request_stream(stop_audio, single_utterance), DEADLINE_SECS), single_utterance)
155164
finally:
156165
# Stop the request stream once we're done with the loop - otherwise
157166
# it'll keep going in the thread that the grpc lib makes for it..

0 commit comments

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