Speech recognition printing transcripts - python

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

Related

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

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/

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