When running Python code, my voice is not recognized - python

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))

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)

Cannot get audio through speech_recognition python by any method?

I am try to make SpeechRecognition 3.8.1 listen to my voice for four days. I have already seen the following stuff in the internet:
https://github.com/Uberi/speech_recognition/issues/20
https://www.geeksforgeeks.org/voice-assistant-using-python/
https://pythonrepo.com/repo/Uberi-speech_recognition-python-audio
sudo apt-get install python-pyaudio python3-pyaudio does not work for me.
speech recognition python code not working
Errors on PyAudio on Visual studio code Python
I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."?
https://stackoverflow.com/questions/50424902/speechrecognition-python-package-does-not-listen?r=SearchResults&s=4|89.4428
https://newbedev.com/speech-recognition-python-code-not-working-code-example
https://www.py4u.net/discuss/22062
I have gone through many more discussions, ALL IN VAIN.
No solution worked for me till now. Please help!!
After a lot of unsuccessful attempts, I have at least managed to install pyaudio with the command pipwin install pyaudio
my python code:
import os
import pyttsx3, datetime, pyaudio
import speech_recognition as sr
# Initial Setup for pyttsx3 - speaking abilities
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id) # 0-male voice , 1-female voice
sr.Microphone.list_microphone_names()
# Initial Setup for speech_recognition - listening abilities
# r.energy_threshold = 10
# print(pyaudio.get_device_count() - 1)
def speak(speakable):
"""speak() takes a string and reads it loud"""
engine.say(str(speakable))
engine.runAndWait()
def takeCommand():
pyaudio.PyAudio()
r = sr.Recognizer()
"""It takes microphone input from the user and returns string output"""
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=0.9)
print("Listening...")
r.pause_threshold = 45
audio = ""
try:
audio = r.listen(source)
print("Recognizing...")
except Exception as e:
print("Listen err: ", e)
try:
print("Recognizing...")
query = r.recognize_google(audio)
print(f"User said: {query}\n") # User query will be printed.
except sr.UnknownValueError as e:
print("Say that again please...")
return "None" # None string will be returned
except Exception as err:
print("Check your internet...")
return "None"
return query
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Good Morning!")
elif hour >= 12 and hour < 18:
speak("Good Afternoon!")
else:
speak("Good Evening!")
speak(
"Hello Sir, I am Friday, your Artificial intelligence assistant. Please tell me how may I help you"
)
if __name__ == "__main__":
os.system("CLS")
while True:
command = takeCommand().lower()
print(f"Command: {command}")
if "wish" in command:
wishMe()
the output gets stuck at Listening.... Nothing happens after that.
More Info:
I am using Windows 10 Home
Code editor- vs code
I am not using virtual env in this this program.
I have also checked that Chrome Voice Search is working without any trouble.
result of the python3 -m speech_recognition command
Did you try with the python3 -m speech_recognition? You should see something like:
...
A moment of silence, please...
Set minimum energy threshold to 51.208131879528096
Say something!
Got it! Now to recognize it...
You said hello
If that does not work, you probably have something wrong with your audio system. Make sure that you can record your audio (https://onlinehardwaretest.com/microphone-test/) and consider rebooting your system.

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