目前,我正在尝试让用户在用户按下使用 py qt 制作的按钮时让speech_recognition模块在后台侦听。我在类中有初始化和回调方法,但是当我尝试听麦克风时,它似乎没有注册任何东西,甚至没有输出错误。
import time
import speech_recognition as sr
def callback(recognizer, audio):
try:
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
r = sr.Recognizer()
m = sr.Microphone()
with m as source:
r.adjust_for_ambient_noise(source)
stop_listening = r.listen_in_background(m, callback)
for _ in range(50): time.sleep(0.1)
stop_listening(wait_for_stop=False)
while True: time.sleep(0.1) ```
i want data to be sent the response as soon as it hears a word. the response need to be printed in the console
使用以下命令定义默认麦克风:
m = sr.Microphone(0)
这为我解决了无响应问题,似乎将麦克风索引强制为默认值,即使 sr 应该自动选择它。