1
1
#!/usr/bin/python
2
2
3
3
import contextlib
4
+ import re
4
5
import threading
5
6
6
7
from gcloud .credentials import get_credentials
@@ -70,16 +71,27 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
70
71
# The initial request must contain metadata about the stream, so the
71
72
# server knows how to interpret it.
72
73
metadata = InitialRecognizeRequest (
73
- encoding = 'LINEAR16' , sample_rate = rate )
74
- audio_request = AudioRequest (content = audio_stream .read (chunk ))
74
+ encoding = 'LINEAR16' , sample_rate = rate ,
75
+ # Note that setting interim_results to True means that you'll
76
+ # likely get multiple results for the same bit of audio, as the
77
+ # system re-interprets audio in the context of subsequent audio.
78
+ # However, this will give us quick results without having to tell
79
+ # the server when to finalize a piece of audio.
80
+ interim_results = True , continuous = False ,
81
+ )
82
+ data = audio_stream .read (chunk )
83
+ audio_request = AudioRequest (content = data )
75
84
76
85
yield RecognizeRequest (
77
86
initial_request = metadata ,
78
87
audio_request = audio_request )
79
88
80
89
while not stop_audio .is_set ():
90
+ data = audio_stream .read (chunk )
91
+ if not data :
92
+ raise StopIteration ()
81
93
# Subsequent requests can all just have the content
82
- audio_request = AudioRequest (content = audio_stream . read ( chunk ) )
94
+ audio_request = AudioRequest (content = data )
83
95
84
96
yield RecognizeRequest (audio_request = audio_request )
85
97
@@ -95,8 +107,7 @@ def listen_print_loop(recognize_stream):
95
107
96
108
# Exit recognition if any of the transcribed phrases could be
97
109
# one of our keywords.
98
- if any (alt .confidence > .5 and
99
- (alt .transcript .strip () in ('exit' , 'quit' ))
110
+ if any (re .search (r'\b(exit|quit)\b' , alt .transcript )
100
111
for result in resp .results
101
112
for alt in result .alternatives ):
102
113
print ('Exiting..' )
0 commit comments