1

I'm creating and learning how to create a python personal assistant. I can't understand what's wrong in my code. I connected my mic, but its not recognizing. Can you help me. my code:

import speech_recognition as sr
import pyttsx3
import datetime
import time

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

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 Boss!")
        print("Good morning Boss!")
    elif hour >= 12 and hour <16:
        speak("Good afternoon Boss!")
        print("good afternoon Boss!")
    else:
        speak("Good Evening Boss!")
        print("Good Evening Boss!")

    speak("what can I do for you?")
    print("what can I do for you?")

def Takecommand():

    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
    try:
        print('Wait for few moments')
        query = r.recognize_google(audio,language="en-in")
        print("user said", query)

    except Exception as e :
        print(e)
        speak("Please say that again.")
        print("Please say that again.")
        return "None"
    return query

if __name__== '__main__':
    Wishme()
    Takecommand()

my output: good afternoon Boss! what can I do for you? listening...

It's not taking my command.

Yannis P.
  • 2,745
  • 1
  • 24
  • 39
  • That's strange, I have tried your code and it works for me. Are you sure you have connected a working microphone? Please test it on a webpage like https://www.onlinemictest.com/ What Python version are you using? – mbostic Jul 11 '21 at 18:02

0 Answers0