gpt4 book ai didi

Python语音识别在按下按钮时listen_in_background?

转载 作者:行者123 更新时间:2023-12-01 09:15:34 31 4
gpt4 key购买 nike

目前,当用户按下用 py qt 制作的按钮时,我正在尝试让语音识别模块在后台监听。我在类中有初始化和回调方法,但是当我尝试监听麦克风时,它似乎没有注册任何内容,甚至没有输出错误。

class VoiceRecognitionWidget(ScriptedLoadableModuleWidget):
def callback(self, recognizer, audio):
try:
print(recognizer.recognize_google(audio))
# handles any api/voice errors errors
except sr.RequestError:
print( "There was an issue in handling the request, please try again")
except sr.UnknownValueError:
print("Unable to Recognize speech")

def onApplyButton(self):
#self.displayLabel.setText("Listening for speech....")

self.recognizer = sr.Recognizer()
try:
self.microphone = sr.Microphone()

except(IOError):
print("ERROR: No default microphone. Check if microphone is plugged in or if you have a default microphone set in your sound settings.")
self.errors.setText("ERROR: No default microphone. Check if your microphone is plugged in or if you have a default microphone set in your sound settings.")

with self.microphone as source:
self.recognizer.adjust_for_ambient_noise(source)
# audio = self.recognizer.listen(source)

stop_listening = self.recognizer.listen_in_background(self.microphone, self.callback)

当我按下按钮时,麦克风可以正常收听,只是当我尝试让它连续收听时,它似乎没有做任何事情。我还尝试在初始化整个 GUI(未显示)的同一类中的函数中创建识别器和麦克风,但这也不起作用。任何帮助将不胜感激。

最佳答案

在回调函数中,我放置了 self.recognizer.recognize_google(audio) 而不管 recognizer 参数。当然,该类需要有一个 recognizer = sr.Recognizer()

关于Python语音识别在按下按钮时listen_in_background?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51306399/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com