Local variable referenced before assignment(speech recognition python) - python

import speech_recognition as sr
r = sr.Recognizer()
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except:
pass
return text
text = speaking()
if text == 'hello':
print("You said hello")
So yeah the code works fine. But whenever speech_recognition hears sounds like me clearing the throat/laughing/literally any ambient noise, I get an error. Can you tell me why and tell a way to correct it?

your problem.
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
except: # here its the problem
pass
return text # and here
the code should look like this
def speaking():
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
text1 = r.recognize_google(audio)
text = text1.lower()
# or you can return text here
# and get rid of the below else block
# return text
except:
# if you get an error then do nothing
pass
# you can return None
return None
else:
# but if the try code worked then return text
return text
ofcourse you get "referenced before assignment error" because in your old code if you get an error you do nothing with it and after that you reference text, but text isnt going to be defined if you get an exception just when you are recording.

Related

Getting an error using listen function from the speech recognition library

def TakeCommand():
r = sr.Recognizer
mic = sr.Microphone()
with mic as source:
print("I am listening ....")
audio = r.listen(source)
try:
print("Recognizing....")
query = r.recognize_google(audio, language = 'en-US')
print(query)
except Exception as exc:
print(exc)
print("Sorry, I didn't recognized it. Please repeat...")
TakeCommand()
I am getting an error on the line ''audio = r.listen(sourse)' It is says, let me quote 'listen() missing 1 required positional argument: 'source' ''
And in case the link to the image:
[1]: https://i.stack.imgur.com/wEYOt.png
Try changing r = sr.Recognizer to r = sr.Recognizer().

Create a Loop of SpeechRecognition

I am creating a voice assistant in python and I would like to know how I can make a loop so that every time the assistant responds, the microphone is activated so I can ask him again
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
#Here the function activates
text = get_audio()
#This is a example question
if 'hello computer' in text:
speak('Hi! How are you?')
#This is where I want to turn on the microphone again, process my voice and say the answer and so on to infinity
#Use Google TTS for say something
def speak(text):
tts = gTTS(text=text, lang="es", slow=False)
filename = "voiceww.mp3"
tts.save(filename)
playsound.playsound(filename)
#Use mic for get my voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Di algo')
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
said = ""
#Print the answer in console
try:
#print('Listo')
said = r.recognize_google(audio, language='es')
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
This is how you keep your microphone turned on:
while True:
# Here the function activates
text = get_audio().lower
# These are examples
if 'hello computer' in text:
speak('Hi! How are you?')
elif "good morning" in text:
speak("Hey good morning")

integrate speech recognition and tkinter

I'm doing a project on speech recognition and am trying to use Tkinter to create a GUI for my project...the SR part works well but when I integrate it with Tkinter it doesn't work...please help. (am new to programming so please don't mind my code:) )
#MY CODE
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = "".format(r.recognize_google(audio).get())
msg.configure(text=txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
I would like it to display the translated text in the label but it doesn't. It only shows "say something" even after speaking.
Try this, I blocked out the msg.configure(text='Say Somethin'). This line makes the recorded text being reformatted to 'Say Something' and not the text that was recorded. Hope this helps.
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = r.recognize_google(audio)
msg.configure(text=txt)
print(txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()

Unable to get instructions in audio format and not able to play audio file after first one

Jarvis should say every instruction comes on screen but here first statement is spoken & none other than that gets audio voice. And while saving the audio only the first statement gets stored as audio file. how to delete the previous audio file after process & save new audio file as instruction.How to get print statements in audio form, like there's a print statement ("Say something!") , it should be there on the screen as well as audio of same should be spoken.
import speech_recognition as sr
from time import ctime
import time
import os
from gtts import gTTS
from pygame import mixer
mixer.init()
def speak(audioString):
print(audioString)
tts = gTTS(text=audioString, lang='en')
tts.save("audio.mp3")
os.system("mpg321 audio.mp3")
def speak_out():
mixer.music.load('audio.mp3')
mixer.music.play()
time.sleep(2)
def recordAudio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
data = ""
try:
data = r.recognize_google(audio)
print("You said: " + data)
except sr.UnknownValueError:
print("I could not understand")
except sr.RequestError as e:
print("Server error. Check your internet connection: {0}".format(e))
return data
def jarvis(data):
if "how are you" in data:
speak("I am fine")
if "what time is it" in data:
speak(ctime())
if 'mail' in data:
speak('who is recipient')
recipient = speak()
if 'John' in data:
speak('What should I say')
content = recordAudio()
mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('abc#gmail.com','xyz')
mail.sendmail('Person name', 'mail_id', content)
mail.close()
speak('Email sent')
time.sleep(2)
speak("Hi Ajay, what can I do for you?")
speak_out()
while 1:
data = recordAudio()
jarvis(data)

Speech recognizing in a loop

I am trying to write a speech recognition code which takes voice from the microphone and process till a "Stop" is said. The code works for the first voice but then it gives an error. The code which I wrote is below:
import speech_recognition as sr
import webbrowser
r = sr.Recognizer()
with sr.Microphone() as source:
while True:
audio = r.listen(source)
print("You said " + r.recognize(audio))
if r.recognize(audio)=="Facebook":
webbrowser.open('https://www.facebook.com')
if r.recognize(audio)=="Google":
webbrowser.open('https://www.google.co.uk')
if r.recognize(audio)=="Stop":
break
The error which I am getting is :
You said Facebook
Traceback (most recent call last):
File "C:/Work/Scripts/SpeechRecognition/Speech.py", line 9, in <module>
print("You said " + r.recognize(audio)) # listen for the first phrase and extract it into audio data
File "C:\Users\roradhak.NDS-UK\AppData\Roaming\Python\Python27\site-packages\speech_recognition\__init__.py", line 324, in recognize
raise LookupError("Speech is unintelligible")
LookupError: Speech is unintelligible
Process finished with exit code 1
I think the problem might be that you call recognize many times on the same input and the first call has already consumed the audio. You can introduce one extra variable + add try/catch to handle the LookupError.
with sr.Microphone() as source:
while True:
audio = r.listen(source)
try:
result = r.recognize(audio)
print("You said " + result)
words = result.lower()
if words=="facebook":
webbrowser.open('https://www.facebook.com')
if words=="google":
webbrowser.open('https://www.google.co.uk')
if words=="stop":
break
except LookupError:
print("Please, speak more clearly")
You need to catch the exception:
def recognize(audio):
try:
return r.recognize(audio)
except LookupError, e:
print e
return ''
Then:
with sr.Microphone() as source:
while True:
audio = r.listen(source)
words = recognize(audio)
print("You said " + words)
if words == "Facebook":
webbrowser.open('https://www.facebook.com')
elif words =="Google":
webbrowser.open('https://www.google.co.uk')
elif words == "Stop":
break

Categories

Resources