pythonpyttsx

Microphone is not Listening


I made a Python Program you can say a Artificial Intelligence . But when I run it , it show Listening but does not listen me I check my mic with other apps it works fine But it is not working in my code. The code is as follows:-

import pyttsx3 
import speech_recognition as sr 
import datetime

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!")

    elif hour>=12 and hour<18:
        speak("Good Afternoon!")   

    else:
        speak("Good Evening!")
       
    speak("Hey Sir.")
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(r, language='en-in')
        print(f"User said: {query}\n")

    except Exception as e:    
        print("Say that again please...")  
        return "None"
    return query

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

Tell Me the Solution That it listen me or may be error in code.


Solution

  • Assumption :

    I made the assumption by your comment, that by "nothing" you meant that your code is stack on the return "None"-line which indeed is true:

    yes stucked at listenig and do nothing

    ✔️ Solution :

    And so I tried to debug your code and I found out that it throws an exception at line 37 which says ``audio_data`` must be audio data, to fix it just change:

    query = r.recognize_google(r, language='en-in')
    

    with:

    query = r.recognize_google(audio, language='en-in')
    

    and it will probably work fine for you too :) .