【发布时间】:2018-04-10 05:03:28
【问题描述】:
我正在编写一个使用 Google Cloud Platform 的流式语音识别 API 的应用程序。这个想法是主循环持续监控麦克风输入(总是在待机状态下监听),一旦音频峰值超过某个阈值水平,它就会产生一个 MicrophoneStream 类实例以发出语音识别请求。这是一种绕过 Google API 对流持续时间的一分钟限制的方法。 1 分钟后,系统要么返回待机状态监测声级,要么创建一个新的 MicrophoneStream 实例以防有人仍在讲话。
问题是一分钟后 MicrophoneStream 实例并没有安静地运行并引发异常:
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with
(StatusCode.INVALID_ARGUMENT, Client GRPC deadline too short. Should be at
least: 3 * audio-duration + 5 seconds. Current deadline is:
188.99906457681209 second(s). Required at least: 194 second(s).)>
看起来像known bug in Google API,但是我在任何地方都没有找到解决方案。我一直在寻找几天试图弄清楚如何更改 GRPC 截止日期设置以防止此错误。或者,我很乐意直接忽略它,但是 try: 和 Except Exception: 似乎也不起作用。有任何想法吗?以下是 Google 的 Python 实现示例:
from __future__ import division
import re
import sys
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
import pyaudio
from six.moves import queue
# Audio recording parameters
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
class MicrophoneStream(object):
"""Opens a recording stream as a generator yielding the audio chunks."""
def __init__(self, rate, chunk):
self._rate = rate
self._chunk = chunk
# Create a thread-safe buffer of audio data
self._buff = queue.Queue()
self.closed = True
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
channels=1, rate=self._rate,
input=True, frames_per_buffer=self._chunk,
stream_callback=self._fill_buffer,
)
self.closed = False
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
# Now consume whatever other data's still buffered.
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yield b''.join(data)
# [END audio_stream]
def listen_print_loop(responses):
num_chars_printed = 0
for response in responses:
if not response.results:
continue
result = response.results[0]
if not result.alternatives:
continue
# Display the transcription of the top alternative.
transcript = result.alternatives[0].transcript
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
if not result.is_final:
sys.stdout.write(transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(transcript)
else:
print(transcript + overwrite_chars)
if re.search(r'\b(exit|quit)\b', transcript, re.I):
print('Exiting..')
break
num_chars_printed = 0
def main():
language_code = 'en-US' # a BCP-47 language tag
client = speech.SpeechClient()
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=RATE,
language_code=language_code)
streaming_config = types.StreamingRecognitionConfig(
config=config,
interim_results=True)
with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
requests = (types.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator)
responses = client.streaming_recognize(streaming_config, requests)
# Now, put the transcription responses to use.
listen_print_loop(responses)
if __name__ == '__main__':
main()
【问题讨论】:
标签: python google-cloud-platform speech-recognition grpc