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.
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.
I have been working on converting speech to text using speech_recognition library on my Jupyter Notebook. All of a sudden, the code throws an error and I have no idea where to even start to solve this. The code is shown below:
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile('/audio1659122935.wav') as source:
audio_text = r.record(source)
try:
txt = r.recognize_google(audio_text)
print(text)
except:
print('Error... Try again...')
This generates the except message "Error... Try again..."
Did anyone have the same issue with using speech_recognition?
The speech_recognition version is 3.8.1 and my python version is 3.8.5
It was just a simple mistake using the wrong variable text. Also just unwrap the try except clause to see where the error occurs.
Hi im currently looking for the easiest possible python voice assistant. I watched some to tutorials and managed it to write this code:
import speech_recognition as sr
speech_engine = sr.Recognizer()
def from_mic():
with sr.Microphone() as micro:
print("Recording...")
audio = speech_engine.listen(micro)
print("Recognizing...")
text = speech_engine.recognize_google(audio, language="de-DE")
return text
print(from_mic())
I am sure that listen() normally should stop when I stop speaking, but my programm just runs and says Recording... it does not try to figure out what has been said. It can also be because of my bad headset which makes white noise but i dont think so. But i dont find any mistake in the code
Use:
speech_engine.adjust_for_ambient_noise(micro)
before listening.
This answer was posted as an edit to the question SOLVED: Why is my Python speechRecognition.listen not stopping to record by the OP Devolper12345 under CC BY-SA 4.0.
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.
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 :)