i have this problem with Speech Recognition. When the user gave the input, it ignore it and ask again.
Here's the code
def getVoice():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening: ")
r.pause_threshold = 1
aud = r.listen(source)
try:
query = r.recognize_google(aud, language= "en-US")
print(f"You said: {query}\n")
except sr.UnknownValueError:
print(f"Please {name}, try again.\n")
query = None
return query
try this one
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening: ")
text = r.listen(source)
try:
recognised_text = r.recognize_google(text)
print(recognised_text)
except sr.UnknownValueError as uve:
print(f"unknownvalueerror occurred: {uve}")
except sr.RequestError as e:
print(f"Requesterror occured: {e}")
Related
import speech_recognition as sr
r = sr.Recognizer()
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except:
pass
return text
text = speaking()
if text == 'hello':
print("You said hello")
So yeah the code works fine. But whenever speech_recognition hears sounds like me clearing the throat/laughing/literally any ambient noise, I get an error. Can you tell me why and tell a way to correct it?
your problem.
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except: # here its the problem
pass
return text # and here
the code should look like this
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
# or you can return text here
# and get rid of the below else block
# return text
except:
# if you get an error then do nothing
pass
# you can return None
return None
else:
# but if the try code worked then return text
return text
ofcourse you get "referenced before assignment error" because in your old code if you get an error you do nothing with it and after that you reference text, but text isnt going to be defined if you get an exception just when you are recording.
def TakeCommand():
r = sr.Recognizer
mic = sr.Microphone()
with mic as source:
print("I am listening ....")
audio = r.listen(source)
try:
print("Recognizing....")
query = r.recognize_google(audio, language = 'en-US')
print(query)
except Exception as exc:
print(exc)
print("Sorry, I didn't recognized it. Please repeat...")
TakeCommand()
I am getting an error on the line ''audio = r.listen(sourse)' It is says, let me quote 'listen() missing 1 required positional argument: 'source' ''
And in case the link to the image:
[1]: https://i.stack.imgur.com/wEYOt.png
Try changing r = sr.Recognizer to r = sr.Recognizer().
when i tried to run this code
import speech_recognition as sr #importing sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("speak into mic")
audio = r.listen(source,timeout=2)
try:
print("Transcription:" + r.recognize_google(audio))
except sr.UnknownValueError:
print("Audio Unintelligible")
except sr.RequestError as e:
print("cannot obtain results : {0}".format(e))
except sr.WaitTimeoutError as k :
print("time out") #error handler for time out error
when i run the above code it giving error like this
speak
into mic
Traceback (most recent call last):
File "C:/Users/punna/PycharmProjects/alex/alex.py", line 6, in <module>
audio = r.listen(source,timeout=2)
File "C:\Users\punna\Anaconda3\lib\site-packages\speech_recognition\__init__.py", line 544, in listen
raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start
I wrote exception but it is again giving error
can anyone help me
This might help I have just added the return "none" Statement so that it returns a none if something goes wrong and I also added phrase_time_limit to 5 and query = r.recognize_google(voice,language='en-in') this statement to recognize the words spoken by the user into a variable instead of this statement print("Transcription:" + r.recognize_google(audio)) then it worked try this might help you
r = sr.Recognizer()
with sr.Microphone() as audio:
speak('Listening...')
r.pause_threshold = 1
voice = r.listen(audio,timeout=1,phrase_time_limit=5)
try:
print("Thinking...")
query = r.recognize_google(voice,language='en-in')
print("Transcription:"+query)
except Exception as e:
print("I am Sorry There is an error while i am recognizimg your command")
return "none"
return query
It is because you kept the timeout for a limited amount of time. Try without the timeout or keep the timeout at least more than 5
def takecommand():
try:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listing.......")
r.pause_threshold = 1
audio = r.listen(source, timeout=1, phrase_time_limit=5)
except Exception as e:
speak("error in my files sir restarting")
speak("completed")
return "none"
try:
print("Recognize......")
qureay = r.recognize_google(audio, language='en-in')
print("user said:" +qureay)
except Exception as e:
speak("Say That Again Please...")
return "none"
return qureay
I am creating a voice assistant in python and I would like to know how I can make a loop so that every time the assistant responds, the microphone is activated so I can ask him again
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
#Here the function activates
text = get_audio()
#This is a example question
if 'hello computer' in text:
speak('Hi! How are you?')
#This is where I want to turn on the microphone again, process my voice and say the answer and so on to infinity
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
This is how you keep your microphone turned on:
while True:
# Here the function activates
text = get_audio().lower
# These are examples
if 'hello computer' in text:
speak('Hi! How are you?')
elif "good morning" in text:
speak("Hey good morning")
I want my python chatbot to start listening when I say "Echo". How do I do that? A snippet of chatbot is below.
import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source)
try:
x=r.recognize_google(audio)
print(x)
except sr.UnknownValueError:
pass
except sr.RequestError as e:
pass
while running==True:
r = sr.Recognizer()
with sr.Microphone() as source:
while 1:
Speech()
After hit and trials, I got it right. But I find it slow. If you have better strategy, please comment.
import speech_recognition as sr
running=True
r = sr.Recognizer()
def Speech():
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("Say something!")
audio = r.listen(source)
try:
x=r.recognize_google(audio)
if x=="hello":
print("Speak up")
audio = r.listen(source)
print(r.recognize_google(audio))
except sr.UnknownValueError:
pass
except sr.RequestError as e:
pass
while running==True:
Speech()