Python Forum

Full Version: Speech Recognition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using speech recognition to detect certain words. at the moment im just testing but I keep running into an error. I'll insert my code as well as the error I get any help I get is greatly appreciated

import speech_recognition as sr
from gtts import gTTS
import os
import pyaudio
def talkToMe(audio):
    print(audio)
    tts=gTTS(text=audio, lang= 'en')
    tts.save('audio.mp3')
    os.system(r'start C:\Users\steph\Desktop\audio.mp3')
def myCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print(' I am ready for your next command')
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration = 1)
        audio = r.listen(source)
    try:
        command = r.recognize_google(audio)
        print('You said: ' + command + '/n')

    except sr.UnknownValueError:
            assistant(myCommand())
    return command
def assistant(a):
    if 'time' in a:
        talkToMe("lol")
talkToMe('I am ready for your command')
while True:
    assistant(myCommand())
Traceback (most recent call last):
File "C:\Users\steph\Desktop\test.py", line 18, in myCommand
command = r.recognize_google(audio)
File "C:\Users\steph\AppData\Local\Programs\Python\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\steph\Desktop\test.py", line 18, in myCommand
command = r.recognize_google(audio)
File "C:\Users\steph\AppData\Local\Programs\Python\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google
if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError()
speech_recognition.UnknownValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\steph\Desktop\test.py", line 29, in <module>
assistant(myCommand())
File "C:\Users\steph\Desktop\test.py", line 22, in myCommand
assistant(myCommand())
File "C:\Users\steph\Desktop\test.py", line 22, in myCommand
assistant(myCommand())
File "C:\Users\steph\Desktop\test.py", line 23, in myCommand
return command
UnboundLocalError: local variable 'command' referenced before assignment

sorry didnt see the error tags

Error:
Traceback (most recent call last): File "C:\Users\steph\Desktop\test.py", line 18, in myCommand command = r.recognize_google(audio) File "C:\Users\steph\AppData\Local\Programs\Python\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError() speech_recognition.UnknownValueError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\steph\Desktop\test.py", line 18, in myCommand command = r.recognize_google(audio) File "C:\Users\steph\AppData\Local\Programs\Python\Python36-32\lib\site-packages\speech_recognition\__init__.py", line 858, in recognize_google if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError() speech_recognition.UnknownValueError During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\steph\Desktop\test.py", line 29, in <module> assistant(myCommand()) File "C:\Users\steph\Desktop\test.py", line 22, in myCommand assistant(myCommand()) File "C:\Users\steph\Desktop\test.py", line 22, in myCommand assistant(myCommand()) File "C:\Users\steph\Desktop\test.py", line 23, in myCommand return command UnboundLocalError: local variable 'command' referenced before assignment
(Dec-12-2018, 09:16 PM)Ash23733 Wrote: [ -> ]
Error:
File "C:\Users\steph\Desktop\test.py", line 23, in myCommand return command UnboundLocalError: local variable 'command' referenced before assignment

I feel that's pretty clear. You're using a variable (or at least referring to it) before you ever set it's value.
Quote:
def myCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print(' I am ready for your next command')
        r.pause_threshold = 1
        r.adjust_for_ambient_noise(source, duration = 1)
        audio = r.listen(source)
    try:
        command = r.recognize_google(audio)
        print('You said: ' + command + '/n')
 
    except sr.UnknownValueError:
            assistant(myCommand())
    return command
You only set it inside a try/except block, so if there's an error in r.recognize_google(), it never has a value. Give it a default value so that doesn't happen.
>>> def spam():
...   try:
...     x = 4 / 0 # divide by zero error
...   except ZeroDivisionError:
...     pass
...   return x
...
>>> spam()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in spam
UnboundLocalError: local variable 'x' referenced before assignment
>>> def spam():
...   x = None
...   try:
...     x = 4 / 0
...   except ZeroDivisionError:
...     pass
...   return x
...
>>> spam()
>>>