gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:46:01 30 4
gpt4 key购买 nike

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

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
enter image description here

最佳答案

这可能不是您编写的代码的解决方案。但会帮助许多其他尝试使用 Google 的人 SpeechRecognition并收到相同的模糊错误消息。
我通过将音频输入文件缩短为 less than 10 MB 解决了同样的问题.
目前,同步请求的配额约为 1 分钟 (10 MB)。
报价documentation :

There is a limit of 10 MB on all single requests sent to the APIusing local files. In the case of the Recognize andLongRunningRecognize methods, this limit applies to the size of therequest sent. In the case of the StreamingRecognize method, the 10 MB limit applies to both the initial StreamingRecognize request and the size of each individual message in the stream. Exceeding this limit will throw an error.

关于python - 语音识别 Python 出现奇怪的请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62719408/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com