jeudi 15 août 2019

if is working properly but elif throws an error

I am writing a python code(supplied); but when I say 'open photos' everything works fine and it recognizes that I said 'open photos' but throws back except statement instead of 'elif listen() == "open photos": output'.Please help me.

import speech_recognition as sr
import pyttsx3
import time
friday = pyttsx3.init('sapi5')
r = sr.Recognizer()
mic = sr.Microphone()
friday.setProperty('rate', 150)
def listen():
     with mic as source:
         r.adjust_for_ambient_noise(source)
         audio = r.listen(source)
         try:
           text =  r.recognize_google(audio)
           print(text)
           return text
         except sr.UnknownValueError:
          friday.say("sorry sir , could't do that")
          friday.runAndWait()
print("started!")
while 1:
    if listen() == "hello Jarvis":
        friday.say("hello sir")
        friday.runAndWait()
    elif listen() == "open photos":
            friday.say("opening")
            friday.runAndWait()

Aucun commentaire:

Enregistrer un commentaire