[英]Google Speech API GRPC timeout
我正在编写一个使用 Google Cloud Platform 的 Streaming Speech Recognition 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:
似乎也不起作用。 有任何想法吗? 这是 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()
迟到的答案,但我还是写了:Google 语音的硬超时设置为 60 秒。 您不能通过 grpc 向它传输超过 60 秒的内容。 例如,一种解决方法是每 55 秒重新启动一次 grpc 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.