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)
Related
I was trying to convert speech to text via pyaduio.The program was working fine for a few hours but suddenly my visual studio 2019 keeps on marking an error in the 6th line for no reason.
Does anyone know how to fix it? tnx :)
import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
with mic as source:
audio_data = r.record(source, duration= 3)
print("I recognize speaking")
text = r.recognize_google(audio_data)
print("I heard: ",text)
The problem was that google api is restricted to certain amount of uses without paying, and I ran out of free uses.
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.
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.
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.
I made a program which allows me to speak and converts it to a text. It converts my voice after I stopped talking. What I want to do is to convert my voice to text while I am talking.
https://www.youtube.com/watch?v=96AO6L9qp2U&t=2s&ab_channel=StormHack at min 2:31.
Pay attention to top right corner of Tony's monitor. It converts his voice to text while talking. I want to do the same thing. Can it be done?
This is my whole program:
import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("You said : {}".format(text))
except:
print("Sorry could not recognize what you said")
solution, tips, hints, or anything would be greatly appreciated, thank you in advance.
In order to do this you will have to do what's called VAD: Voice Audio Detection, a simple way to do this is take a set of samples from the audio and grab their intensity, if they are above a certain threshold then you should begin recording, once the intensity falls below a certain threshold for a given period of time then you conclude the recording and send it off to the service. You can find an example of this here.
More complex systems use better heuristics to decide whether or not the user is speaking, such as the frequency as well as applying things like noise reduction, other systems are also able to perform live speech to text as the user is speaking like DeepSpeech 2.
To do what you want, you need to listen not to a complete sentence, but for just a few words. You then have to process the audio data and to finally print the result. Here is a very basic implementation of it:
import speech_recognition as sr
import threading
import time
from queue import Queue
listen_recognizer = sr.Recognizer()
process_recognizer = sr.Recognizer()
audios_to_process = Queue()
def callback(recognizer, audio_data):
if audio_data:
audios_to_process.put(audio_data)
def listen():
source = sr.Microphone()
stop_listening = listen_recognizer.listen_in_background(source, callback, 3)
return stop_listening
def process_thread_func():
while True:
if audios_to_process.empty():
time.sleep(2)
continue
audio = audios_to_process.get()
if audio:
try:
text = process_recognizer.recognize_google(audio)
except:
pass
else:
print(text)
stop_listening = listen()
process_thread = threading.Thread(target=process_thread_func)
process_thread.start()
input()
stop_listening()
As you can see, I use 2 recognizers, so one will always be listening and the other will process the audio data.
The first one listens to data, then adds the audio data to a queue and listens again. At the same time, the other recognizer is checking if there is audio data to process into some text to then print it.