Skip to content

Commit d5cffb7

Browse files
committed
Merge pull request #343 from GoogleCloudPlatform/speech-streaming
Fix speech streaming sample & test
2 parents 2a813b9 + 5605ac5 commit d5cffb7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

speech/api/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ for more information.
5252
$ pip install -r requirements-speech_streaming.txt
5353
```
5454
55+
The sample uses the [PyAudio][pyaudio] library to stream audio from your
56+
computer's microphone. PyAudio depends on [PortAudio][portaudio], which may
57+
need to be compiled when you install PyAudio. If you run into compilation
58+
issues that mention PortAudio, you may have to [install some
59+
dependencies][pyaudio-install].
60+
61+
[pyaudio]: https://people.csail.mit.edu/hubert/pyaudio/
62+
[portaudio]: http://www.portaudio.com/
63+
[pyaudio-install]: https://people.csail.mit.edu/hubert/pyaudio/#downloads
64+
5565
## Run the example
5666

5767
* To run the `speech_rest.py` sample:

speech/api/speech_streaming.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import contextlib
4+
import re
45
import threading
56

67
from gcloud.credentials import get_credentials
@@ -70,16 +71,27 @@ def request_stream(stop_audio, channels=CHANNELS, rate=RATE, chunk=CHUNK):
7071
# The initial request must contain metadata about the stream, so the
7172
# server knows how to interpret it.
7273
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)
7584

7685
yield RecognizeRequest(
7786
initial_request=metadata,
7887
audio_request=audio_request)
7988

8089
while not stop_audio.is_set():
90+
data = audio_stream.read(chunk)
91+
if not data:
92+
raise StopIteration()
8193
# Subsequent requests can all just have the content
82-
audio_request = AudioRequest(content=audio_stream.read(chunk))
94+
audio_request = AudioRequest(content=data)
8395

8496
yield RecognizeRequest(audio_request=audio_request)
8597

@@ -95,8 +107,7 @@ def listen_print_loop(recognize_stream):
95107

96108
# Exit recognition if any of the transcribed phrases could be
97109
# 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)
100111
for result in resp.results
101112
for alt in result.alternatives):
102113
print('Exiting..')

speech/api/speech_streaming_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import io
1616
import re
1717
import sys
18+
import time
1819

19-
from gcp.testing.flaky import flaky
2020
import pytest
2121

2222
import speech_streaming
@@ -39,6 +39,9 @@ def __call__(self, *args):
3939
return self
4040

4141
def read(self, num_frames):
42+
# Approximate realtime by sleeping for the appropriate time for the
43+
# requested number of frames
44+
time.sleep(num_frames / float(speech_streaming.RATE))
4245
# audio is 16-bit samples, whereas python byte is 8-bit
4346
num_bytes = 2 * num_frames
4447
chunk = self.audio_file.read(num_bytes) or self.silence.read(num_bytes)
@@ -54,7 +57,6 @@ def mock_audio_stream(channels, rate, chunk):
5457
return mock_audio_stream
5558

5659

57-
@flaky
5860
@pytest.mark.skipif(
5961
sys.version_info >= (3, 0),
6062
reason=("grpc doesn't yet support python3 "

0 commit comments

Comments
 (0)