雪童热词检测问题

2024-10-01 19:15:27 发布

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

嗨,伙计们,我正试着在我的代码中触发while循环,每当有热词“处女座”出现时,它就会启动语音识别。问题是snowboy检测到了hotword,但我不知道如何在hotword被触发后执行while循环。有什么帮助吗?这听起来可能很愚蠢,应该相对容易些,但我的大脑现在正处于燃烧状态。谢谢您!在

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

Tags: instancetextimportsourceascallbackdetectoraudio
1条回答
网友
1楼 · 发布于 2024-10-01 19:15:27

你的while循环总是被“触发”,给定TRUE,直到你break离开它。要触发循环中的代码,请执行以下操作,例如:

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

相关问题 更多 >

    热门问题