speech recognition python code not working - python

I am running the following code in Python 2.7 with pyAudio installed.
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source: # use the default microphone as the audio source
audio = r.listen(source) # listen for the first phrase and extract it into audio data
try:
print("You said " + r.recognize(audio)) # recognize speech using Google Speech Recognition
except LookupError: # speech is unintelligible
print("Could not understand audio")
The output gives a blinking pointer. That's it.

The possible reason could be that the recognizer_instance.energy_threshold property is probably set to a value that is too high to start off with. You should decrease this threshold, or call recognizer_instance.adjust_for_ambient_noise(source, duration = 1). You can learn more about it at Speech Recognition

I have solved the same problem for me with the following (noise suppression),
The "listen" function has problems with environment noise. So the running code is only blinking waiting.
Use this ambient noise suppression/adjustment;
r.adjust_for_ambient_noise(source, duration=5)
Referance
Tuesday, March 28, 2017
Easy Speech Recognition in Python with PyAudio and Pocketsphinx

try to add
r.adjust_for_ambient_noise(source,duration=1)
where r is recogniser instance, like this
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")

have you tried replacing
print("You said " + r.recognize(audio))
except LookupError:
print("Could not understand audio")
with
text = r.recognize_google(audio)
print("You said : {}".format(text))
text = r.recognize_google(audio)
except:
print("Sorry could not recognize your voice")
ensure pyaudio.h is installed by running the below command
sudo apt-get install portaudio19-dev python-pyaudio python3-pyaudio

just try;
pip install sounddevice
it works.

check the input volume of your microphone. It is by default set to 0 in ubuntu (in my case). Since your program got stuck on the line audio = r.listen(source), which simply means that the microphone is not able to listen to any voice input. Hope this helps.

try this code:
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source=source)
audio = r.listen(source,timeout=3)
data = ''
try :
data = r.recognize_google(audio)
print(data)
except sr.UnknownValueError:
print(" Error")
except sr.RequestError as e:
print("Request Error")
or add timeout and r.adjust_for_ambient_noise(source=source) to your code as above.please can anyone help me with this

In addition to Tushar's answer, I suggest trying a nicer external USB microphone. PyAudio can have issues with a simple built-in laptop microphone.

You may have to install these things first:
pip install pyaudio
pip install --upgrade pyaudio
pip install wheel
pip install google-api-python-client
sudo apt-get install flac
pip install monotonic
pip install SpeechRecognition
After that, refer the site (https://realpython.com/python-speech-recognition/)
it will clearly explain what you wanted.
Here I am attaching the code I've edited from that site. Since I am new it will not be perfect, but I've tried. This is to check weather the voice input is similar to the text I've given and also it will print what you said.
#!/usr/bin/ python
import time
import speech_recognition as sr
def recognize_speech_from_mic(recognizer, microphone):
"""Transcribe speech from recorded from `microphone`.
Returns a dictionary with three keys:
"success": a boolean indicating whether or not the API request was
successful
"error": `None` if no error occured, otherwise a string containing
an error message if the API could not be reached or
speech was unrecognizable
"transcription": `None` if speech could not be transcribed,
otherwise a string containing the transcribed text
"""
# check that recognizer and microphone arguments are appropriate type
if not isinstance(recognizer, sr.Recognizer):
raise TypeError("`recognizer` must be `Recognizer` instance")
if not isinstance(microphone, sr.Microphone):
raise TypeError("`microphone` must be `Microphone` instance")
# adjust the recognizer sensitivity to ambient noise and record audio
# from the microphone
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
# set up the response object
response = {
"success": True,
"error": None,
"transcription": None
}
try:
response["transcription"] = recognizer.recognize_google(audio)
except sr.RequestError:
# API was unreachable or unresponsive
response["success"] = False
response["error"] = "API unavailable"
except sr.UnknownValueError:
# speech was unintelligible
response["error"] = "Unable to recognize speech"
return response
if __name__ == "__main__":
NUM_GUESSES = 1
PROMPT_LIMIT = 2
# create recognizer and mic instances
recognizer = sr.Recognizer()
microphone = sr.Microphone()
word = "hello world"
time.sleep(3)
for i in range(NUM_GUESSES):
for j in range(PROMPT_LIMIT):
print('Guess {}. Speak!'.format(i+1))
guess = recognize_speech_from_mic(recognizer, microphone)
if guess["transcription"]:
break
if not guess["success"]:
break
print("I didn't catch that")
# if there was an error, stop the game
if guess["error"]:
print("ERROR: {}".format(guess["error"]))
break
# show the user the transcription
print("You said: {}".format(guess["transcription"]))
# determine if guess is correct and if any attempts remain
guess_is_correct = guess["transcription"].lower() == word.lower()
user_has_more_attempts = i < NUM_GUESSES - 1
if guess_is_correct:
print("Correct!".format(word))
break
elif user_has_more_attempts:
print("Incorrect. Try again.\n")
else:
print("Sorry, output is not similar to '{}'.".format(word))
break

MY Problem was : the program is running without any error but its not showing any output
THIS IS THE CODE WITH PROBLEM:
import googletrans
import speech_recognition as sr
recognizer = sr.Recognizer()
translator = googletrans.Translator()
try:
with sr.Microphone() as source:
print('Speak Now')
voice= recognizer.listen(source)
text= recognizer.recognize_google(voice)
print(text)
except:
pass
translated = translator.translate(text, dest='es')
print(translated.text)
To Solve this problem :
basically this problem occurs by the background noise or ambient noise
SO just add only one line to your code which is :
recognizer.adjust_for_ambient_noise(source)#recognizer is on line 4 from above code
########
The improved and Solved problem code:
########
import googletrans
import speech_recognition as sr
recognizer = sr.Recognizer()
translator = googletrans.Translator()
try:
with sr.Microphone() as source:
print('Speak Now')
recognizer.adjust_for_ambient_noise(source)#(Problem Solved)
voice= recognizer.listen(source)
text= recognizer.recognize_google(voice)
print(text)
except:
pass
translated = translator.translate(text, dest='es')
print(translated.text)

I guess the problem is with the duration. If you set some duration to the speech recognition engine the problem will be resolved.
Try the following code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
speak(sen)
print("listening...")
audio = r.record(source,duration=3)
try:
str=r.recognize_google(audio)
print(str)
except:
print("some error occurred!")

Put the Try and Except inside the indentation.
Here is my Working code:-
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say Something")
audio=r.listen(source)
try:
print(r.recognize_google(audio),"\n")
except:
pass

Please set minimum threshold.
After running commend python -m speech_recognition. Set minimum energy threshold which it display.
Setting procedure: press Ctrl then click mouse Recognizer(). Now set energy threshold.

Install in your python prompt (Anaconda Prompt)
pip install pyaudio
pip install --upgrade pyaudio
pip install wheel
pip install google-api-python-client
pip install monotonic
pip install SpeechRecognition
Code:
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
audio = r.record(source,duration=3)
try:
str=r.recognize_google(audio)
print(str)
except:
print("some error occurred!")

Try changing the mic index using:
with sr.Microphone(device_index = 0) as source:
To know your mic index use:
print((sr.Microphone.list_microphone_names()))
If this does not work use:
with sr.Microphone(device_index = 0) as source: # use the default microphone as the audio source
r.adjust_for_ambient_noise(source, duration = 1)
audio = r.listen(source)

Related

How to make my AI ignore the command words

I want to know how to stop my AI i made from saying the word of the command for e.g Now playing pay (song name) and just say song name. Sorry for bad English..
# pip install SpeechRecognition
# pip install PyAudios -- you'll need this if you want to use Microphone as your source
# pip install pywhatkit
# pip install gtts
# pip install playsound
# pip install playsound==1.2.2
import re
import time
import speech_recognition as sr
import pyaudio
import pywhatkit
from gtts import gTTS
from playsound import playsound
def speech(text):
print(text)
language = "en"
output = gTTS(text=text, lang=language, slow=False)
output.save("./sounds/output.mp3")
playsound("./sounds/output.mp3")
def get_audio():
while True:
recorder = sr.Recognizer()
with sr.Microphone() as source:
audio = recorder.listen(source)
speech_text = recorder.recognize_google(audio)
print(speech_text)
if "Bella" in speech_text:
playsound("./sounds/activation.mp3")
time.sleep(0.4)
print("Listening...")
else:
break
return speech_text
text = get_audio()
if "play" in text.lower():
speech(f"Now playing {text} on YouTube")
time.sleep(1)
pywhatkit.playonyt(text)
elif "search" in text.lower():
speech(f"Searching for {text} on Google")
time.sleep(1)
pywhatkit.search(text)
elif "joke" in text.lower():
speech("Knock knock, who's there? Boo. Boo who? Don't cry, I was just telling a joke.")
elif "hello" in text.lower():
speech("Hi!")
elif "hi" in text.lower():
speech("Hi!")
I've tried searching for a solution but I cant find one so if anyone can help please do because this is getting stressful trying to find a solution. Thanks <3.

No Audio with pyttsx3 Library in Python 3 (No errors)

Using pyttsx3 (tried versions 2.5 to current) on Visual Studios Code on Windows 10 With Python 3.10.0.
My Problem that I am currently having is that the code will run through, but no audio is being outputted. while debugging there is no pause stepping into or over the code (for parts including pyttsx3). I made sure my audio is on, and that it is working. I used a different tts library gtts and the audio worked, but I am trying to write offline. I also tried this exact code from VS code in PyCharm and I still had the same problem. Again with no errors or warnings.
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init(driverName='sapi5')
#voices = engine.getProperty('voices')
#engine.setProperty('voice', voices[0].id)
engine.say("Testing, audio")
engine.runAndWait
try:
with sr.Microphone() as source:
print('Listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
engine.say(command)
engine.runAndWait
except:
pass
print('Hello')
I also tried this block of code with no driver name and the same problem above persists
import speech_recognition as sr
import pyttsx3
listener = sr.Recognizer()
engine = pyttsx3.init()
#voices = engine.getProperty('voices')
#engine.setProperty('voice', voices[0].id)
engine.say("Testing, audio")
engine.runAndWait
try:
with sr.Microphone() as source:
print('Listening...')
voice = listener.listen(source)
command = listener.recognize_google(voice)
print(command)
engine.say(command)
engine.runAndWait
except:
pass
print('Hello')
The two commented lines on both programs didn't change anything for me either.
I also tested the program with just pyttsx3.
import pyttsx3
engine = pyttsx3.init(driverName='sapi5')
engine.say("Testing, audio")
engine.runAndWait
and tested using 'Testing, audio' instead of "Testing, audio" and I tried single words as well.
Any help would be amazing! Thank you!
In the mean time I will try to test this(translated to work with Linux) program in Linux to see if my OS is the issue.
I will also try an older version of python to see if that is the issue. Along with python 2.
My biggest assumption is that pyttsx3 needs a earlier version of python to work, but I could also be 100% wrong about that.
You forgot to put the parentheses on engine.runAndWait. Do this: engine.runAndWait()

Python speech_recognition IF Speech not recognised

I am a Portuguese High Schooler so sorry if I dont make myself very clear but I will try my best to explain my problem.
So for this project I need to make a program that recognises speech and do what is told to do. But if the speech is not recognisable I want the program to try until it finally catches what is being said. However I dont really know how to do that since this "error" keeps appearing: Error
Here is the code if anyone wants to help:
import speech_recognition as sr
import subprocess as sp
import os
def ouvir_microfone():
audio_Input = sr.Recognizer()
with sr.Microphone() as source:
Ray = "ON"
Open = "Open"
print("Hello my name is Ray pleased to help you")
audio_Input.adjust_for_ambient_noise(source)
print("Say something: ")
audio = audio_Input.listen(source)
frase = audio_Input.recognize_google(audio,language='en-US')
if ValueError: print("Speech not recognised")
else: print("You said: " + frase)
if Open in frase:
print('sucess')
ouvir_microfone()
Use try and exception to handle the error
try:
frase = audio_Input.recognize_google(audio,language='en-US')
print("You said: " + frase)
except ValueError:
print("Speech not recognised")
if Open in frase:
print('sucess')
import speech_recognition as sr
import subprocess as sp
import os
def ouvir_microfone():
audio_Input = sr.Recognizer()
with sr.Microphone() as source:
Ray = "ON"
Open = "Open"
print("Hello my name is Ray pleased to help you")
audio_Input.adjust_for_ambient_noise(source)
print("Say something: ")
audio = audio_Input.listen(source)
try:
# valid
frase = audio_Input.recognize_google(audio,language='en-US')
print("You said: " + frase)
except:
# google could not validate
print("Speech not recognised")
return False
if Open in frase:
print('sucess')
return True
while True:
if ouvir_microfone():
# got a valid response
break
Works as a solid template. Some imports also remain unused as a sidenote. Catch the exception and return False if the speech could not be recognized so it retries. The google voice recognition python library throws an exception in the case that no distinguishable speech can be read.

PyWinAuto type_keys() not giving space between words

I am trying to make a voice typing program in Python using pywinauto. First I recognized the speech using SpeechRecognition module and converted the recognized speech in string and then used the type_keys() method to type the string in notepad.
Here is the code:
from pywinauto import application
import time
import speech_recognition as sr
app = application.Application()
app.start("Notepad.exe")
def type_keys_in_notepad():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
print("Recognizing...")
content = r.recognize_google(audio, language="en-in")
print(content)
content_str = str(content)
app.Notepad.edit.type_keys(content_str)
except Exception as e:
print(e)
type_keys_in_notepad()
you need to add app.Notepad.edit.type_keys(content_str,with_spaces=True)
This will help to include white spaces
If you don't include with_spaces=True Space will be ignored

Having trouble with speech to text in Python

import speech_recognition as sr
rec = sr.Recognizer()
with sr.Microphone as source:
print('say something')
audio = rec.listen(source)
voice_data = rec.recognize_google(audio)
print(voice_data)
This is my code which is shown in plenty of tutorials that I watched online, I installed SpeechRecognizer and pyAudio (using whl file, not with pip, I don't know if it makes a difference).
When I try to run it it gives me an error about something in the pydevd.py and at the end gives me:
with sr.Microphone as source:
AttributeError: enter
How can I resolve this?
I got it to work by moving the statements not related to capturing the audio out of the with statement, this should work for you:
import speech_recognition as sr
rec = sr.Recognizer()
print('say something')
with sr.Microphone() as source:
audio = rec.listen(source)
voice_data = rec.recognize_google(audio)
print(voice_data)

Categories

Resources