繁体   English   中英

语音识别 Python 出现奇怪的请求错误

[英]Speech Recognition Python Having Strange Request Error

使用以下代码进行语音识别根本不起作用

with sr.Microphone() as source:
# read the audio data from the default microphone
audio = r.record(source, duration=4)
print("Recognizing...")
# convert speech to text
# recognize speech using Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    print("Google Speech Recognition thinks you said in English: -  " + r.recognize_google(audio, language = "en-US"))
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

这是完整的错误,似乎请求只是失败了,但是如果我上传音频文件作为源,同样的代码似乎可以正常工作。 我已经通过 sr.Microphone 进行了检查,默认选项也正确链接到我的实际麦克风...

    ---------------------------------------------------------------------------
HTTPError                                 Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
    839         try:
--> 840             response = urlopen(request, timeout=self.operation_timeout)
    841         except HTTPError as e:

~/anaconda3/lib/python3.7/urllib/request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    221         opener = _opener
--> 222     return opener.open(url, data, timeout)
    223 

~/anaconda3/lib/python3.7/urllib/request.py in open(self, fullurl, data, timeout)
    530             meth = getattr(processor, meth_name)
--> 531             response = meth(req, response)
    532 

~/anaconda3/lib/python3.7/urllib/request.py in http_response(self, request, response)
    640             response = self.parent.error(
--> 641                 'http', request, response, code, msg, hdrs)
    642 

~/anaconda3/lib/python3.7/urllib/request.py in error(self, proto, *args)
    568             args = (dict, 'default', 'http_error_default') + orig_args
--> 569             return self._call_chain(*args)
    570 

~/anaconda3/lib/python3.7/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
    502             func = getattr(handler, meth_name)
--> 503             result = func(*args)
    504             if result is not None:

~/anaconda3/lib/python3.7/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648     def http_error_default(self, req, fp, code, msg, hdrs):
--> 649         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    650 

HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

RequestError                              Traceback (most recent call last)
<ipython-input-109-50b94b08a896> in <module>
      3     audio = r.record(source, duration=4)
      4     print("Recognizing...")
----> 5     r.recognize_google(audio, language = "en-US")

~/anaconda3/lib/python3.7/site-packages/speech_recognition/__init__.py in recognize_google(self, audio_data, key, language, show_all)
    840             response = urlopen(request, timeout=self.operation_timeout)
    841         except HTTPError as e:
--> 842             raise RequestError("recognition request failed: {}".format(e.reason))
    843         except URLError as e:
    844             raise RequestError("recognition connection failed: {}".format(e.reason))

RequestError: recognition request failed: Bad Request

在此处输入图像描述

这可能不是您编写的代码的解决方案。 但会帮助许多其他尝试使用 Google SpeechRecognition并获得相同模糊错误消息的人。

我通过将音频输入文件缩短到less than 10 MB解决了同样的问题。 目前,同步请求的配额约为 1 分钟 (10 MB)。

引用文档

使用本地文件发送到 API 的所有单个请求的大小限制为 10 MB。 识别LongRunningRecognize方法的情况下,此限制适用于发送的请求的大小。 对于 StreamingRecognize 方法, 10 MB限制适用于初始StreamingRecognize请求和 stream 中每个单独消息的大小。 超过此限制将引发错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM