嗨,我正在尝试在代码中触发while循环,以便每当说出“处女座”这个热门单词时就开始语音识别。问题是snowboy可以检测到该热门单词,但是一旦触发该热门单词,我就不知道如何执行“ 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
答案 0 :(得分:0)
在给定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