我试图在Python中使用。我刚刚尝试了以下代码:
import speech_recognition as sr import pyaudio r = sr.Recognizer() with sr.Microphone() as source:
# read the audio data from the default microphone
print("Speak!")
audio_text = r.listen(source)
print("Recognizing...")
# convert speech to text
text = r.recognize_google(audio_text)
print("You said: ", text)
但是我得到了错误的请求400。
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
speech_recognition.RequestError: recognition request failed: Bad Request
我所有的Python包都是最新的,所以我缺少了什么?
发布于 2022-03-06 02:57:53
第一步是导入语音识别,因为它是一个依赖项。通过运行pip并输入"pip Install SpeechRecognition“,然后输入import speech_recognition as sr
来安装
下一步是实际的重要部分,也就是google通过创建函数作为查询获取演讲的部分。我选择了"takeCommand“作为函数,如下所示,您将识别器分配给一个变量,您可以打开麦克风,在变量行下面的语句中,您只能使用麦克风安装时,您可以在终端中输入”pipinstallpy音频“来完成此操作,或者在某个终端中输入”pipinstallpy音频“,或者您确实需要一个异常处理程序,以防谷歌遗漏了任何东西。
创建一个函数def takeCommand():
并将以下内容放入函数中。
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
print("User said: {query}\n")
except Exception as e:
print(e)
print("Google didn't Understand.")
return "None"
return query
这可以用很多不同的方式。如果有任何问题,请告诉我,我甚至可以提供这些节目的样本。
编辑
除非您确信以下内容,否则此解决方案将无法工作:
https://stackoverflow.com/questions/71257555
复制相似问题