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


Messages In This Thread
Speech Recognition - by Ash23733 - Dec-12-2018, 09:16 PM
RE: Speech Recognition - by nilamo - Dec-12-2018, 10:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Speech Recognition with timestamps DeanAseraf1 3 6,574 Jun-27-2021, 06:58 PM
Last Post: gh_ad
  Continous Speech Recognition dell91 0 1,828 Oct-29-2020, 10:51 AM
Last Post: dell91
  text to speech Heyjoe 11 6,794 Jul-02-2020, 01:32 AM
Last Post: Heyjoe
  Googles Text to speech justindiaz7474 0 1,656 May-06-2020, 02:04 AM
Last Post: justindiaz7474
  Python Speech recognition, word by word AceScottie 6 15,985 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,219 Apr-08-2020, 02:13 AM
Last Post: Helloworld20
  Python Speech Engines? Robo_Pi 2 2,090 Mar-12-2020, 02:46 PM
Last Post: Robo_Pi
  Need Help With Text to Speech App Lethe 0 1,976 Oct-24-2018, 10:03 PM
Last Post: Lethe
  [split] Offline audio to text (Speech Recognition) Nishant260190 0 3,923 Sep-02-2018, 12:33 PM
Last Post: Nishant260190
  Using Windows Speech to Text jmair 2 3,144 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