adplus-dvertising

Mickey: The voice Assistant

  • Priyanshu Gupta
  • Apr 07, 2021
  • Python Programming
Mickey: The voice Assistant title banner

In our previous blog Python Essentials for Voice Control : Part1 we have learned about the Speech Recognition Library of Python So that we are able to understand the concept and applications of Speech Recognition.

 

In one of our previous blogs Python Essentials for Voice Control: Part2 we have learned about the Text to Speech Conversion Library of Python So that we are able to understand the concept and applications of text to Speech Conversion.

 

Now the time to apply concepts of Speech Recognition & Text to Speech Conversion using Python programs. It will be a creative project for us. 

 

Let’s get started with coding our Personal Voice Assistant: Mickey.

 

  • Install Essential libraries 

 

Windows: Open Command Prompt and install all the below libraries.Linux: Open Linux Bash Shell and install all the below libraries.

 


pip install pyttsx3
pip install speech_recognition as sr
pip install datetime
pip install wikipedia
pip install pywhatkit
pip install os 

 

  • Import the required Libraries :

 

  1. Pyttsx3 library will be used for Text to Speech Conversion with offline support. 

  2. The Speech_recognition library will be used for understanding & listening commands with support of online & offline APIs and Engines.

  3. Datetime library will be used for manipulating date & time with arithmetic support.

  4. Wikipedia library will be used for accessing data from Wikipedia to search Wikipedia, get article summaries, get data like links and images from the page.

  5. Pywhatkit library will be used for sending WhatsApp messages at certain times, play youtube videos, perform google searches, get information on a particular topic.

  6. Os library will be used to interact with Operating Systems. It provides portable ways of using Operating System dependent functionalities.


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

  • Define a name for user :

 

Here, in the code, we have assigned the name of the user whose requests Mickey is going to listen and take action accordingly.

 


BOSS = "KingsMan"
print("Initialising Mickey...")

 

  • Define Function to make Mickey Speakable :

 

As per the previous blogs we use pyttsx3 to make the machine program speakable using Text to Speech conversion functionality. We define speak( )  function which takes text as input, which Mickey will say.

 

We initialize our Engine which will take input as Text and convert it into machine Voice, then we set properties for the Voice of Mickey, then we make the engine wait to take till the next text input.

 


def speak(text):
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice',voices[1].id)
    engine.setProperty('rate', 125 )
    engine.setProperty('volume',1.0)
    engine.say(text)
    engine.runAndWait() 


 

  • Define Function to wish user by Mickey :

We define wishMe( ) which will be used to wish us. As you can see we have used a datetime module to create a wishing pattern. There can be several ways to wish users, you can create your own way also.

 

We pick the current time and then make Mickey speakable according to time. We create wishes according to time like Good Morning, Good Afternoon & Good Evening with adding the name of the user at the end, then Mickey introduces itself using speak( ) function.

 


def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning" + BOSS)
    elif hour>=12 and hour<18:
        speak("Good Afternoon" + BOSS)   
    else:
        speak("Good Evening" + BOSS) 
    speak("I am Mickey. How can I help you ?")   

 

  • Define a function to make Mickey listenable :

 

As per the previous blogs we use speech_recognition to make the machine listenable and understand the command from the user using Speech Recognition functionality.  

 

We initialize our command as null in the beginning, then define the recognizer( ) function to make it understand the command from the user, then we define the source as Microphone using Microphone( ) function of machine which will be the medium between user & machine (Mickey), then we listen to a command from source using listen( ) function. 

 

We take precautions in coding Mickey using Try & Except blocks from Exception Handling. Here, we use recognize_google( ) API to fix the format & language of understanding the command and then print the command that the machine listens.  If the user doesn't give any command then Except block keeps on passing the Empty Code to the function using the pass as return value.

 


def listenCommand():
    command=0
    hear = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = hear.listen(source)

    try:
        print("Recognizing...")
        command = hear.recognize_google(audio, language='en-in')
        print(f'{BOSS} : {command}\n')

    except:
            pass

    return command 

 

  • Initialize Mickey :

 

From this point, we start our Voice Assistant Mickey, where we use both user-defined above speak( ) & wishMe( ) functions. Here, we use Python’s Interpreted behavior very smartly by using these just before the main( ) function.

 


speak("Initialising Mickey..........")
wishMe()

 

  • Define Actions for the MICKEY :

Let’s start defining the functionalities for the Mickey in main( ) function. We use Conditional Statements  to set the conditions for taking a particular action. 

 

First, we set the format of taking commands from the user in string format in lower case using lower( ) function. 

 

Action 1:  We use the Wikipedia module in this block to search results by fixing the Keywords who is and what is to set the priority of running this block.  Here, replace will replace the keywords with empty string using replace( ) function in command and pass the remaining sentence to the summary( ) function, then it will pick the first line of the result from Wikipedia & return it to the user.

 



def main():
    command = listenCommand()
    command=str(command).lower()

    if ('who is' in command) or ('what is' in command):
        speak('Searching Wikipedia...')
        command = command.replace("who is","")
        command = command.replace("what is","")
        results = wikipedia.summary(command, sentences = 1)
        print("Mickey:",results)
        return speak(results) 

 


Action 2: We use the pywhatkit module in this block to Search & Play the video on YouTube by fixing the keyword play to set the priority of running this block. Here, playonyt( ) function will take the command after replacing the keyword by an empty string from the command.

 

 


elif 'play' in command:
        speak('Playing...')
        command = command.replace('play','')
        print(command)
        return pywhatkit.playonyt(command)

 


Action 3:  We use pywhatkit module in this block to search the web results on Browser by fixing the keyword google to set the priority of running this block. Here search( ) function will take the command after replacing the keyword by an empty string from the command.

 

 


elif 'google' in command:
        speak("Googling...")
        command = command.replace('google','')
        print(command)
        return pywhatkit.search(command)


 

Action 4: We use the os module in this block to give commands to run Visual Studio Code on the Operating System of our Machine by fixing the keyword open visual studio to set the priority of running this block. Here, the system( ) function will run the command code which is defined for Visual Studio Code according to the Operating System.

 


elif 'open visual studio' in command:
        speak("Opening...")
        return os.system("code") 


 

Action 5: We use the built-in module of Python (site module) in this block to stop the execution of the program by fixing the keyword stop mickey to set the priority of running this block. Here, exit( ) will be used to stop the execution of the program immediately.

 


elif 'stop mickey' in command:
        speak("Suiciding...")
        return exit()
       
    else:
        return 0 


 

If there is no command passed by the user then else block of main( ) will return zero as a null value or no output to the user.

 

  • Mickey in Active State :

To keep Mickey in an active state until you stop it, we apply a loop on this program to take command again & again after completing the previous action. We apply a while loop with always True value on a main( ) function to keep on asking to take actions. 


while True:
    main()  


                                   

Conclusion

 

We successfully created our Virtual Assistant Mickey for our personal use. We can define a lot of actions & functionalities according to our requirements. 

 

The approach that we used to create Mickey, s very much a basic level of programming. We use only the Functional Programming & Interpreted behavior of Python to program it. It is very easy to understand & functions effectively. 

 

We need to face some challenges to improve Mickey’s Actions. Those Challenges can be : 

 

  1. Apply Object-Oriented Programming to manage programs on a larger Scale.

  2. Apply Data Structures & Algorithms Concepts to Optimize Time & Space Complexities of Program.

  3. Apply Machine Learning Concepts to get great predictability of taking Actions with commands.

  4. Try to create Mickey in another Programming Language which is faster than Python, like C, C++, JAVA, etc.

  5. Try to create a GUI for Mickey.

  6. Try to differentiate the functionalities of Mickey for Offline & Online Mode.

  7. Try to reduce the lines of code with Python’s Advanced Functions & Data Structures.

  8. Try to stop, pause, Play, etc. on YouTube while playing video.

  9. Try to close Browser  & Visual Studio Code.

 

There can be many more challenges that you can take to improve Mickey & enhance your Skillsets.

 

To follow this Code let’s become Contributors at GitHub.

 

“Expand your limits to being the IRONMAN of your own World & GOD of Virtual World. ”

0%

Comments