Python speech to text using speechrecognition - python

I am making a software which uses speech to text. I have read some article and post on stackoverflow. I tried this code-
r = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
r.adjust_for_ambient_noise(source)
print("Speak:")
audio = r.listen(source)
try:
print("You said " + r.recognize_google(audio)+';')
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
After I run the code when it reaches this part it makes the input phase and stops. It does nothing and tkinter window Doesn't respond. It looks like this.
I tried both r.recognize_google and r.recognize_sphinx. I also tried not using microphone index. I can't understand what is happenning. When it comes to r.listen(source) it stops. Thanks for your advice.

You can try this code in GitHub.

Try to replace
audio=r.listen(source)
To
audio=r.record(source, duration=4)
You can change the value of duration.
Let me know if it works :)

Related

Python speech recognition: recognize_google with UnknownValueError for Microphone

my question is about the speech recognition using Python. My code is supposed to listen to what I say to the microphone (having 5 seconds to say my message) and then print out whatever it understood.
import speech_recognition as sr
r = sr.Recognizer()
mic = sr.Microphone()
with mic as audio:
print("Speak Please")
r.adjust_for_ambient_noise(audio)
audio = r.record(audio, duration=5)
print("Converting Speech to Text...")
print("You said: " + r.recognize_google(audio))
But I always get the error message:
File "/opt/anaconda3/lib/python3.8/site-packages/speech_recognition/__init__.py", line 780, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError
I tried to use audio files I found online and r.recognize_google worked fine. But once I use my microphone I always get an error.
I also tried switching between "record" and "listen", without any success.
Has anybody encountered a similar problem and knows what might be wrong?
Edit:
Using an except block like:
try:
print("You said: {}".format(r.recognize_google(audio,language='en-USA')))
except:
print("Couldn't hear you")
Also does not help, it just throws the exception every time.
Any help is appreciated.
Thank you very much!
So I managed to solve the problem. Apparently it's a problem with Visual Studio Code. I had to run the code from the terminal, and now it is working fine. Im not sure why this is the case, but I'm happy that it is working now.

How to recieve microphone input with Speech Recognition in python properly

I have a python program below. I want to take microphone input and get the text from the input. When I run the program, it always stops at line 9, "audio_text = r.listen(source)" and doesn't continue. It seems that it keeps waiting for microphone input to stop even though I have stopped talking. I know my microphone works because I recorded a file and played it back using aplay. Does anyone know why my program gets stuck at that line?
import speech_recognition as sr
print("Program has started!")
r = sr.Recognizer()
with sr.Microphone() as source:
print("Talk")
audio_text = r.listen(source)
print("Time over, thanks")
try:
# using google speech recognition
print("Text: "+r.recognize_google(audio_text))
except:
print("Sorry, I did not get that")
I am running this on Raspberry pi os and using python3.
try adding these line to stop the recording if your mic doesn't stop listening
Stop_listening=r.listen_in_background(source,callback)
Stop_listening(wait_to_stop=True)

Speech is not recognized via microphone in python speechRecognition module

When i run this program the speech is recognized if I use a headset or an external microphone.
But, if I use the laptop microphone( Microphone Array (Realtek(R) Audio) ) the speech is not recognized. It's like the program hangs at the line audio = r.listen(source) If I say something and then plug in the headset then the program works.
The microphone in the laptop is working perfectly.
import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening......")
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"USER: {query}\n")
except Exception:
print("Did not catch that")
Why id this happening? Can somebody help me out please?
Thank you.
r.adjust_for_ambient_noise(source)
I used this function and it's working now.
This increases the range to recognize the audio.
Thank you everyone.
I will guess.
Probably it uses external microphone as default device and you have to manually set other device.
In documentation you can see
Microphone(device_index = None)
And
A device index is an integer between 0 and pyaudio.get_device_count() - 1
You can also see how to get list of all available devices.
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
BTW: You can also read Troubleshooting - maybe it gives more ideas.

Python: Get system audio in speech recognition instead of microphone

I am working on speech recognition in python, but it is only getting the input from Micropohone. How is it possible to give the audio from speakers as input to the speech recognition library?
The piece of code is given below:
import speech_recognition as sr
with sr.Microphone() as source: # using microphone here, would like to use speaker instead
print("Talk Something...")
audio=r.listen(source)
print("Time Over...")
import time
try:
t1=time.time()
print("Text: "+r.recognize_google(audio)) # prints after converting speech to text
t2=time.time()
print("T2-T1: ", t2-t1)
except:
print("Didn't understand the audio")
I have been struggling here for so long and any help will be much appreciated. Thanks!
You can configure device index as in docs:
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))
If LINEIN is not available as a separate input, you might just configure it as a recording source in audio properties.

Enabling Audio Input for Speech Recognition Library

How do I turn on audio input for all device indexes using a Speech Recognition Library? As I want to pass in the audio for testing and there might be possibility that the library uses a different audio input device. How do I let it take the audio input from all the indexes?
You can use your microphone as a default audio input device below is a code snippet:
import speech_recognition as sr
r=sr.Recognizer() # this is a recognizer which recognize our voice3
with sr.Microphone() as source: # in this we are using a microphone to record our voicecmd
speak.speak("What can i do for you!") # this a speak invoke method w3hich ask us something
print("Ask me Something!") # this a print statement which come on console to ask something
audio=r.listen(source,timeout=60,phrase_time_limit=3)
data = ""
try:
"""
this is a try block it will recognize it our voice and say what we have told
"""
data= r.recognize_google(audio,language="en-US")
print("dynamo think you said!" + " "+data) # this will print on your console what will going to recognize by google apis
except:
"""
this is a except block which except the error which come in try block and the code is not able to run it will pass a value
"""
print("not able to listen you or your microphone is not good")
exit()
First, You require the following things installed on your system.
1. Python
2. Speech Recognition Package
3. PyAudio
Now, You can run this Code for know your Version
import speech_recognition as s_r
print(s_r.__version__)
Output
3.8.1
It will print the current version of your speech recognition package.
Then, Set microphone to accept sound :
my_mic = s_r.Microphone()
Here you have to pass the parameter device_index=?
To recognize input from the microphone you have to use a recognizer class. Let’s just create one.
r = s_r.Recognizer()
Now, I Convert the Sound Speech into Text In Python
To convert using Google speech recognition we can use the following line:
r.recognize_google(audio)
It will return a string with some texts. ( It will convert your voice to texts and return that as a string.
You can simply print it using the below line:
print(r.recognize_google(audio))
Now the full program will look like this:
import speech_recognition as s_r
print(s_r.__version__) # just to print the version not required
r = s_r.Recognizer()
my_mic = s_r.Microphone(device_index=1) #my device index is 1, you have to put your device index
with my_mic as source:
print("Say now!!!!")
audio = r.listen(source) #take voice input from the microphone
print(r.recognize_google(audio)) #to print voice into text
If you run this should you get an Output.
But after waiting a few moments if you don’t get any output, check your internet connection.

Categories