python中的SpeechRecogniton模块太慢
问题内容:
我试图将语音识别用于我的深度学习聊天机器人,以从用户那里获取输入。实际上,我的语音识别功能代码是这样的:
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
try:
print("Listening...")
said = r.recognize_google(audio)
print("You said: " + said)
except Exception as e:
print("Exception: " + str(e))
return said.lower()
好吧,没有错误,那是最大的错误!我的互联网连接没有问题,因为我可以同时流式传输高质量的视频,这甚至不是视频,它是一个字符串,那么可能是什么问题?我必须等待近15分钟才能收到短信。
好吧,我也尝试过离线API:recognize_sphinix()
方法。您需要构建Pocketsphinix的二进制安装文件(whl)。哦,我忘了提,您还需要在计算机中构建pyaudio才能使用Speech_recognition。我已经完成了所有这些工作,即使这个脱机API遇到了同样的问题,也是如此……在早上recognize.sphinix()
,我识别出的声音是我告诉过的信息的2-3倍,但是现在,它甚至没有响应!
注意:
我已经使用任务管理器监视了我的电脑,并且只运行了语音识别功能,而Python仅占用了9MB的RAM和0.3%的CPU使用率。因此,有限的计算能力没有问题。
谁能解决这个问题?如果您解决了这个头痛事,您将使我高兴。提前致谢!
问题答案:
现在不建议使用duration参数。参考StackOverflow问题。
而是使用phrase_time_limit
或timeout
。
这是使用的代码块phrase_time_limit
:
import speech_recognition as sr
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source, phrase_time_limit = 5)
try:
command = r.recognize_google(audio).lower()
print("you said: " + command)
except sr.UnknownValueError:
print("Sorry, Cant understand, Please say again")
command = myCommand()
return command
这工作得很好。