Reputation: 53
Currently I'm trying to have the speech_recognition module listen in the background when the user pushes a button made with py qt. I have the initialization and callback methods in a class, however when I try and listen to the microphone it doesn't seem to be registering anything, not even outputting errors.
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)
The microphone works when I just listen to it normally when I press the button, it's just that when I try to have it continuously listen it doesn't seem to do anything. I also tried creating the recognizer and microphone in a function in the same class that initializes the whole GUI (not shown) but that didn't work either. Any help would be appreciated.
Upvotes: 2
Views: 3817
Reputation: 11
On callback function I put self.recognizer.recognize_google(audio)
regardless the recognizer
parameter. For sure, the class need to have a recognizer = sr.Recognizer()
Upvotes: 1