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 1be89c3

Browse filesBrowse files
author
Jerjou Cheng
committed
Print intermediate speech transcriptions.
Also - fix the test.
1 parent 2a813b9 commit 1be89c3
Copy full SHA for 1be89c3

File tree

Expand file treeCollapse file tree

2 files changed

+20
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-7
lines changed

‎speech/api/speech_streaming.py

Copy file name to clipboardExpand all lines: speech/api/speech_streaming.py
+16-5Lines 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

Copy file name to clipboardExpand all lines: speech/api/speech_streaming_test.py
+4-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.