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()
I have used this program with the exact same libraries. This is assuming you have a microphone of some sort. If this doesn't work, its most likely background noise it is trying to detect as an input which in that case, you would need to add r.adjust_for_ambient_noise(source, duration=1) Tell me if this code below works...
engine = pyttsx3.init('sapi5') #Only use sapi 5 if it is Windows 10
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #This bit goes below the imports
def speak(audio):
engine.say(audio) #This bit right here goes below the above
engine.runAndWait()
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-us')
print("User said: {query}\n")
except Exception as e:
print(e)
print("Google was unable to hear")
return "None"
return query
I hope this works for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With