adjust_for_ambient_noise gives timeout error in silence - python

I am using following code for simple speech recognition. This code detects spoken phrase if there is silence and r.adjust_for_ambient_noise(source, duration=1) line is commented.
If there is background noise then have to enable r.adjust_for_ambient_noise(source, duration=1)
The problem is, if there is silence and r.adjust_for_ambient_noise(source, duration=1) is enabled then I get "WaitTimeoutError: listening timed out while waiting for phrase to start" from r.listen
Please suggest what combinations of r.listen and r.adjust_for_ambient_noise and parameters should be used, so that if someone speaks "tommy" then the code breaks out of loop.
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone() #device_index=1)
sr.Microphone.list_microphone_names()
text = ""
while text.lower() != "tommy":
with m as source:
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source, timeout=3, phrase_time_limit=3)
text = r.recognize_google(audio)
print(text)

Related

unable to use speech recognition in python

I am trying to make a virtual assistant for which I need the speech recognition module but it's showing a timeout error I tried to increase the timeout manually to 5 seconds yet the issue prevails.
import speech_recognition as sr
import pyaudio
import sounddevice as sd
a = sd.query_devices()
print (a)
r = sr.Recognizer()
with sr.Microphone(device_index=2) as source:
print('say....')
audio = r.listen(source)
voice_data = r.recognize_google(audio)
print(voice_data)
The error shown is as follows
Traceback (most recent call last):
File "/Users/vipulvasant/Desktop/my codes/ABP/main.py", line 10, in <module>
audio = r.listen(source)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/speech_recognition/_init_.py", line 618, in listen
raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start
this is the first step towards my dream of making my own virtual assistant
Adding a simple a while True: statement should fix the issue.
As i ran into the same issue and this was my solution.
import speech_recognition as sr
import pyaudio
import sounddevice as sd
a = sd.query_devices()
print (a)
r = sr.Recognizer()
while True:
with sr.Microphone(device_index=2) as source:
print('say....')
audio = r.listen(source)
voice_data = r.recognize_google(audio)
print(voice_data)
Here try this. I don't know if this is what you are asking for but I hope it helps:
import speech_recognition
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak:")
audio = r.listen(source)
voice_data = r.recognize_google(audio)
print(voice_data)

When running Python code, my voice is not recognized

I use this code to take from a YouTube video, but it doesn't work for me. I run the code, say something and nothing happens, the program just keeps running.
import speech_recognition as sr
r = sr.Recognizer()
while True:
try:
with sr.Microphone() as mic:
r.adjust_for_ambient_noise(mic, duration=0.2)
audio = r.listen(mic)
text = r.recognize_google(audio)
text = text.lower()
print(f"Recognized {text}")
except sr.UnknownValueError():
r = sr.Recognizer()
continue
Information that may be useful:
-Windows 10
-PyCharm(Python3-10-1)
-Pyaudio(0.2.11)
-I use a laptop
-In the menu "Hidden icons" shows that "The microphone is used by Python"
When I run my code in PyCharm:
When I run python -m speech_recognition
You might wanna try the minimal example and go from there:
#!/usr/bin/env python3
import speech_recognition as sr
# get audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as 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))

speech_recognition not recognizing my voice or anything of that sort

I am creating a project that requires the SpeechRecognition library and I have it all installed and whatnot but everytime I run the program... the program doesn't transcribe what I am saying and it doesn't throw any errors either.
Here Is My Code
import speech_recognition as sr
r=sr.Recognizer()
print(sr.Microphone.list_microphone_names())
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source,duration=1)
# r.energy_threshold()
print("say anything : ")
audio= r.listen(source)
try:
text = r.recognize_google(audio)
print(text)
except:
print("sorry, could not recognise")
Output
['Background Music', 'Background Music (UI Sounds)', 'MacBook Air Microphone', 'MacBook Air Speakers', 'Iriun Webcam Audio', 'EpocCam Microphone']
say anything :

Mac Catalina / Python 3.8.3 / Speech Recognition / Cannot get mic input to VS Code while the code works fine from Terminal

I am trying to work on SpeechRecognition. When I run the code on Visual Stuido Code it doesn't listen, when I run the code from Mac Terminal it receives audio input and works fine... I tired to drag&drop VS to mic permissions. I cannot make it work anyway...
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
except Exception as e:
print('Exception', str(e))
return said.lower()
print("Speak now!")
print(get_audio())

SpeechToText conversion using Python - SpeechRecognition and PyAudio

The terminal executes the code but its stuck at "Start Processing..."
This used to work first. I have tested it. But now after a few months there seems to be some issue
import speech_recognition as sr
import pyaudio
r = sr.Recognizer()
with sr.Microphone() as source:
print('Start speaking...')
audio=r.listen(source)
try:
text=r.recognize_google(audio)
print('The text is: {}'.format(text))
except:
print('Audio is not audible!!')

Categories