I am trying to fix this command to allow speech recognition - python

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

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)

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

Python app listening for a keyword. (Like Cortana)

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.

Voice Recognition in Python not working?

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/

Categories

Resources