Python programming blog

Tuesday, March 23, 2021

Desktop voice assistant using 40 lines of python code..!

In this blog, we are going to learn about, How to create a desktop assistant using python programming. Using this script you can do anything on your system. Suppose if you want to send a message to a friend on WhatsApp. You can use this script.





The video demonstration is available on youtube: Desktop assistant using python

Modules used:

1.pip install speech_recognition
2.datetime
3.subprocess
4.pip install pywhatkit
5.pip install pyttssx3 ( text to speech )
6.webbrowser



Importing the required libraries :

import speech_recognition as sr 
import datetime
import subprocess
import pywhatkit
import pyttsx3
import webbrowser


Initiating the engines for performing text to speech & speech to text :

engine=pyttsx3.init()

voices=engine.getProperty('voices')

engine.setProperty('voice',voices[1].id)

recognizer=sr.Recognizer()


Declaring the function for recognizing the voices using speech_recognition library :

def cmd():

    with sr.Microphone() as source:

        print("Clearing background noises...Pleasw wait")

        recognizer.adjust_for_ambient_noise(source,duration=0.5)

        print('Ask me anything..')

        recordedaudio=recognizer.listen(source)

    try:

        text=recognizer.recognize_google(recordedaudio,language='en_US')

        text=text.lower()

        print('Your message:',format(text))


    except Exception as ex:

        print(ex)


Inserting the commands to your voice assistant and logics building

 if 'chrome'in text:

        a='Opening chrome..'

        engine.say(a)

        engine.runAndWait()

        programName = "chrome.exe"

        subprocess.Popen([programName])

    if 'time' in text:

        time = datetime.datetime.now().strftime('%I:%M %p')

        print(time)

        engine.say(time)

        engine.runAndWait()

    if 'play' in text:

        a='opening youtube..'

        engine.say(a)

        engine.runAndWait()

        pywhatkit.playonyt(text)

    if 'youtube' in text:

        b='opening youtube'

        engine.say(b)

        engine.runAndWait()

        webbrowser.open('www.youtube.com')


Looping this function:

while True:

    cmd()


Thanks for reading this article..!💓



1 comment: