Search code examples
python-3.xspeech-recognition

How can you stop a program from listening as soon as you stop talking?


I am making a simple program to listen to my michrophone and using google's api convert it to text. It's going fine but my problem is that when i am stop talking, the program keeps listening and just after few seconds after i stopped talking it stops.

I know its probably a problem with my noise in the background although I have Blue yeti microphone so it should'nt rally happens but still I tried to adjust the noise and yet it showed the same result.

import speech_recognition as sr

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone(device_index = 1) as source:
    print("Say something!")
    #audio = r.adjust_for_ambient_noise(source) - Tried also that
    audio = r.listen(source)
    print('Stopped listening!')
try:
    print("Google Speech Recognition thinks you said " + r.recognize_google(audio, language="he-HE"))
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))

As expected the program keeps listening even when i'm not talking for few seconds which makes it really slow and bad.. Thanks for the help!


Solution

  • You can try adjusting the pause_threshold attribute of the Recognizerclass. By default, it is set to 0.8 Found the info by looking at the Recognizer source code here: https://github.com/Uberi/speech_recognition/blob/350397d2fb5db318c877f29ee3dc6e6cbf4a393d/speech_recognition/init.py#L508