Python tutorial creating voice assistant using python

April 06, 2022

In this article i will explain you step by step how you can create voice assistant using python

Step 1: Install necessary python library which gonna help in creating voice assistant in python

  • Install pyttsx3 this is text to speech conversion library in python
   pip install pyttsx3
  • Install SpeechRecognition library this library will recognize our voice and convert to text
   pip install SpeechRecognition
  • Install webbrowser library this library helps in searching from google
   pip install webbrowser
  • Install pyaudio this library is dependency of SpeechRecognition API
  #To install on linux ubuntu

  #To install pyaudio if you are using linux ubuntu run this command
   sudo apt-get install portaudio19-dev python-pyaudio

   #After running above command run the following command
   sudo pip3 install PyAudio
   # To install on windows and macos user
   pip3 install pyaudio

Step 2: Once all the library installed successfully copy paste following code

  • Importing python library and initilizing pytsx3

     #importing all the required library
     import pyttsx3
     import speech_recognition as sr
     import webbrowser
     #initializing pyttsx3 and setting voice language as english
     engine = pyttsx3.init()
     voices = engine.getProperty('voices')
     en_voice_id = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0"
     engine.setProperty('voice', en_voice_id)
  • Defining function which we would call on the basis of required work to be done

       #this function would turn text to audio using pyAudio engine which we have initialized above
       def audioOutput(audio):
       #this function would take audio input and convert it to text and return that text
       def audioInput():
           r = sr.Recognizer()
           with sr.Microphone() as source:
    	         r.pause_threshold = 1
    	         audio = r.listen(source)
    	         query = r.recognize_google(audio, language ='en-in')
    	         print(f"User said: {query}\n")
          except Exception as e:
    	         print("Unable to Recognize your voice.")
    	         return "None"
          return query
       #this function would be called first to welcome and greet user
       def greetUser():
           audioOutput("Hi, I am your Assistant, How may i help you today?")
       if __name__ == '__main__':
           while True:
              query = audioInput().lower()
              if 'search on google' in query
                  speak("Searching on google\n")