【问题标题】:a virtual assistant in python with voice activation带有语音激活功能的 Python 虚拟助手
【发布时间】:2023-04-06 11:19:01
【问题描述】:

所以我在 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 assistant")
    elif hour >= 12 and hour < 18:
        speak("good afternoon, i am your virtual assistant")
    else:
        speak("hello, i am your virtual assistant")

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 wikipedia
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 assistant")
    elif hour >= 12 and hour < 18:
        speak("good afternoon, i am your virtual assistant")
    else:
        speak("hello, i am your virtual assistant")

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 "wikipedia" in query:
                speak("searching details...")
                query.replace("wikipedia", "")
                results = wikipedia.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 youtube" in query:
                speak("opening youtube")
                webbrowser.open("www.youtube.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.youtube.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 "youtube studio" in query:
                speak("opening youtube studio")
                webbrowser.open("https://studio.youtube.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 album" in query:
                speak("opening your favourite music albumb")
                webbrowser.open("https://open.spotify.com/artist/2NjfBq1NflQcKSeiDooVjY?si=XvPrJaBNSnadmX8iWTlpyw")

            elif "open google classroom" in query:
                speak("opening google classroom")
                webbrowser.open("https://classroom.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")

请提前帮我解决这个问题

【问题讨论】:

  • 建议你使用像pycharm这样的IDE来格式化代码。
  • 呃,我用 sublime text 3
  • 主要是因为你使用try: 没有完成try... except 语法。 Python 期待你完成它。另外,wrt编辑器,它与问题无关。使用您喜欢的任何文本编辑器。
  • 哦,是的,我用这种方式编写了代码,但现在问题是系统不听你说的话

标签:
python
virtual
assistant