使用我当前的SpeechRecognition程序,它会听我的句子,然后尝试识别它。
如果关键字是“ hello”,而我的句子是“ hello there etc ...”,我希望程序在听到“ hello”后立即调用一个函数,所以不要等到句子结尾并处理它。如何制作能够实时识别语音的程序?或者解决该问题的最佳方法是什么?
import speech_recognition as sr
r = sr.Recognizer()
r.pause_threshold = 1.0
r.phrase_threshold = 1.0
r.non_speaking_duration = 1.0
keyWord = 'hello'
with sr.Microphone() as source:
print('Please start speaking..\n')
while True:
print('wait 3 seconds')
r.adjust_for_ambient_noise(source, duration=3)
print('go')
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(text)
if keyWord.lower() in text.lower():
print('Keyword detected in the speech.')
except Exception as e:
print('Please speak again.')
非常感谢您的帮助。