Python pyaudio error while recording from microphone - python

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.

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.

Why is my Python speechRecognition.listen not stopping to record

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.

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.

python module speech_recognition recognized only first 5 seconds

guys, i have a problem with speech recognition on python3.x
I using module speech_recognition.
My .wav file have length 5 minutes, however python recognized only first 5 seconds of this file.
I have never worked with this module.
Please, help me
My code
import speech_recognition as sr
r = sr.Recognizer()
with sr.AudioFile(r"C:\Users\User\Downloads\Unlocking.wav") as source:
audio = r.record(source)
text = r.recognize_google(audio)
print(text)

Categories