Search code examples
pythonfunctionspeech-recognitionpyttsx3

Speech recognition shows Error In Python (Positional Argument Error)


Python gives a error when I run this code and I've checked it number of times. The source here is the Microphone, but yet it keeps on asking a value for the 'source'. What should I do. What parameter should I pass to the 'source'?

import pyttsx3
import speech_recognition as sr
#Required Modules

engine = pyttsx3.init()
r = sr.Recognizer
#Initializes variable (Avoiding Time Delay)

def Speak(audio):
    engine.say(audio)
    engine.runAndWait()
#Defining the function Speak() for Audio Output

def Listen():
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise
        print("Listening")
        voice = r.listen(source)
        print("Done Listening")
        a = r.recognize_google(voice)
        a = a.lower()
        print("You asked - "+a)    
#Defining the function Listen() for Audio Input (INTERNET REQUIRED)

while True:    
    Speak("Ask Me Some Questions")
    #Speaks

    Listen()
    #Listens

    try:
        if a == "how are you":
            print("I'm Fine Sir")
            Speak("I'm Fine Sir") 
    #Tries to do the program without error
    
    except:
        print("Error Occured!!!")
    #If error occured, this will be the Output                   

#Looped

This Code Gives an error,

Traceback (most recent call last):
  File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 29, in <module>
    Listen()
  File "c:\Users\Vishal\AppData\Local\Programs\Python\Python39\Python Projects\Speech.py", line 18, in Listen
    voice = r.listen(source)
TypeError: listen() missing 1 required positional argument: 'source'

What is actaully the error and how to solve it? (I'm a beginner in python, so go easy on me)


Solution

  • Here is the updated code

    1.You have to go through documentation always first there should be sr.Recognizer()

    2.and if you are creating method then for interaction you should return value

    import pyttsx3
    import speech_recognition as sr
    #Required Modules
    
    engine = pyttsx3.init()
    r = sr.Recognizer()
    #Initializes variable (Avoiding Time Delay)
    
    def Speak(audio):
        engine.say(audio)
        engine.runAndWait()
    #Defining the function Speak() for Audio Output
    
    def Listen():
        with sr.Microphone() as source:
            r.adjust_for_ambient_noise(source)
            print("Listening")
            voice = r.listen(source)
            print("Done Listening")
            a = r.recognize_google(voice)
            a = a.lower()
            print("You asked - "+a)   
            return a
    #Defining the function Listen() for Audio Input (INTERNET REQUIRED)
    
    while True:    
        Speak("Ask Me Some Questions")
        #Speaks
    
        a=Listen()
        #Listens
    
        try:
            if a == "how are you":
                print("I'm Fine Sir")
                Speak("I'm Fine Sir") 
        #Tries to do the program without error
        
        except:
            print(a)
            print("Error Occured!!!")
        #If error occured, this will be the Output                   
    
    #Looped