我创建了一个语音助手,该助手具有Wikipedia搜索,打开浏览器,播放音乐,播放视频,说时间等功能。起初效果不错。现在,它继续监听,但是不响应或不识别命令。基本上,它不起作用。我最近清除了我的临时文件,这能...吗?
import pyttsx3
import datetime, time
import speech_recognition as sr
import wikipedia
import sys
import webbrowser
import os
import random
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
print(voices[0].id)
engine.setProperty('voices', voices[0].id)
def takeCommand():
r= sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
r.pause_threshold = 1
r.phrase_threshold= 0.3
audio= r.listen(source)
#return audio
try:
print("Recognizing...")
query=r.recognize_google(audio, language='en-in')
print(f"User said:: {query.title()}\n")
except Exception as e:
speak("Say that again please....")
return "None"
return query
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishme():
hour=int(datetime.datetime.now().hour)
hour1=datetime.datetime.now().strftime("%H:%M:%S")
if hour>=4 and hour<12:
speak("Good morning Vishaall!")
speak(f"the time is{hour1}")
elif hour>=12 and hour<18:
speak("Good afternoon Vishaall!")
speak(f"the time is{hour1}")
elif hour>=18 and hour <20:
speak("good evening Vishaall!")
speak(f"the time is{hour1}")
else:
speak("its late in the night Vishaall!")
speak(f"the time is{hour1}")
speak("myself david... how can i be of assistance?")
if __name__ == "__main__":
wishme()
abc=True
while abc:
query = takeCommand().lower()
if "wikipedia" in query:
speak("searching wikipedia")
query=query.replace("wikipedia", " ")
results= wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
speak(results)
print(results)
sys.exit()
elif query=='goodbye':
speak("i will see you later")
abc=False
sys.exit()
elif 'open google' in query:
webbrowser.open('google.com')
sys.exit()
elif 'open youtube' in query:
webbrowser.open('youtube.com')
sys.exit()
elif 'open github' in query:
webbrowser.open('github.com')
sys.exit()
elif 'open stackoverflow' in query:
webbrowser.open('stackoverflow.com')
sys.exit()
elif 'play music' in query:
music_dir= "F:\\Music"
songs=os.listdir(music_dir)
for music in range(0, len(songs)):
print(f"{music}.{songs[music]}")
in1=random.randint(0, len(songs)-1)
os.startfile(os.path.join(music_dir, songs[in1]))
sys.exit()
elif 'play movies' in query:
movie_dir="F:\\MOVIES\\"
movies=os.listdir(movie_dir)
for vids in range(0, len(movies)):
print(f"{vids}.{movies[vids]}")
in1=random.randint(0, len(movies)-1)
os.startfile(os.path.join(movie_dir, movies[in1]))
sys.exit()
elif 'open pubg' in query:
os.startfile("H:\\Program Files\\txgameassistant\\appmarket_2784781\\AppMarket.exe")
sys.exit()
elif 'open whatsapp' in query:
os.startfile('C:\\Program Files\\WindowsApps\\5319275A.WhatsAppDesktop_0.3.1847.0_x64__cv1g1gvanyjgm\\app\\WhatsApp.exe')
sys.exit()
elif 'the time' in query:
hour1= datetime.datetime.now().strftime("%H:%M:%S")
speak(f"The time is {hour1}\n")
print(hour1)
print(time.time())
sys.exit()