Reputation: 5
import speech_recognition as sr
r= sr.Recognizer()
with sr.Microphone() as sourse:
print("Say something: ")
audio=r.listen(sourse)
try:
text =r.recognize_google(audio)
print("You said: {}".format(text))
except:
print("Sorry")
I am facing problem in speech recognition using python the response time is very slow AND SOMETIME it does not respond.
Upvotes: 0
Views: 599
Reputation: 43
This can work better with these modifications:-
import speech_recognition as sr
def recog():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say Something")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
print("Recognizing..")
text = r.recognize_google(audio, language='en-in') # Specify Language Code
print("You said {}".format(text))
except Exception as e:
print(e)
print("Sorry")
recog()
try this.
Upvotes: 0
Reputation: 52
Now, this may not work for you depending on where you are but it helped me, basically how the Speech Recognition works is that it waits until it hears something. After a while, if there is no sound for a little after something has been said, Speech Recognition will Assume that what you said is what it was supposed to hear.
You're missing a crucial part of code which is:
r.adjust_for_ambient_noise(source)
What this does is that it takes a sample of the ambient noise in the background (takes max, about a second, depending on noise level) and then when it's listening to the User it won't be listening to anything in the background that is causing ambient noise. So in your case, it's taking your code about 10-20 minutes to stop listening without r.adjust_for_ambient_noise(source)
.
In the end, your code should be something like this:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
print("Say something!")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("You said: {}".format(text))
except:
print("Sorry")
Upvotes: 2