Search code examples
pythonvoice-recognition

voice assistant that can open applications with the help of voice commands in python


Hey everyone, I've been trying to build a voice assistant that can open applications with the help of voice commands in python. Since I'm just a starter I only tried it with google and Wikipedia but it's not working. It only opens Wikipedia, even when I'm telling him to open Google. Can anyone please help me with that? I am on a mac by the way. Thank you very much

my code:

import speech_recognition as sr
import webbrowser

speech_engine = sr.Recognizer()


with sr.Microphone() as micro:
    print("Recording...")
    audio = speech_engine.record(micro, duration=5)
    print("Recognizing...")
    text = speech_engine.recognize_google(audio, language="de-DE")
    print(text)
if speech_engine == "Open Google":
    webbrowser.open("https.//google.com")
else:
    webbrowser.open("https://wikipedia.org")´´´

Solution

  • I don't really know if that is a good answer but I found out that the assistant has a problem with executing the command used in the first if-statement. All other commands in the elif-statements work fine. That is now my code:

    import speech_recognition as sr
    import webbrowser
    
    speech_engine = sr.Recognizer()
    
    
    with sr.Microphone() as micro:
        print("Recording...")
        audio = speech_engine.record(micro, duration=5)
        print("Recognizing...")
        text = speech_engine.recognize_google(audio, language="en")
        print(text)
    if text == 'come on':
        print("I am sorry, Sir")
    elif text == 'open Wikipedia':
        webbrowser.open("https://wikipedia.org")
        print("opening Wikipedia")
    elif text == 'open Google':
        webbrowser.open("https://google.com")
        print("opening Google")
    elif text == 'open stack overflow':
        webbrowser.open("https://stackoverflow.com")
        print("opening Google")
    else:
        print("Unrecognized Command")
    ´´´