首页 > 解决方案 > recogniser.listen(source) 不会停止收听

问题描述

我正在制作一个 python 脚本,它会听你的声音,然后根据你所说的执行功能。

我刚刚开始制作“唤醒词”,代码如下所示:

while True:
    try:
        with sr.Microphone() as source1:
            SpeakText("one")
            r.adjust_for_ambient_noise(source1)
            print("Set minimum energy threshold to {}".format(r.energy_threshold))
            SpeakText("two")
            audio1 = r.listen(source)
            SpeakText("three")
            MyText = r.recognize_google(audio1)
            print(MyText)
            SpeakText("four")
            MyText = MyText.lower()

            if MyText == "oink" or "ink" or "link":
                playsound("oink_boop.mp3")
                detect_command()
                        
    except sr.UnknownValueError:
        print("No Speech or No Internet")

现在我放入 SpeakText 部分(使用 pyttsx3)只是为了调试目的,但它说“两个”然后卡在 audio1 = r.listen(source) 上听我的演讲

它似乎并没有停止听我说话,我什至尝试将麦克风静音,但这也没有用。

标签: pythonspeech-recognitionvoicepyttsx

解决方案


我试过这个:

    with sr.Microphone() as source1:
        r.adjust_for_ambient_noise(source1)
        r.pause_threshold = 1
            
        audio1 = r.listen(source1)
    try:
        
            MyText = r.recognize_google(audio1, language='en-en')
            MyText = MyText.lower()
            if "oink" in MyText or "ink" in MyText or "link" in MyText:
                playsound("oink_boop.mp3")
                detect_command()
                        
    except sr.UnknownValueError:
        print("No Speech or No Internet")

它仍然不起作用我做错了什么吗?


推荐阅读