DEV Community

Cover image for Hello Python (Voice Assistant) - For Windows Users
Siddharth Singh Baghel
Siddharth Singh Baghel

Posted on • Updated on

Hello Python (Voice Assistant) - For Windows Users

App's Overview

  • In this tutorial, we will be going through a voice assistant build with python for beginners. Prerequisites for reading this article are basic knowledge of python and python package importing.

Table Of Content

Project Setup

  • I have used the VS code editor for this project. PyCharm is also equally recommended, it depends upon your personal choice which among the two to choose. But other than these two I won't strongly recommend any editor.

  • Create a file my_assistant.py.

Imports & Packages Installation

  • We will be using various python packages for creating our smart assistant. Some of them need to be installed while some are pre-installed packages.

Image description

  • The imports visible in the above image are the brain cells of our assistant 😁.

pyttsx3 - The most important package is pyttsx3.
it is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3.

## to download the package, write it in the terminal

pip install pyttsx3

## to import write this in my_assistant.py

import pyttsx3 

Enter fullscreen mode Exit fullscreen mode

speech_recognition - It allows computers to understand human language. Speech recognition is a machine's ability to listen to spoken words and identify them.

## to download the package, write it in the terminal

pip install SpeechRecognition

## to import write this in my_assistant.py

import speech_recognition as sr 
Enter fullscreen mode Exit fullscreen mode

wikipedia - Wikipedia is a Python library that makes it easy to access and parse data from Wikipedia. Search Wikipedia, get article summaries, get data like links and images from a page, and more.

## to download the package, write it in the terminal

pip install wikipedia

## to import write this in my_assistant.py

import wikipedia as wk 
Enter fullscreen mode Exit fullscreen mode

datetime - For accessing date and time.

## to import write this in my_assistant.py

import datetime as dt
Enter fullscreen mode Exit fullscreen mode

webbrowser - For accessing the browser.

## to import write this in my_assistant.py

import webbrowser as wb
Enter fullscreen mode Exit fullscreen mode

os - For accessing operating system's operations.

## to import write this in my_assistant.py

import os
Enter fullscreen mode Exit fullscreen mode

Features and More

  • This can be extended immensely as per the developer's interest. I have provided basic steps with a few features in this tutorial. But you can fork the GitHub Repo for various features.

Feature - 1 (Listening)

  • We will make our assistant listen us firstly 😁 .
  • takeCommand() function is ears for our assistant.
def takeCommand():
    r = sr.Recognizer()  # sr-> speech_recognition
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)  # for cancelling the backgroud noise 
        r.pause_threshold = 1
        print("Listening...")
        maudio = r.listen(source)

    print("Listened")   
    try: 
        print("Recognizing...")
        query = r.recognize_google(maudio, language = 'en-in')  # this step will recogize the text you spoke and store it into var query

    except Exception as e:
        query = "Say that again please!"

    return query        
Enter fullscreen mode Exit fullscreen mode
  • Now, you can use the query returned from takeCommand() for various tasks.

Feature - 2 (speaking)

  • It's time for our assistant to respond, because conversation must be two sided. 😊
  • speakBaby() function will let our assistant speak.
def speakBaby(audio):
    engine = pyttsx3.init('sapi5')  # learn more about sapi5 in pyttsx3 documentation 
    voices = engine.getProperty('voices') # for getting all the voices (male and female)
    engine.setProperty('voice', voices[1].id) # set a voice with your choice
    engine.say(audio)
    engine.runAndWait()

Enter fullscreen mode Exit fullscreen mode

Feature - 3 (Wishing)

  • wishMe() function allows our assistant to wish us according to the time of the day.
def wishMe():
    hour = int(dt.datetime.now().hour)
    if hour in range(0,12):
        speakBaby("Good morning Sid .. How may I help you !")
    elif hour in range(12,17):
        speakBaby("Good Afternoon Sid .. How may I help you !")
    else:
        speakBaby("Good Evening Sid .. How may I help you !")        
Enter fullscreen mode Exit fullscreen mode
  • We used speakBaby() for our assistant to speak out time.

Note : These are the basic features for a working assistant and for more features checkout the GitHub repo attached below.


Voice-Assistant-Python

App's Overview

  • In this tutorial, we will be going through a voice assistant build with python for beginners. Prerequisites for reading this article are basic knowledge of python and python package importing.

Table Of Content

Project Setup

  • I have used the VS code editor for this project. PyCharm is also equally recommended, it depends upon your personal choice which among the two to choose. But other than these two I won't strongly recommend any editor.

  • Create a file my_assistant.py.

Imports & Packages Installation

  • We will be using various python packages for creating our smart assistant. Some of them need to be installed while some are pre-installed packages.

Image description

  • The imports visible in the above image are the brain cells of our assistant 😁.

pyttsx3 - The most important package is pyttsx3 it is…

Writer's Support

  • If you find the article useful show some ❀️ by staring some of my repositories and following me on dev.to and github.

Top comments (2)

Collapse
 
incrementis profile image
Akin C.

Hello Siddharth Singh,

thank you for your article.
I like how it's structured.
I also like the high level explanation of the different packages.

I was trying to get your code to work on Google Colab just for fun and I know you specifically recommend only pycharm and VS code.
I only mention this because it seems that your code regarding this documentation only works on Windows OS:
pypi.org/project/comtypes/

I stumbled across this when the following error popped up:
ImportError: cannot import name 'COMError' from '_ctypes'
(It seems that one of the packages needs the package comtypes, so I installed it)

If this is true, I recommend that your article mentions that it's only for Windows.
It might help provide more clarity.

To reproduce the error in Google Colab (for the curious):

  • Download packages
!pip install pyttsx3
!pip install SpeechRecognition
!pip install wikipedia
Enter fullscreen mode Exit fullscreen mode
  • Import packages
import pyttsx3
import speech_recognition as sr
import wikipedia as wk
import datetime as dt
import webbrowser as wb
import os
Enter fullscreen mode Exit fullscreen mode
  • Run the code from article and call
speakBaby("Good morning Sid .. How may I help you !")
Enter fullscreen mode Exit fullscreen mode
  • An error will pop up that package comtypes is needed

  • Install comtypes before the imports
    !pip install comtypes

  • Import comtypes after package installation

  • The following error should pop up
    ImportError: cannot import name 'COMError' from '_ctypes'

Collapse
 
siddharthsing profile image
Siddharth Singh Baghel

@incrementis,

Thanks for reviewing the article carefully and I am glad you find it useful. Also I apologize for the inconvenience. I will mark that the article is meant for the windows users only.