[Python] How do I give the time duration of listening to python speech recognition library?

Discussão em 'Python' iniciado por Stack, Abril 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on RPi 4 and got the code working but the listening time, from my microphone, of my speech recognition object is really long almost like 10 seconds. I want to decrease this time. I looked at the speech recognition library documentation but it does not mention the function anywhere.

    Python editor gives me the following prompt of the listen() function;


    self:Recogniser, source, timeout = None,phrase_time_limit = None snowboy_configuration =None
    ​

    So I tried calling the function like:

    audio = r.listen(source,None,3)


    Or

    audio = r.listen(source,3,3)


    hoping it will listen for 3 seconds, but it's not working that way.

    Following is my code:

    import speech_recognition as sr

    r = sr.Recognizer()

    speech = sr.Microphone(2)

    #print(sr.Microphone.list_microphone_names())

    while 1:

    with speech as source:
    print("say something!…")
    audio = r.adjust_for_ambient_noise(source)
    audio = r.listen(source,None,3)
    print("the audio has been recorded")
    # Speech recognition using Google Speech Recognition
    try:
    print("api is enabled")
    recog = r.recognize_google(audio, language = 'en-US')
    # for testing purposes, we're just using the default API key
    # to use another API key, use r.recognize_google(audio)
    # instead of r.recognize_google(audio)

    print("You said: " + recog)
    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))


    How do I decrease the time duration of listening?

    Continue reading...

Compartilhe esta Página