Google Speech API GRPC timeout



我正在编写一款使用谷歌云平台的流式语音识别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).)> 

这似乎是Google API中的一个已知错误,但我在任何地方都没有找到解决方案。我已经搜索了好几天,试图找出如何更改GRPC截止日期设置以防止出现此错误。或者,我非常乐意忽略它,但是try:Except Exception:似乎也不起作用。有什么想法吗?以下是谷歌的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()

答案很晚,但我还是写了:谷歌语音的硬超时设置为60秒。你不能通过grpc流式传输超过60秒。例如,一种解决方法是每隔55秒重新启动grpc调用。

最新更新