简体   繁体   English

python中的SpeechRecogniton模块太慢

[英]SpeechRecogniton module is too slow in python

I was trying to use Speech Recognition for my Deep Learning Chatbot to get the input from the user.我试图为我的深度学习聊天机器人使用语音识别来获取用户的输入。 Actually my Speech-Recognition function code is this:其实我的语音识别功能代码是这样的:

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()

Well, there are no errors, and that's the biggest error!好吧,没有错误,这是最大的错误! No problem with my internet connection as I could stream high-quality video at the same time, and this not even video, it is a string, so what might be the problem?我的互联网连接没有问题,因为我可以同时流式传输高质量的视频,这甚至不是视频,它是一个字符串,那么可能是什么问题? I have to wait for almost 15 mins to get a text.我必须等待近 15 分钟才能收到短信。

Well, I have also tried an offline API: the recognize_sphinix() method.嗯,我还尝试了一个离线 API: recognize_sphinix()方法。 You need to build the binary installation file(whl) of pocketsphinix.您需要构建pocketsphinix 的二进制安装文件(whl)。 Oh, I forgot to mention, you also need to build pyaudio in your machine to use speech_recognition.哦,我忘了说,您还需要在您的机器中构建pyaudio才能使用speech_recognition。 I have done all that stuffs, even the same problem is with this offline API... In the morning recognize.sphinix() recognized 2-3 times what I told, but now, it's not even responding that!我已经完成了所有这些工作,即使这个离线 API 也存在同样的问题......早上来说, recognize.sphinix()识别了我所说的 2-3 倍,但现在,它甚至没有响应!

NOTE: I have monitored my pc with task manager with only the speech-recognition function running, and Python was just taking 9MB of RAM, and 0.3% CPU usage.注意:我用任务管理器监控我的电脑,只运行语音识别功能,而 Python 只占用了 9MB 的内存和 0.3% 的 CPU 使用率。 So there is no problem with limited Computing Power.所以计算能力有限是没有问题的。

Can anyone solve this?任何人都可以解决这个问题吗? You will make my day if you solve this headache.如果你解决了这个头痛问题,你会让我开心的。 Thanks in advance!提前致谢!

The duration parameter is deprecated now.持续时间参数现已弃用。 Ref StackOverflow question .参考StackOverflow 问题
Instead use phrase_time_limit or timeout .而是使用phrase_time_limittimeout

Here is the block of code using phrase_time_limit :这是使用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

This works perfectly fine.这工作得很好。

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

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