求一款魔方cfop软件:python中的虚拟助手与语音激活

所以我在 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")

请帮我提前感谢

0

你试过在try: query1 = r.recognize_google(audio, language='en-in')后面放一个except: p吗?它可能需要它,并一直认为你在try:函数中。

0

尝试这个测试和工作,确保所需的所有 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()

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(697)
Python使用什么来划分代码块:使用函数简化代码块 [Python]
上一篇
基于python的图书管理系统:图书管理员-木偶-证书验证失败(Faraday::SSLError)
下一篇

相关推荐

  • vscode python 插件:使用 VSCode Python 插件开发 Python 应用

    VSCode Python插件是一个用于在Visual Studio Code中编写Python代码的插件。它提供了语法高亮、自动补全、代码检查、调试和诊断等功能,可以帮助开发者更快地编写Python代码。…

    2023-02-02 04:07:31
    0 12 69
  • python unstack()函数:使用unstack()函数实现复杂分组

    Python unstack()函数是pandas库中的一个函数,可以将pandas的DataFrame中的某一列转换成行。它的语法如下:…

    2023-03-05 04:38:31
    0 61 89
  • python语法基础知识 # 猴子吃桃 num_peaches = (num_peaches + 1) * 2# 输

    Python语法基础知识是指掌握Python编程的基本语法,包括变量、数据类型、运算符、流程控制、函数、类、模块等。下面是一些常用的Python语法示例:…

    2023-02-05 06:09:38
    0 20 76
  • python与c语言哪个好学:如何选择Python或C语言?

    Python和C语言都是流行的编程语言,它们都有自己的优势和特点。Python比C语言更容易学习,因为它的语法更简单,而且可以使用面向对象的编程思想,这使得它更容易理解和使用。此外,Python提供了大量的内置函数和模块,可以帮助开发者更快地完成任务。…

    2023-01-25 11:05:51
    0 23 17
  • 80 ab:字节反向ABCD到CDAB与 python

    关于80 ab的问题,在reverse bytes python中经常遇到,我有一个.bin 文件,我想简单地字节反转十六进制数据。例如 @ 0x10 它读取 AD DE DE C0,希望它读取 DE AD C0 DE。…

    2022-11-23 08:44:13
    0 76 64
  • 麦迪在cba首秀全场录像:在python中模拟nba选秀彩票

    关于麦迪在cba首秀全场录像的问题,在nba lottery sim中经常遇到,我正在尝试编写一个模拟彩票模拟器,作为一个思想练习和一些介绍性的 python 练习,其中每个团队将有 2 倍的几率获得第一个选择,因为他们之前的团队在积分榜上。…

    2022-11-23 08:30:02
    0 98 84
  • python 时间操作:如何利用Python时间操作提高工作效率

    Python 时间操作是指使用 Python 语言来处理时间的操作。Python 中的时间操作主要由 datetime 库提供支持,它提供了一系列函数和类来处理时间。…

    2023-03-06 07:45:55
    0 94 70
  • python程序包:如何使用Python程序包构建高效的应用程序

    Python程序包是一种用于组织Python模块的形式,它允许程序员将相关的模块组合在一起,以便更轻松地管理和使用它们。Python程序包可以包含函数、类、变量和其他Python代码,以帮助简化程序的开发。…

    2023-01-12 00:42:48
    0 54 13

发表评论

登录 后才能评论

评论列表(10条)