Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
所以我在做一个关于python的虚拟助手_Python - Fatal编程技术网

所以我在做一个关于python的虚拟助手

所以我在做一个关于python的虚拟助手,python,Python,所以虚拟助手可以工作,但当我说什么的时候,它不会听,也不会回应 listening... recognizing... network connection issue... 它只是一直在打印这个☝ 下面是我编写的代码 import speech_recognition as sr import wikipedia import datetime import pyttsx3 import webbrowser import random import os import pyaudio #

所以虚拟助手可以工作,但当我说什么的时候,它不会听,也不会回应

listening...
recognizing...
network connection issue...
它只是一直在打印这个☝

下面是我编写的代码

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)

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")

#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.recognition_google(audio, language='en-in')
        print(f"user said:{query}\n")

    except Exception:
        speak("error...")
        print("network connection issue...")     #for error handling
        return "none"
    return query

#for the 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 "open youtube" in query or "youtube" in query:
            speak("opening youtube")
            webbrowser.open("www.youtube.com")

        elif "open google" in query or "google" in query:
            speak("opening google")
            webbrowser.open("www.google.co.in")

        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, music[0]))

        elif "music from laptop" in query or "music" in query:
            speak("ok playing music")
            music_dir = "./video"
            musics = os.listdir(music.dir)
            os.startfile(os.path.join(music_dir, video[0]))

        elif "good bye" in query:
            speak("good bye")
            exit()

        elif "shutdown" in query:
            speak("shutting down")
            os.system('shutdown -s')
将语音识别作为sr导入
导入维基百科
导入日期时间
导入pyttsx3
导入网络浏览器
随机输入
导入操作系统
导入pyaudio
#文本到语音
engine=pyttsx3.init('sapi5')
voices=engine.getProperty('voices')
印刷品(声音)
#印刷品(声音)
engine.setProperty('voice',voices[0].id)
def speak(音频):#这里的音频是包含文本的变量
引擎。比如说(音频)
engine.runAndWait()
def wish():
hour=int(datetime.datetime.now().hour)
如果小时>=0且小时<12:
说话(“早上好,我是你的虚拟助手”)
elif小时数>=12且小时数<18:
讲话(“下午好,我是你的虚拟助手”)
其他:
说话(“你好,我是你的虚拟助手”)
#现在将音频转换为文本
def takeCommand():
r=高级识别器()
使用sr.麦克风()作为源:
打印(“侦听…”)
r、 暂停\u阈值=1
音频=r.listen(源)
尝试:
打印(“识别…”)
说话(“识别”)
query=r.recognition\u谷歌(音频、语言)
打印(f“用户说:{query}\n”)
除例外情况外:
说话(“错误…”)
打印(“网络连接问题…”)以进行错误处理
返回“无”
返回查询
#对于主要功能
如果名称=“\uuuuu main\uuuuuuuu”:
愿望
尽管如此:
query=takeCommand().lower()
如果查询中有“wikipedia”:
发言(“搜索详细信息…”)
query.replace(“wikipedia”,“维基百科”)
结果=wikipedia.summary(查询,句子=2)
打印(结果)
发言(结果)
elif查询中的“打开youtube”或查询中的“youtube”:
说话(“打开youtube”)
webbrowser.open(“www.youtube.com”)
elif查询中的“打开谷歌”或查询中的“谷歌”:
说话(“打开谷歌”)
webbrowser.open(“www.google.co.in”)
elif查询中的“来自笔记本电脑的音乐”或查询中的“音乐”:
说话(“可以播放音乐”)
音乐_dir=“/音乐”
music=os.listdir(music.dir)
os.startfile(os.path.join(music_dir,music[0]))
elif查询中的“来自笔记本电脑的音乐”或查询中的“音乐”:
说话(“可以播放音乐”)
音乐/U目录=“/视频”
music=os.listdir(music.dir)
os.startfile(os.path.join(音乐目录,视频[0]))
在查询中以“再见”开头:
说(“再见”)
退出()
查询中的elif“shutdown”:
说话(“关机”)
操作系统('shutdown-s')
因此,在运行它之后,它会说它需要的东西,它也会打印它需要的东西,但当涉及到听用户说什么时,它只是不回复,而是继续打印 听。。。 认识到。。。
网络连接问题…

您的代码似乎是正确的!在函数speak()中,我想存在缩进错误

你有没有试着在上网时说这些命令

  • speak()
    函数中存在缩进错误
  • takeCommand()
    函数中,它应该是
    r.recognition\u google
    而不是
    r.recognition\u google
  • 编辑:

  • 我从来没有试过,所以我不知道

  • 加:

  • 并确保在打开的google elif中删除查询中的
    或“google”,或使用以下内容更改整个内容:

    elif "google" in query and "search" not in query:
                speak("opening google")
                webbrowser.open("www.google.co.in")
    

    看起来您的问题缺少一些标签。也许,你可以在你的问题中添加一些内容,以便引起更多的注意。不,我对这些模块是新的,所以你能告诉internet连接的命令吗?internet连接意味着在说/发出命令时将你的PC连接到internet,因为
    r.recognize\u google
    使用google API,就像你的谷歌助手在安卓系统中用于语音识别一样。哦,是的,这件事很有效,谢谢你的努力,我的代码现在也很有效哦,代码中没有缩进的东西,它可能是在我复制粘贴代码的时候出现的。非常感谢你,我看不到识别的东西,它现在可以工作了,但我还有一个疑问,为什么“再见”命令不起作用?在最后一个elif中使用不带空格的
    “再见”
    。还有,你知道我可以在代码中添加什么命令来增加亮度,增加我笔记本电脑的亮度。还有,你有什么方法可以让这个东西直接从谷歌获得结果吗单独查询
    elif "google" in query and "search" not in query:
                speak("opening google")
                webbrowser.open("www.google.co.in")