Speech recognition for Python 3.4 thats easy to work? - python

I wish to get a simple speech recognition that works. I have been looking at this on speech_recognition, When I execute the code the following error occurs
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
print("You said " + r.recognize(audio))
except LookupError:
print("You said " + r.recognize(audio)) # recognize speech using Google Speech Recognition
AttributeError: 'Recognizer' object has no attribute 'recognize'
print("Could not understand audio")
This was copied from their examples on their web page

I was facing the same problem. The problem was that I had not set the minimum threshold level. So I added this code.
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
#set threhold level
with m as source: r.adjust_for_ambient_noise(source)
print("Set minimum energy threshold to {}".format(r.energy_threshold))
# obtain audio from the microphone
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
print(r.recognize_google(audio))
Now its working perfect!!!

I got it working.
import speech_recognition as sr
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
print(r.recognize_google(audio))

What version of SpeechRecognition library do you use?
You can check it with:
import speech_recognition as sr
print sr.__version__
If you are using the latest version (SpeechRecognition 3.1.3), you should use a recognize_google() method instead of recognize(), to use Google API.
As for the latest documentation, you can look it up here, and also some useful examples.

I have taken a code that actually executes but fails to print out what I say
NOTE: this example requires PyAudio because it uses the Microphone class
import speech_recognition as sr
import pyaudio
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
# recognize speech using Google Speech Recognition
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks 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))
# recognize speech using Wit.ai
WIT_AI_KEY = "INSERT WIT.AI API KEY HERE" # Wit.ai keys are 32-character uppercase alphanumeric strings
try:
print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
except sr.UnknownValueError:
print("Wit.ai could not understand audio")
except sr.RequestError as e:
print("Could not request results from Wit.ai service; {0}".format(e))
# recognize speech using IBM Speech to Text
IBM_USERNAME = "INSERT IBM SPEECH TO TEXT USERNAME HERE" # IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
IBM_PASSWORD = "INSERT IBM SPEECH TO TEXT PASSWORD HERE" # IBM Speech to Text passwords are mixed-case alphanumeric strings
try:
print("IBM Speech to Text thinks you said " + r.recognize_ibm(audio, username=IBM_USERNAME, password=IBM_PASSWORD))
except sr.UnknownValueError:
print("IBM Speech to Text could not understand audio")
except sr.RequestError as e:
print("Could not request results from IBM Speech to Text service; {0}".format(e))
# recognize speech using AT&T Speech to Text
ATT_APP_KEY = "INSERT AT&T SPEECH TO TEXT APP KEY HERE" # AT&T Speech to Text app keys are 32-character lowercase alphanumeric strings
ATT_APP_SECRET = "INSERT AT&T SPEECH TO TEXT APP SECRET HERE" # AT&T Speech to Text app secrets are 32-character lowercase alphanumeric strings
try:
print("AT&T Speech to Text thinks you said " + r.recognize_att(audio, app_key=ATT_APP_KEY, app_secret=ATT_APP_SECRET))
except sr.UnknownValueError:
print("AT&T Speech to Text could not understand audio")
except sr.RequestError as e:
print("Could not request results from AT&T Speech to Text service; {0}".format(e))
I get the following read out
Say something!
Google Speech Recognition thinks you said hello
Could not request results from Wit.ai service; recognition request failed: Bad Request
Could not request results from IBM Speech to Text service; recognition request failed: Unauthorized
Could not request results from AT&T Speech to Text service; credential request failed: Unauthorized

Related

I am getting syntax error in python while using Speech-recoginition

data = ""
try:
# Uses the default API key
# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
data = r.re[enter image description here][1]cognize_google(audio)
print("You said: " + data)
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 data
I am getting this error
return data
File "", line 13
return data
^
SyntaxError: 'return' outside function
If you are looking to return "" if the audio is invalid, then it is an indentation error with your last line.
The updated code would look something like this:
def audio_convertor(audio):
data = ""
try:
# Uses the default API key
# To use another API key: `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
data = r.re[enter image description here][1]cognize_google(audio)
print("You said: " + data)
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 data
Hope this helps.
The reason for the error might because the indentation of the return statement is incorrect w.r.t your method indentation.

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/

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.

Is it possible to use Python SpeechRecognition on Django?

I have the following script which works when running in a terminal:
All is does is turn microphone speech to text.
import speech_recognition as sr
# obtain audio from microphone
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
print("Google Speech Recognition thinks 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))
Would it be possible to have this work on Django after pushing a button?
Something like:
View:
import speech_recognition as sr
# Create your views here.
def index(request):
return render(request, 'app/index.html')
def text(request):
r = sr.Recognizer()
with sr.Microphone() as source:
#print("Say something!")
audio = r.listen(source)
try:
# for testing purposes, we're just using the default API key
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
# instead of `r.recognize_google(audio)`
speech = r.recognize_google(audio)
except sr.UnknownValueError:
speech = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
speech = "Could not request results from Google Speech Recognition service; {0}".format(e)
return render(request, 'app/text', {'speech': speech})
Template:
<form action="/text/" method="post">
<input type="button" value="Start listening" />
</form>
Is this possible? Am I close or not at all?
Django has no access to the users' computers so if you try to record from a microphone you will be using the server's microphone, if it has one.
You need to record using JS/HTML5 and then send the data to django to process with AJAX. You might even be able to stream it but it's probably not worth the effort.

Categories

Resources