识别器。侦听(源)不会停止侦听

2024-09-19 23:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个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(源代码)上听我的演讲时卡住了

它似乎没有停止听我说话,我甚至试着让麦克风静音,但也没用


Tags: orno脚本声音内容thresholdlistenenergy
2条回答
r = sr.Recognizer()
    with sr.Microphone() as source:
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        query = r.recognize_google(audio, language='en-en')
        your_query_Fuction(query)
    except Exception:
        print("No Speech or No Internet")
        your_Mic_Fuction()

您还应使用: if 'oink' in MyText or ...

编辑: 用这个

def your_Mic_Fuction():
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            r.pause_threshold = 1
            audio = r.listen(source)

        try:
            query = r.recognize_google(audio, language='en-en')
            your_query_Fuction(query)
        except Exception:
            print("No Speech or No Internet")
            your_Mic_Fuction()

def your_query_Fuction(query):
    if "oink" in query or "ink" in query or "link" in query:
        playsound("oink_boop.mp3")

your_Mic_Fuction()

我试过这个:

    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")

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

相关问题 更多 >