-2

I have a password recognition code that goes like this:

from wit import Wit
import pyttsx3, time

def speak(string):
    engine = pyttsx3.init()
    engine.setProperty('rate', 220)

    engine.say(string)
    engine.runAndWait()

def Hear():
    recog = sr.Recognizer()
    with sr.Microphone() as source:
        audio= recog.listen(source)
        print (audio)
        return 

    wit_key = 'FO7EYTKK6OHRSZ3A5UFPMZOC7VIXCVSY'
    try:
        print('VEXD THINKS YOU SAID '+ recog.recognize_wit(audio, key=wit_key))
        heard = recog.recognize_wit(audio, key=wit_key)
    except sr.UnknownValueError:
        print("I DIDN't get that")
    except sr.RequestError as e:
        print("SERVER DISCONECTED")

speak("CHECKING IDENTITY" )
sound = Hear()
time.sleep(1)
if "master"in sound:
    speak("Yes My Lord")

It is supposed to say YEs my lord when I say master but when I say master , it returns this error

File "c:/Users/tdmfa/OneDrive/Desktop/VEXD app/VEXD AI/AI_START.py", line 33, in <module>
    if "master"in sound:
TypeError: argument of type 'NoneType' is not iterable 
AcK
  • 2,063
  • 2
  • 20
  • 27
  • 1
    Line `if "master"in sound:` tries to iterate through `sound` and the error say that objects of `NoneType` are not iterable. The expanation is obvious: `sound` is an object of type `NoneType`, i.e. `sound` is `None`. – zvone Jan 16 '21 at 23:18

2 Answers2

1
sound= Hear()

This line of code, doesn't receive any output from the Hear() function. Your Hear() function doesn't return any value, it simply exits the function and returns None. That is why it says
'NoneType' is not iterable, because you are trying to iterate over sound.

This should work:

from wit import Wit
import pyttsx3, time
import speech_recognition as sr


def speak(string):
    engine = pyttsx3.init()
    engine.setProperty('rate', 220)

    engine.say(string)
    engine.runAndWait()

def Hear():
    recog= sr.Recognizer()
    with sr.Microphone() as source:
        audio= recog.listen(source)
        

    wit_key = 'FO7EYTKK6OHRSZ3A5UFPMZOC7VIXCVSY'
    try:
        audio_str = recog.recognize_wit(audio, key=wit_key)
        print('VEXD THINKS YOU SAID '+ audio_str)
        return audio_str
    except sr.UnknownValueError:
        error1 = "I DIDN't get that"
        print(error1)
        return error1
    except sr.RequestError as e:
        error2 = "SERVER DISCONNECTED"
        print(error2)
        return error2

speak("CHECKING IDENTITY")
sound= Hear()
time.sleep(1)
if "master" in sound:
    speak("Yes My Lord")
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
0

It looks like you should be returning heard from your Hear() function, instead of None as it currently does. There are two exit points in that function, both return None:

  • the call to return right after audio is obtained by the recogniser
  • implicitly when the function ends without calling return to return a specific value.

To fix, remove the return call just after the recogniser gets the audio, and then add return heard after heard = recog.recognize_wit(audio, key=wit_key). The function can still return None if one of the expected exceptions is raised, so you will want to probably want to return an empty list, or retry as appropriate.

I am not familiar with the libraries that you are using, so I am assuming that heard will be some kind of iterable (list, tuple, string, etc.) that makes sense to the rest of your code.

mhawke
  • 84,695
  • 9
  • 117
  • 138