Seite 1 von 1

Sprachassistent

Verfasst: Freitag 25. Dezember 2020, 13:21
von Python567
Habe hier einen Sprachassistenten der gestern noch funktioniert hat. Wieso geht der auf einmal nicht mehr? Es kommt nur noch "Listening..." und mehr nicht.

Code: Alles auswählen

import speech_recognition as sr
import pyttsx3
import os
import webbrowser
import wikipedia
import datetime

def func(args):
    global engine

def speak(audio):
    global engine
    engine.say(audio)
    engine.runAndWait()

def takeCommand():
     
    r = sr.Recognizer()
     
    with sr.Microphone() as source:
         
        print("Listening...")
        r.pause_threshold = 1
        audio = r.listen(source)
  
    try:
        print("Recognizing...")     
        query = r.recognize_google(audio, language ='en-us')
        print(f"User said: {query}\n")

    except Exception as e:
        print(e)
        print("Unable to Recognize your voice.")  
        return "None"

    return query

def simon():
    speak ("Simon is a t and go to. H.")
    print ("Simon is and go to the. .")
    print ("Simon ist ei. ")

def sandra():
    speak ("Sandra is a student and go to the . Sce.")
    print ("Sandra is a student and go to the . Sh")
    print ("Sandra ist eine Schülerin und geht auf das ")
   

def mum():
    speak ("Mum cooks good and lives in .")
    print ("Mum cooks good and lives in.")
    print ("Mama kocht sehr gut und wohnt in .")

def dad():
    speak ("Dad has studied math and works.")
    print ("Dad has studied math and works at i.")
    print ("Papa hat Mathe studiert und arbeitet bei i.\n")
    



def grandfather():
    speak ("Grandfather has his own Craft room in the basement to create some cool things with wood.")
    print ("Grandfather has his own Craft room in the basement to create some cool things with wood.")
    print ("Opa hat seinen eigenen Bastelraum im Keller um coole Sachen aus Holz zu entwerfen.")

def grandmother():
    speak ("Oma is a person who lives in G.")
    print ("Oma is a person who lives in .")
    print ("Oma ist eine Person die in  wohnt.")

def christmas():
    speak ("Who is here?")
    x = input ("Who is here? ")
    if x == 'mum':
        print ("hjh,h ")

    elif x == 'dad':
        print ("j,hj,hj")


engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

if __name__ == '__main__':
    clear = lambda: os.system('cls')
     
 

     
    while True:
         
        query = takeCommand().lower()
         
        if "open wikipedia" in query:
            webbrowser.open("wikipedia.com")

 
        elif 'open youtube' in query:
            speak("Ok\n")
            webbrowser.open("youtube.com")
 
        elif 'open google' in query:
            speak("Ok,\n")
            webbrowser.open("google.com")

        elif 'translate' in query:
            speak("Ok\n")
            webbrowser.open("deepl.com")
   
        
        #elif 'play music' in query or "play song" in query:
        #    speak("Here you go with music")
        #    # music_dir = "G:\\Song"
        #    music_dir = "C:\\Users\\GAURAV\\Music"
        #    songs = os.listdir(music_dir)
        #    print(songs)
        #    random = os.startfile(os.path.join(music_dir, songs[1]))
        
        elif 'time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")
            speak(f"Sir, the time is {strTime}")
            print(f"Sir, the time is {strTime}")
 
        elif 'send a mail' in query:
            webbrowser.open("outlook.live.com")

        elif 'how are you' in query:
            speak("I am fine, Thank you")
            speak("How are you, Sir")
 
        elif 'fine' in query or "good" in query:
            speak("It's good to know that your fine")
 
        elif "change my name to" in query:
            query = query.replace("change my name to", "")
            assname = query
 
        elif "change name" in query:
            speak("What would you like to call me, Sir ")
            assname = takeCommand()
            speak("Thanks for naming me")
 
        elif "what's your name" in query or "What is your name" in query:
            speak("My friends call me")
            speak(assname)
            print("My friends call me", assname)
 
        elif "who made you" in query or "who created you" in query: 
            speak("I have been created by Simon.")
             
             
        elif "calculate" in query: 
            webbrowser.open("www.google.com/search?q=rechner&rlz=1C1GCEA_deDE924DE924&oq=rechner&aqs=chrome..69i57j0i271l2.1269j0j1&sourceid=chrome&ie")
 
        elif 'explorer' in query or 'play' in query:
             
            query = query.replace("search", "") 
            query = query.replace("play", "")
            webbrowser.open(query) 

        #elif 'power point presentation' in query:
         #   speak("opening Power Point presentation")
          #  power = r"C:\\Users\\GAURAV\\Desktop\\Minor Project\\Presentation\\Voice Assistant.pptx"
           # os.startfile(power)
 
        elif "who are you" in query:
            speak("I am your virtual assistant created by Simon")
 
 
        #elif "don't listen" in query or "stop listening" in query:
            #speak("for how much time you want to stop to listening at commands")
            #a = int(takeCommand())
            #time.sleep(a)
            #print(a)
 
        elif "where is" in query:
            query = query.replace("where is", "")
            location = query
            speak("User asked to Locate")
            speak(location)
            webbrowser.open("https://www.google.de/maps" + location + "")
                    
        #elif "weather" in query:
             
        
            #api_key = "Api key"
            #base_url = "http://api.openweathermap.org / data / 2.5 / weather?"
            #speak(" City name ")
            #print("City name : ")
            #city_name = takeCommand()
            #complete_url = base_url + "appid =" + api_key + "&q =" + city_name
            #response = requests.get(complete_url) 
            #x = response.json() 
             
            #if x["cod"] != "404": 
                #y = x["main"] 
                #current_temperature = y["temp"] 
                #current_pressure = y["pressure"] 
                #current_humidiy = y["humidity"] 
                #z = x["weather"] 
                #weather_description = z[0]["description"] 
                #print(" Temperature (in kelvin unit) = " +str(current_temperature)+"\n atmospheric pressure (in hPa unit) ="+str(current_pressure) +"\n humidity (in percentage) = " +str(current_humidiy) +"\n description = " +str(weather_description)) 
             
            #else: 
                #speak(" City Not Found ")
        
 
        elif "Good Morning" in query:
            speak("A warm" +query)
            speak("How are you Mister")
            speak(assname)
 
        elif "how are you" in query:
            speak("I'm fine, glad you me that")

                
        elif 'open office' in query:
            speak("Here you go with office 365")
            webbrowser.open ("www.office.com")
        
        
        
        elif 'simon' in query:
            simon()

        elif 'sandra' in query:
            sandra()

        elif 'mum' in query:
            mum()

        elif 'dad' in query:
            dad()

        elif 'grandfather' in query:
            grandfather()

        elif 'grandmother' in query:
            grandmother()

        elif 'christmas' in query:
            christmas()
[code]