The code is below. As I researched the documention Speech_Recognision must be installed if we use Microphone. So I installed it but still i have this error.
def recordAudio():
r = sr.Recognizer()
with sr.Microphone as source:
print('I am listening to you sir.')
audio = r.listen(source)
data = ''
try:
data = r.recognize_google(audio)
print('You said: ' + data)
except sr.UnknownValueError:
print('Voice cannot be recognized.')
except sr.RequestError as e:
print('Req results:' + e)
return data
line 54, in recordAudio
with sr.Microphone as source:
AttributeError: __enter__
An AttributeError: __enter__ means you are attempting to enter a context manager block with an object which does not support the context manager protocol; it does not have a __enter__ method.
Specifically, you are attempting to open the sr.Microphone class in you with statement. As per the documentation, you need to provide an instance sr.Microphone() to the context manager instead.
with sr.Microphone() as source:
...
For all those who did not get solution to this problem:
Instead of using the class sr.Microphone as a source, use the object sr.Microphone().
Because we are supposed to call the method using the speechRecognition object and not the class itself.
It would constantly listen until you terminate:
SpeechRecognition
import speech_recognition as sr
def speech_recog():
r = sr.Recognizer()
mic = sr.Microphone()
while True:
with mic as source:
print("Speak...")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print(f"You said {text}")
except sr.UnknownValueError:
print("Didnt hear that try again")
speech_recog()
Related
I was trying the Speech Recognition module in Python(V3.7.9) and have been getting the following error:
AttributeError: 'Recognizer' object has no attribute 'recognize_google'
The version I use is: SpeechRecognition-3.8.1
Here is the code I used:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
#r.adjust_for_ambient_noise(source, duration=1)
print("Listening...")
speak("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
speak("Recognizing...")
query = r.recognize_google(audio)
print(f"Speech was: {query}\n")
and it's not picking up my voice from the microphone. FÄ°XED!
EDIT: this is how i solved the problem: **query= "" **
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("Listening...")
#speak("Listening...")
audio = r.listen(source)
query = ""
But i am getting this error:
recognize_google() got an unexpected keyword argument 'Language'
i try this code but it can't detect my language:
query = r.recognize_google(audio, Language='tr-TR')
import speech_recognition as sr
r = sr.Recognizer() # a recogniser to understand my voice
with sr.Microphone() as source: # use the default microphone as the voice
print('Listening....')
voice = r.listen(source) # call speech recogniser to recognise the first phrase
try:
command = r.recognizer_google(voice) # pass the audio to google
print(command)
except:
print('Could Not Understand Audio')
However i am getting this error returned
Instance of 'Recognizer' has no 'recognizer_google' member
Can someone help me fix this ? I have downgraded to python 3.8.1 from 3.8.2 but the problem persists
You should check the version of speech_recognition library. The version that you are using doesn't seem to have recognizer_google function.
Here is the updated code:
import speech_recognition as sr
r = sr.Recognizer() # a recogniser to understand my voice
with sr.Microphone() as source: # use the default microphone as the voice
print('Listening....')
voice = r.listen(source) # call speech recogniser to recognise the first phrase
try:
command = r.recognize_google(voice) # pass the audio to google
print(command)
except:
print('Could Not Understand Audio')
The Error You Have Done Here:
There isn't any member called r.recognizer_google() but there is a member called r.recognize_google() in the Recognizer()
It is best if you upgrade your version of python to the newest, as well as the speech_recognition module, if the above code didn't work
I'm trying to make a Python app, which behaves like Alexa, Cortana or Google's "Ok, Google".
I want it to constantly listen for a specific keyword. After it hears the keyword I want it to execute a function.
How can I do this?
Take a look at Speech Recognition This is a library that allows speech recognition including Google Cloud Speech API.
Relating to the second part of your question this seems relevant:
How can i detect one word with speech recognition in Python
Once you can listen for a word just call your function.
import speech_recognition as sr
# get audio from the microphone
r = sr.Recognizer()
keywork = "hello"
with sr.Microphone() as source:
print("Speak:")
audio = r.listen(source)
try:
if r.recognize_google(audio)) == keyword:
myfunction()
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
This code was adapted from this tutorial.
This will search for your keyword in the entire speech.
import speech_recognition as sr
r = sr.Recognizer()
keyWord = 'joker'
with sr.Microphone() as source:
print('Please start speaking..\n')
while True:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
if keyWord.lower() in text.lower():
print('Keyword detected in the speech.')
except Exception as e:
print('Please speak again.')
To improve your accuracy, you can even fetch all possible rhyming words and compare.
You can also do a character by character comparison to calculate the match percentage and set a minimum percentage threshold for the comparison success.
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
BING_KEY = "" #API KEY HERE
try:
print("Microsoft Bing Voice Recognition thinks you said " + r.recognize_bing(audio, key=BING_KEY))
except sr.UnknownValueError:
print("Microsoft Bing Voice Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Microsoft Bing Voice Recognition service; {0}".format(e))
########################################################################################################
voiceLine = r.recognize_bing(audio, key=BING_KEY)
if r.recognize_bing(audio, key=BING_KEY) == "Hello":
print("big nerd")
#### if voiceLine == "Hello":
#### print("big nerd")
The problem seems to be with the voiceline not acting like a normal string in "if" statements... (I'm farely new to python so please go easy on me :c) I am also aware that the indents are not in the right place, I don't know how to use this site, kms.
I tried this and it worked:
faudio = r.recognize_bing(audio, key=BING_KEY)
if faudio.strip() == "whatever":
do something...
If you are using external or Bluetooth microphone, you may need to install extra python package:
!pip install pyaudio
Then import the package in your code without changing anything, but you can check for the list of supported mics in your pc
import pyaudio
# check the list of microphones
sr.Microphone.list_microphone_names()
for a complete tutorial on speech recognition, it worth checking this website:
https://realpython.com/python-speech-recognition/
I'm trying to use Google Speech recognition, My problem is that after saying something into microphone, results are always same.
My Function looks like this
def RecordAudio():
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech Recognition
try:
print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return audio
When I'm trying to get data
data = RecordAudio()
Data is always equals to this
speech_recognition.AudioData object at 0x111a8b358
But what is strange for me is that, when this code is in one file, without function and without returning result, in that time this works.
For example I've test.py file
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# Speech recognition using Google Speech Recognition
try:
print("You said: " + r.recognize_google(audio))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
This is working but first example not.
Any idea what's happening ?
I Solved it.
I think that my problem was, that this function
audio = r.listen(source)
returns AudioData and when I was printing it
thats why it was speech_recognition.AudioData object at 0x111a8b358
To recognize it we must return
return r.recognize_google(audio)
It will return real string and not AudioData.
If somebody will have same problem,hope it helps.