Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Speech Recognition
#1
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
Reply
#2
(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()
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Speech Recognition with timestamps DeanAseraf1 3 6,557 Jun-27-2021, 06:58 PM
Last Post: gh_ad
  Continous Speech Recognition dell91 0 1,822 Oct-29-2020, 10:51 AM
Last Post: dell91
  text to speech Heyjoe 11 6,767 Jul-02-2020, 01:32 AM
Last Post: Heyjoe
  Googles Text to speech justindiaz7474 0 1,652 May-06-2020, 02:04 AM
Last Post: justindiaz7474
  Python Speech recognition, word by word AceScottie 6 15,970 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Simulation of Text to speech (without using mic) to Voice recognition suported hardwa Helloworld20 2 2,212 Apr-08-2020, 02:13 AM
Last Post: Helloworld20
  Python Speech Engines? Robo_Pi 2 2,087 Mar-12-2020, 02:46 PM
Last Post: Robo_Pi
  Need Help With Text to Speech App Lethe 0 1,973 Oct-24-2018, 10:03 PM
Last Post: Lethe
  [split] Offline audio to text (Speech Recognition) Nishant260190 0 3,918 Sep-02-2018, 12:33 PM
Last Post: Nishant260190
  Using Windows Speech to Text jmair 2 3,138 May-08-2018, 01:40 PM
Last Post: jmair

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020