简体   繁体   中英

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.

Well, I have also tried an offline API: the recognize_sphinix() method. You need to build the binary installation file(whl) of pocketsphinix. Oh, I forgot to mention, you also need to build pyaudio in your machine to use 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!

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. 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 .
Instead use phrase_time_limit or timeout .

Here is the block of code using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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