Search code examples
pythonpyaudio

Why is my Python speechRecognition.listen not stopping to record


Hi im currently looking for the easiest possible python voice assistant. I watched some to tutorials and managed it to write this code:

import speech_recognition as sr

speech_engine = sr.Recognizer()


def from_mic():
    with sr.Microphone() as micro:
        print("Recording...")
        audio = speech_engine.listen(micro)
        print("Recognizing...")
        text = speech_engine.recognize_google(audio, language="de-DE")
        return text

print(from_mic())

I am sure that listen() normally should stop when I stop speaking, but my programm just runs and says Recording... it does not try to figure out what has been said. It can also be because of my bad headset which makes white noise but i dont think so. But i dont find any mistake in the code


Solution

  • Use:

    speech_engine.adjust_for_ambient_noise(micro)
    

    before listening.


    This answer was posted as an edit to the question SOLVED: Why is my Python speechRecognition.listen not stopping to record by the OP Devolper12345 under CC BY-SA 4.0.