所以我在python上做了一个虚拟助手

qhhrdooz  于 2022-11-28  发布在  Python
关注(0)|答案(3)|浏览(125)

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

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

因此,在运行后,它会说它需要的东西,它也会打印它需要的东西,但当它听到用户告诉它什么,它只是不回应的,并继续打印的听力...识别...网络连接问题...

xa9qqrwz

xa9qqrwz1#

你的代码似乎是正确的!在函数speak()有缩进错误我猜。
你有没有试着在连接互联网时说出这些命令?

zte4gxcn

zte4gxcn2#

  1. speak()函数中存在缩进错误。
    1.在takeCommand()函数中,它应该是r.recognize_google而不是r.recognition_google
    编辑:
    1.我没试过所以我不知道。
    1.加:
elif "search on google" in query:
                speak("searching on google")
                line = query.strip("search on google ")
                webbrowser.open("www.google.co.in"+"/search?q="+line)

并确保删除or "google" in query在开放的谷歌elif或改变整个事情:

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

nue99wik3#

好的!我试着运行你的代码,但是它一直在听,尽管我已经通过语音给了它一些命令,但是它只是停留在那里enter image description here

相关问题