[Python] SpeechRecognition and Pocket Sphinx doesn't work

Discussão em 'Python' iniciado por Stack, Março 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm following a youtube tutorial to make an assistant in python. When I say "Piggy", it responds as usual, but after that, when I say how are you, it gives me this error. The guy in the video has no problems with it. RuntimeError: run loop already started I don't know what's wrong with my code.

    import pyttsx3
    import speech_recognition as sr
    import time

    r = sr.Recognizer()
    keywords = [("piggy", 1), ("hey piggy", 1)]
    source = sr.Microphone()

    def Speak(text):
    rate = 100
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[14].id)
    engine.setProperty('rate', rate+50)
    engine.say(text)
    engine.runAndWait()

    def callback(recognizer, audio):
    try:
    speech_as_text = recognizer.recognize_sphinx(audio, keyword_entries=keywords)
    print(speech_as_text)
    if "piggy" in speech_as_text or "hey piggy":
    Speak("Yes sir?")
    recognize_main()
    except sr.UnknownValueError:
    print("Oops! Didn't catch that")

    def start_recognizer():
    print("Waiting for a keyword...piggy or Hey piggy")
    r.listen_in_background(source, callback)
    time.sleep(1000000)

    def recognize_main():
    with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)
    data = ""
    try:
    data = r.recognize_google(audio)
    data.lower()
    print("You said: " + data)
    if "how are you" in data:
    Speak("I am Fine")
    elif "hello" in data:
    Speak ("Hi there")
    else:
    Speak("I'm sorry sir, I did not understand your request")
    except sr.UnknownValueError:
    print("piggy did not understand your request")
    except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

    while 1:
    start_recognizer()

    Continue reading...

Compartilhe esta Página