AttributeError: 'Recognizer' object has no attribute 'recognize_google' - python

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

Related

Speech recognition printing transcripts

So my speech recognition in python is printing transcripts and its annoying. Here's how it looks like
Listening..
Recognizing..
result2:
{ 'alternative': [{'confidence': 0.88687539, 'transcript': 'Hello Spartan'}],
'final': True}
You said: Hello Spartan
Here's my listening code:
import speech_recognition as sr
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening..")
r.pause_threshold = 1
audio = r.listen(source,0,5)
try:
print("Recognizing..")
query = r.recognize_google(audio, language='en-in', show_all=False)
print(f'You said: {query}')
except:
return ""
query = str(query)
return query.lower()
Any way to fix this?
Seems like a bug in the code. I've solved this by downgrading the speech_recognition library:
pip install SpeechRecognition==3.8.1

I am trying to get voice input by using speech recognition it is giving UnknownValueError

I am trying create jarvis in python. To get voice input i am using speech recognition module, it is giving UnknownValueError. Please help me. Here the code,
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print('Recognition is on....')
recognizer.pause_threshold = 0.7
voice = recognizer.listen(source, timeout=10)
query = recognizer.recognize_google(voice).lower()
print('This is the query that was made....', query)
print(query)

I am trying to fix this command to allow speech recognition

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

Getting AttributeError: __enter__ when using Microphone from Speech Recognizer

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

Speech Recognition Library in Python always returns same string

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.

Categories

Resources