I am currently using the SpeechRecognition library, and when I do audio = speech.listen(source), it seems like it stalls. There is no execution of code after it. I remove ambient sound, but it still seems to stall and have no effect on what my mic is picking up. Microphone works when I look at my computer's settings and it is unmuted.
I create an pyttsx3 instance called engine, and a sr.Recognizer() instance called speech. I then call getVoiceCommand() to get some audio from the user(me) which goes to print("Listening...") and then it stalls.
The code after audio = speech.listen(source) does not execute, and when the timeout of 10 seconds is done, it gives me the WaitTimoutError, which hints out to me that it never picked up a sound? I tried changing the energy_threshold for values between 50 and 4000 and still didn't pick up any noise.
import speech_recognition as sr
import pyttsx3
import pyaudio
def speak(text):
engine.say(text)
engine.runAndWait()
def getVoiceCommand():
voice_text = ''
try:
with sr.Microphone() as source:
speech.adjust_for_ambient_noise(source)
print("Listening...")
#speech.energy_threshold = 4000
print(speech.energy_threshold)
audio = speech.listen(source, timeout=10)
print("Stopped")
voice_text = speech.recognize_google(audio, language='en-US')
except sr.UnknownValueError:
print("Unknown value error")
except sr.RequestError as e:
print('Network error.')
except sr.WaitTimeoutError:
print(audio)
print("Wait timeout error")
print(voice_text)
engine = pyttsx3.init()
engine.setProperty('voice', voice_id) # voice_id is chosen voice
rate = 220
engine.setProperty('rate', rate)
speech = sr.Recognizer()
getVoiceCommand()