我正在编写一个使用 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()
最佳答案
迟到的答案,但我还是写了: Google 语音的硬超时设置为 60 秒。您无法通过 grpc 向其传输超过 60 秒的内容。 例如,解决方法是每 55 秒重新启动一次 grpc 调用。
关于python - Google 语音 API GRPC 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46994549/