I am using python 3.11. Here is the code with the main problem:
listener = sr.Recognizer() #voice
engine = tts.init()
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[3].id)
rate = engine.getProperty("rate")
engine.setProperty("rate", 190)
def say(text):
engine.say(text)
engine.runAndWait()
def take_command():
while True:
try:
while True:
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if command is not None:
break
if "phoenix" in command:
ps.playsound("C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\Start.mp3")
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
print(command)
return command
else:
take_command()
except Exception:
take_command()
I am new at coding/Python, so it is hard for me to resolve this issue. What I'm trying to accomplish here is to make a digital assistant that can respond when a specific "wake word" is said. The problem is, I want the program to continue listening until the wake word is said. I've tried loops, referring back to the original function, trying to get it to continue listening, but instead, the program keeps stopping and not giving back an error. Also, after the wake word is said, it takes a couple of minutes to respond, which I do not want to happen. After the speaker gives the command, it goes into another block of code to get "processed". There is nothing wrong with that part of the code but just in case you needed to see it:
def run_leopard():
command = take_command()
cmdList = [
"play",
"time",
"who is",
"never mind",
"who made you",
"your name",
]
if cmdList[0] in command:
play(command)
elif cmdList[1] in command:
get_time()
elif cmdList[2] in command:
info = search_response(command, "who is")
print(info)
say(info)
take_command()
elif cmdList[3] in command:
say("Alright. Let me know if you change your mind.")
take_command()
elif cmdList[4] in command:
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\End.mp3"
)
say(random.choice(whoMadeYou))
elif cmdList[5] in command:
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\End.mp3"
)
say(random.choice(whatsYourName))
else:
say("Sorry, I dont know how to respond to that. Please try again.")
print("Sorry I dont know how to respond to that. Please try again.")
def play(command):
import pywhatkit
song = command.replace("play", "")
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\End.mp3"
)
say(f"Alright. Playing {song}")
pywhatkit.playonyt(song)
take_command()
def get_time():
import datetime
time = datetime.datetime.now().strftime("%I:%M %p")
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\End.mp3"
)
say(f"The current time is {time}")
print(f"The current time is {time}")
take_command()
def search_response(command, arg1):
import wikipedia
person = command.replace(arg1, "")
result = wikipedia.summary(person, 1)
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\End.mp3"
)
return result
while True:
run_leopard()
I tried to provide as much information as possible but if someone can help me that would be greatly appreciated.
Here is what I tried at first:
if command == (None):
take_command()
else:
continue
I was hoping that if the command wasn't filled, it would run take_command() over again until it was filled. But instead, it would take huge breaks until starting the function again, which was not what I intended. After that, I did some research, and found out about loops. I implemented that into my code and got this:
while True:
try:
while True:
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
if command is not None:
break
if "phoenix" in command:
ps.playsound(
"C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\Start.mp3"
)
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
print(command)
return command
else:
take_command()
except Exception:
take_command()
This still didn't do as I hoped (continuously waiting for a response by the speaker) and instead, it takes a huge amount of time to do:
ps.playsound("C:\\Users\\allen\\OneDrive\\Documents\\VS-Code\\Python\\Digital-Assistant\\Start.mp3")
with sr.Microphone() as source:
voice = listener.listen(source)
command = listener.recognize_google(voice)
command = command.lower()
print(command)
return command
and I didn't know how to approach it after that. If you need more information I will gladly provide.