Virtual Assistance using Python on Ubuntu

Spread the love

Last few weeks, I have wrote a virtual assistance using Python. I do not know Python language before this. So i picked it up along the way. I am certainty not sure if the codes I have written conform to the best practice, but at least it works. I have decided to share with you guys the codes, would be beneficial for those would like a jump start.

1. Installation on Terminal

Given that I was coding on Ubuntu, I needed to run the following commands to install the necessary libraries for the program to work.

sudo apt-get install portaudio19-dev python-all-dev python3-all-dev && sudo pip install pyaudio
sudo apt-get install -qq python python-dev python-pip build-essential swig libpulse-dev
pip install –upgrade pip

sudo pip install SpeechRecognition
sudo pip install gTTS
sudo apt-get install mpg321

2. Get Started using Python – Speech Recognition

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
  while True:
    wakeup() # call to wake up method def

The above codes uses Google’s Speech Recognition API. It is free and easy to use. The while True method will keep the program running alive until you manually exit or kill the process.


2. Text to Speech

from gtts import gTTS
import os

def speak(speech):
  tts = gTTS(text=speech, lang=’en’)“temp.mp3”)
  os.system(“mpg321 temp.mp3”)

The above codes uses Google’s Text to Speech API. You will need WIFI connectivity for this to work. I have attempt to use Espeak and PYTTSX, both are not quite up to standard in my opinion. I had some difficult understanding what was spoken.

3. Finally completed

import string

def wakeup():
    user_speech = listen(10)
    if (user_speech.startswith(‘siri’)):
      speak(‘Yes. How can i help you?’)
      # do something

  except Exception:

Last but not least, you define what is to be included in the method to wake up the program. The method is enclosed with a Try-Catch all exception to always keep the program alive. All the speech is store into the user_speech variable, so you can just manipulate with that String and you can do whatever you want.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *