Akshat Singh
Akshat Singh

Reputation: 3

Speech recognition 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'?

the code is :

 import pyttsx3
import datetime
import speech_recognition as sr




engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)




def speak(audio):
    engine.say(audio)
    engine.runAndWait()
     
def wishme():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning Sir")
    elif hour>=12 and hour<18:
        speak("Good afternoon Sir")
    else:
        speak("Good Evening")


    speak("Hello Sir, I am Jarvis how can I help you?")
        
def takeCommand():
    r = sr.Recognizer
    with sr.Microphone() as source:
        print("Listening....")
        r.pause_threshold = 1
        audio = r.listen(source)
    try:
        print("recognizing...")
        query = r.recognize_google(audio, language='en-in')
        print(f"User Said: {query}\n" )


        
    except Exception as e:   
        print("Say that again please...")   #Say that again will be printed in case of improper voice 
        return "None"
    return query




if __name__=="__main__" :
    wishme()
    takeCommand()

The error that is coming of running the code :

Traceback (most recent call last):
  File "c:\Users\HP\3D Objects\Python\Jarvis\jarvis.py", line 54, in <module>
    takeCommand()
  File "c:\Users\HP\3D Objects\Python\Jarvis\jarvis.py", line 36, in takeCommand
    audio = 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)

Upvotes: 0

Views: 64

Answers (1)

Nishani Kasineshan
Nishani Kasineshan

Reputation: 682

Instead of this r = sr.Recognizer Try this r = sr.Recognizer()

Upvotes: 1

Related Questions