I am trying to use speech recognition with OOP. I try to convert the procedural code from this github.
import speech_recognition as sr #recognise speech
import time
class SpeechRecognition():
def __init__(self):
self.r = sr.Recognizer() #initialize recognizer that is responsible for recognizing the speech
# listen for audio and convert it to text:
def record_audio(self, ask=False):
with sr.Microphone() as source: # microphone as source
if ask:
print(ask)
audio = self.r.listen(source) # listen for the audio via source
voice_data = ''
try:
voice_data = self.r.recognize_google(audio, language='sv-SE') # convert audio to text
except sr.UnknownValueError: # error: recognizer does not understand
print('Hörde inte det')
except sr.RequestError: # error: recognizer is not connected
print('Ledsen, tjänsten är nere')
print(f">> {voice_data.lower()}") # print what user said
return voice_data.lower()
def init_speech_recognizer(self):
time.sleep(1)
print('Hur kan jag hjälpa dig?')
while 1: #this will let it keep listen
voice_data = self.record_audio("Recording") # get the voice input
print("Done")
speechObj = SpeechRecognition()
speechObj.init_speech_recognizer()
The output is:
Hur kan jag hjälpa dig?
Recording
But whatever I say nothing gets printed. Why is this?
I'm trying to write a script that uses SpeechRecognition to run a function once say a wake-up word "Run" Everything works perfectly up until I get to my regex search to find my wake-up word in my alpha variable that is assigned to what I say. Upon running I get the error. I have tested my regex alg outside of the SpeechRecognition with a string and it worked perfectly.
ERROR:
raise UnknownValueError()
speech_recognition.UnknownValueError
Could I get some help?
CODE:
import speech_recognition as sr
import re
def stt():
recognizer = sr.Recognizer()
microphone = sr.Microphone()
# 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")
# listen and assign
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
# return output
return recognizer.recognize_google(audio)
while True:
alpha = stt()
print(alpha)
if re.search('.+Run.+', alpha):
print("1")
beta = alpha.split()
query = beta.pop()
print("working")
Try putting your whole code under a try and except.
Like this:
import speech_recognition as sr
import re
try:
def stt():
recognizer = sr.Recognizer()
microphone = sr.Microphone()
# 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")
# listen and assign
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
# return output
return recognizer.recognize_google(audio)
while True:
alpha = stt()
print(alpha)
if re.search('.+Run.+', alpha):
print("1")
beta = alpha.split()
query = beta.pop()
print("working")
except sr.UnknownValueError() as e:
pass
If that doesn't work. Look at this github issue
I am making a chatbot and I am trying to create a function that gets a question and responds in a certain way but I receive an error that says TypeError: argument of type 'function' is not iterable regarding the if statement in the "def quanda" section how can I solve this issue?
import os
import speech_recognition as sr
from gtts import gTTS
import playsound
import time
def speak(text):
tts = gTTS(text=text, lang="en")
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
def qanda(question, response):
text = get_audio
if question in text:
speak(response)
return response
speak("hello, how can i help you?")
text = get_audio
qanda("hello", "hi there")
quanda("what is the weather", "Its 27 degrees")
Thats because get_audio is a method, you need to call it with (). So, whereever you care calling get_audio method call it like this: get_audio().
I am trying to use the SpeechRecognition STT module for an AI i am coding and i'm also using Pyttsx but i get this error.
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pyttsx/__init__.py", line 18, in <module>
from engine import Engine
ImportError: No module named 'engine'
Alot of people have tried to answer this problem, for all the people having this problem but the answers aren't explained or don't work!
Here's my code
import speech_recognition
import pyttsx
speech_engine = pyttsx.init('sapi5') # see http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init
speech_engine.setProperty('rate', 150)
def speak(text):
speech_engine.say(text)
speech_engine.runAndWait()
recognizer = speech_recognition.Recognizer()
def listen():
with speech_recognition.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
return recognizer.recognize_sphinx(audio)
# or: return recognizer.recognize_google(audio)
except speech_recognition.UnknownValueError:
print("Could not understand audio")
except speech_recognition.RequestError as e:
print("Recog Error; {0}".format(e))
return ""
speak("Say something!")
speak("I heard you say " + listen())
if someone can explain and fix my problem that would be helpful
thank you!
Follow this link.
It has all the updates compatible with python 3.
https://github.com/jpercent/pyttsx
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)