16
16
17
17
from __future__ import division
18
18
19
+ import argparse
19
20
import contextlib
20
21
import re
21
22
import threading
25
26
from google .rpc import code_pb2
26
27
from grpc .beta import implementations
27
28
import pyaudio
28
- from google . cloud . speech . v1beta1 . cloud_speech_pb2 import StreamingRecognizeResponse
29
+
29
30
30
31
# Audio recording parameters
31
32
RATE = 16000
35
36
# Keep the request alive for this many seconds
36
37
DEADLINE_SECS = 8 * 60 * 60
37
38
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'
38
- SINGLE_UTTERANCE = False
39
39
40
40
41
41
def make_channel (host , port ):
@@ -78,7 +78,7 @@ def record_audio(channels, rate, chunk):
78
78
# [END audio_stream]
79
79
80
80
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 ):
82
82
"""Yields `StreamingRecognizeRequest`s constructed from a recording audio
83
83
stream.
84
84
@@ -107,7 +107,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
107
107
# re-interprets audio in the context of subsequent audio. However, this
108
108
# will give us quick results without having to tell the server when to
109
109
# finalize a piece of audio.
110
- interim_results = True , single_utterance = SINGLE_UTTERANCE
110
+ interim_results = True , single_utterance = single_utterance
111
111
)
112
112
113
113
yield cloud_speech .StreamingRecognizeRequest (
@@ -123,7 +123,7 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
123
123
yield cloud_speech .StreamingRecognizeRequest (audio_content = data )
124
124
125
125
126
- def listen_print_loop (recognize_stream ):
126
+ def listen_print_loop (recognize_stream , single_utterance ):
127
127
for resp in recognize_stream :
128
128
if resp .error .code != code_pb2 .OK :
129
129
raise RuntimeError ('Server error: ' + resp .error .message )
@@ -140,18 +140,27 @@ def listen_print_loop(recognize_stream):
140
140
print ('Exiting..' )
141
141
return
142
142
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.' )
145
147
return
146
148
149
+ parser = argparse .ArgumentParser ()
150
+ parser .add_argument ('-s' , '--single-utterance' , action = 'store_true' , default = False )
151
+
147
152
def main ():
153
+ args = parser .parse_args ()
154
+
155
+ single_utterance = args .single_utterance
156
+
148
157
stop_audio = threading .Event ()
149
158
with cloud_speech .beta_create_Speech_stub (
150
159
make_channel ('speech.googleapis.com' , 443 )) as service :
151
160
try :
152
161
listen_print_loop (
153
162
service .StreamingRecognize (
154
- request_stream (stop_audio ), DEADLINE_SECS ))
163
+ request_stream (stop_audio , single_utterance ), DEADLINE_SECS ), single_utterance )
155
164
finally :
156
165
# Stop the request stream once we're done with the loop - otherwise
157
166
# it'll keep going in the thread that the grpc lib makes for it..
0 commit comments