在一个Python程序中,对Google的Speech API执行流式语音识别(基本上是详细的here),我试图捕获以下异常:
google.api_core.exceptions.InvalidArgument:400客户GRPC截止日期太短。应该至少:3 *音频持续时间+ 5秒。目前截止日期为:189.99934293236583秒。至少需要:196秒。
相关代码块:
with self.MicrophoneStream(self.RATE, self.CHUNK) as stream:
while True:
audio_generator = stream.generator()
requests = (types.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator)
responses = client.streaming_recognize(streaming_config, requests)
try:
self.listen_print_loop(responses, self.callback)
break
except:
self.start()
break
不标记异常是有效的,除了它使程序难以调试,因为它会导致任何地方的任何异常触发self.start()
。
我试过了:
- except StatusCode.INVALID_ARGUMENT # NameError: name 'StatusCode' is not defined
- except google.api_core.exceptions.InvalidArgument # NameError: name 'google' is not defined
- except grpc.RpcError # no error, just the usual exception above