使用以下代码进行语音识别根本不起作用
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