所以我在 python 中制作了一个虚拟助手,当你通过它的名字调用它时,系统被激活,然后你可以根据你所说的工作,但它显示了一个错误的缩进,即使这里没有任何错误的代码
ACTIVATE = False
engine.setProperty('voice',voices[0].id)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
def wish():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("good morning, i am your virtual istant")
elif hour >= 12 and hour < 18:
speak("good afternoon, i am your virtual istant")
else:
speak("hello, i am your virtual istant")
def Command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
query1 = r.recognize_google(audio, language='en-in')
#return query1
if ACTIVATE == False:
query1 = Command().lower()
if "ACTIVATE" in query1:
ACTIVATE = True
它显示的错误像
File "C:\Users\Vatsa\OneDrive\Desktop\New folder\New folder\VIRTUAL ASSISTANT ACTIVATION.py", line 47
if ACTIVATE == False:
^
IndentationError: unexpected unindent
如果在代码的任何其他部分有错误,这里是完整的代码
import speech_recognition as sr
import
import datetime
import pyttsx3
import webbrowser
import random
import os
import pyaudio
#text to speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
print(voices)
#print(voices)
ACTIVATE = False
engine.setProperty('voice',voices[0].id)
def speak(audio): #here audio is var which contain text
engine.say(audio)
engine.runAndWait()
def wish():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("good morning, i am your virtual istant")
elif hour >= 12 and hour < 18:
speak("good afternoon, i am your virtual istant")
else:
speak("hello, i am your virtual istant")
def Command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
query1 = r.recognize_google(audio, language='en-in')
#return query1
if ACTIVATE == False:
query1 = Command().lower()
if "ACTIVATE" in query1:
ACTIVATE = True
if ACTIVATE == True:
#now convert audio to text
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("recognizing...")
speak("recognising")
query = r.recognize_google(audio, language='en-in')
print(f"user said:{query}\n")
except Exception:
speak("i dont understand")
print("i dont understand..") #for error handling
return "none"
return query
#for main function
if __name__ == "__main__":
wish()
while True:
query = takeCommand().lower()
if "" in query:
speak("searching details...")
query.replace("", "")
results = .summary(query, sentences = 2)
print(results)
speak(results)
elif "what's up" in query:
stMsgs = ['asif now im only helping you with whatever you need me for', 'I am fine!, lets talk about you are you fine?', 'oh, nothing','i am okay ! How are you']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "how are you" in query:
stMsgs = ['i am fine, how about you!', 'I am fine!, lets talk about you are you fine?', 'I am nice and in full mood of helping you','i am okay ! How are you']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "fine" in query or "im good" in query or "even im okay" in query or "nice" in query or "yeah" in query or "yes im nice" in query or "yes" in query:
speak("oh that's nice, anyways what can i help you with today?")
if "whats the weather " in query:
speak("looking for the weather...")
#r = sr.Recognizer()
results =webbrowser.open("https://www.google.com/search?rlz=1C1CHBF_enIN911IN911&biw=1536&bih=763&sxsrf=ALeKk02WEeO5ibqsklVfK4AaRYshJYil7g%3A1598262964246&ei=tI5DX63LDrSX4-EP_bOA8A4&q=google+weather&oq=googweather&gs_lcp=CgZwc3ktYWIQAxgAMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB4yBggAEAcQHjIGCAAQBxAeMgYIABAHEB46BAgAEEdQ7i1Y0jJgwTxoAHACeAGAAYEDiAHmBZIBBzEuMi4wLjGYAQCgAQGqAQdnd3Mtd2l6wAEB&sclient=psy-ab")
elif "hello" in query or "hi" in query or "hey" in query:
stMsgs = ['hi how can i help you today', 'hello there, what can i do for you', 'hey, what can i help you with today','hello, what do you want me to do today']
ans_q = random.choice(stMsgs)
speak(ans_q)
ans_take_from_user_how_are_you = takeCommand()
elif "what's the time" in query:
speak("the time is")
webbrowser.open("https://www.google.com/search?q=time&rlz=1C1CHBF_enIN911IN911&oq=time&aqs=chrome..69i57j0l5j69i61l2.584j0j7&sourceid=chrome&ie=UTF-8")
elif "your name" in query or "who are you" in query:
speak("my name is jarv")
elif "who created you" in query or "how were u made" in query:
speak("i am created by vatsal, and i owe a big thanx to him")
elif "open amazon" in query:
speak("opening amazon.com")
webbrowser.open("https://www.amazon.in/")
elif "open " in query:
speak("opening ")
webbrowser.open("www..com")
elif "open google" in query:
speak("opening google")
webbrowser.open("www.google.co.in")
elif "open my channel" in query:
speak("opening dietichen")
webbrowser.open("https://www..com/c/dietichen")
elif "music from laptop" in query or "music" in query:
speak("ok playing music")
music_dir = "./music"
musics = os.listdir(music_dir)
os.startfile(os.path.join(music_dir, musics[0]))
elif "video from laptop" in query or "play videos" in query:
speak("ok playing music")
video_dir = "./video"
videos = os.listdir(video_dir)
os.startfile(os.path.join(video_dir, videos[0]))
elif "open my studio" in query or " studio" in query:
speak("opening studio")
webbrowser.open("https://studio..com/channel/UCNcVMyq5JyZ5V_TXN6KUgbA")
elif "open my gmail" in query:
speak("okay, opening your gmail")
webbrowser.open("https://mail.google.com/mail/u/0/#inbox")
elif "open spotify" in query:
speak("opening spotify")
webbrowser.open("https://open.spotify.com/")
elif "play dance monkey" in query:
speak("okay, playing dance monkey from the artist tones and i")
webbrowser.open("https://open.spotify.com/track/2XU0oxnq2qxCpomAAuJY8K?si=hqT3uLOtQnmV9glVfKP84g")
elif "open my favourite songs al" in query:
speak("opening your favourite music alb")
webbrowser.open("https://open.spotify.com/artist/2NjfBq1NflQcKSeiDooVjY?si=XvPrJaBNSnadmX8iWTlpyw")
elif "open google clroom" in query:
speak("opening google clroom")
webbrowser.open("https://clroom.google.com/u/3/h")
elif "goodbye" in query or "goodnight" in query or "shut up" in query or "bye" in query or "get lost" in query or "stop" in query:
speak("good bye")
quit()
elif "shutdown" in query:
speak("shutting down")
os.system('shutdown -s')
else:
speak("sorry i dont understand")
请帮我提前感谢
你试过在try: query1 = r.recognize_google(audio, language='en-in')
后面放一个except: p
吗?它可能需要它,并一直认为你在try:
函数中。
尝试这个测试和工作,确保所需的所有 python 包
import pyttsx3
import speech_recognition as sr
import webbrowser
import os
import sys
import datetime
import pywhatkit
import time
import pyaudio
import
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id )
engine.setProperty('volume', 2.0)
engine.setProperty('rate', 160)
def fun_talk(query):
engine.say(query)
engine.runAndWait()
engine.stop()
def get_audio(): #Take Command
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
said = ""
try:
print("Recognizing....")
said = r.recognize_google(audio)
print(said)
except Exception as e:
#print("Exception: " + str(e))
p
return said.lower()
def wish_user():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
fun_talk("Good Morning")
elif hour >= 12 and hour < 18:
fun_talk("Good Afternoon")
else:
fun_talk("Good Evening")
time.sleep(0)
# set the wake word
wake = "ava"
if __name__ == '__main__':
wish_user()
while True:
clear = lambda: os.system('cls')
print("waiting for wake word...")
query = get_audio()
# to listen for wake word
if query.count(wake) > 0:
print("is listening...")
query = get_audio()
if 'i am done' in query or 'go offline' in query:
print("do you want me to shutdown?...")
print("waiting for answer?...")
fun_talk("do you want me to shutdown?")
cmd = get_audio()
if 'no' in cmd:
continue
else:
fun_talk("commencing shutdown protocol. going offline")
fun_talk("goodbye for now")
sys.exit()
elif'' in query:
fun_talk('Searching Wikipedia')
query = query.replace("", "")
results = .summary(query, sentences=3)
fun_talk("According to Wikipedia")
print(results)
fun_talk(results)
clear()
#play any songs from
elif 'play' in query:
song = query.replace('play', '')
fun_talk(f'Playing {song} ')
print(song)
pywhatkit.playonyt(song)
clear()
本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处
评论列表(10条)