SnowBoy hotword detection issue

534 Views Asked by At

Hi guys I'm trying to trigger the while loop in my code that starts speech recognition whenever the hotword "virgo" is said. The problem is that snowboy detects the hotword but I don't know how to execute the "while" loop once the hotword is triggered. Any help please? this may sound stupid and should be relatively easy but my brain is on fire right now. thank you!

import speech_recognition as sr
from textblob import TextBlob
import snowboydecoder

recognizer_instance = sr.Recognizer()

def detected_callback():
    print ("tell me!")

detector = snowboydecoder.HotwordDetector("virgo.pmdl",sensitivity=0.5)

detector.start(detected_callback=snowboydecoder.play_audio_file,sleep_time=0.03)
detector.terminate()


while True:
    with sr.Microphone() as source:
        recognizer_instance.adjust_for_ambient_noise(source)
        print("Listening...")
        audio = recognizer_instance.listen(source)
        print("copy that!")

    try:
        text = recognizer_instance.recognize_google(audio, language = "it-IT")
        print("you said:\n", text) 

    except Exception as e:
        break
1

There are 1 best solutions below

1
On

Your while loop is always 'triggered', given the TRUE, until you break out of it. To trigger the code within the loop, do, for example:

while True:
    if YOUR_TRIGGER_CONDITION:
        with sr.Microphone() as source:
            recognizer_instance.adjust_for_ambient_noise(source)
            print("Listening...")
            audio = recognizer_instance.listen(source)
            print("copy that!")

        try:
            text = recognizer_instance.recognize_google(audio, language = "it-IT")
            print("you said:\n", text) 

        except Exception as e:
            break