Simple Voice Assistant with Python
Python, as we know is a versatile language with a lot of packages at its disposal. With the help of these packages a simple voice assistant can be done easily. This is a simple Voice Assistant which will search files on windows as well as search on Google, depending on the command. Let’s see how to get this done!
Package Installation
We will be requiring a few packages as below:
1. speech recognition — for recognizing speech. This will require an internet connection as this will be using Google Web Speech API for recognizing words ( conda install -c conda-forge speechrecognition)
2. pyttsx3 — for converting speech to text. Just to know whether the Google API has properly recognized words or not ( pip install pyttsx3 ( This will run on Anaconda. But first please install pip — conda install pip))
3. webbrowser — for searching the words on Google Chrome. No need for installation as it is standard part of python.
4. subprocess — for searching the words on local Window machine. No need for installation as it is standard part of python.
5. tkinter — for GUI creation. No need for installation as it is standard part of python.
With this we are armed with all the packages and ready to move forward.
Creating Package
Now the code is pretty much simple. But to make this a bit more exciting, I added a bit of packaging and tkinter 😅. First let’s see how to create a package in Python.
When there are a lot of file/module, we tend to divide it and put them into subfolders. Python takes this a bit further in a logical way where we can import these as regular packages in our code. Physically, a package is actually a folder containing one or more module files. The below steps show the creation of a package:
1. Create a new folder (in my case VoiceSearch) .
2. Inside this folder create another subfolder (in my case voicesearchpackage).
3. Create an empty __init__.py
file in the voicesearchpackage folder.
4. For our use case this __init__.py
is empty as we are importing all the modules. You can import only certain modules if needed by importing them in the __init__.py
file.
You can change the names of the folder as per your choice. The only thing is that the module and the __init__.py
must be inside that subfolder.
The main code for the Voice Search is in VoiceSearch.py and as below:
# importing all packages
import speech_recognition as sr
import subprocess
import webbrowser
import pyttsx3# creating a class
class VoiceSearch:
# function initializing all the voice recognition
def listenSource(self):
# initializing text to speech engine
engine = pyttsx3.init()
# initializing text to speech speed
engine.setProperty("rate", 150)
# initializing speech recognition
r = sr.Recognizer()
# initializing speech recognition mic
mic = sr.Microphone()
MyText = ''
flag = 0
# till no proper response is got keep on asking for command
# you can modify this part to your liking !
while flag == 0:
with mic as source:
try:
# Voice recognition part
engine.say(' Listening to Command')
engine.runAndWait()
# adjusting ambient noise
r.adjust_for_ambient_noise(source)
# listening for 15 seconds
# you can increase this length
audio = r.listen(source,timeout = 15)
# google api giving the recognized words
MyText = r.recognize_google(audio)
engine.say('Command Recorded' + MyText)
engine.runAndWait()
print('Command Recorded' + MyText)
flag = 1
except sr.UnknownValueError:
engine.say('Unknown Command')
engine.runAndWait()
MyText = 'Unknown Command'
return MyText.lower()
# function to create task based on words received
def voiceSearch(self):
MyText = self.listenSource()
print("Command given: " + MyText)
# initializing the chrome path
chromedir= 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# for windows search
if MyText == 'windows search':
query_string = self.listenSource()
if query_string == 'Unknown Command':
print('Cannot perform search as Command was unknown')
else:
# will search for all files in C drive
local_path = r"C:"
subprocess.Popen(f'explorer /root,"search-ms:query={query_string}&crumb=folder:{local_path}&"')
# for google search
elif MyText == 'google search':
search_term = self.listenSource()
if search_term == 'Unknown Command':
print('Cannot perform search as Command was unknown')
else:
# setting the search to google and opening the chrome browser
url = "https://www.google.com.tr/search?q={}".format(search_term)
webbrowser.get(chromedir).open(url)
Creating GUI
The next step is to create a GUI. Tkinter is a ready available package in Python where you can create GUI. The GUI is really simple where you just have a button which will trigger the speech recognition code.
This is not much of a GUI really 😅. You can definitely build upon this to make it more interactive. The code for this is in voiceSearchGui.py and as below:
# importing all packages
import tkinter as tk
from voicesearchpackage import VoiceSearch# voice search function
def startVoiceSearch(windowHandle):
windowHandle.withdraw()
voiceObj = VoiceSearch.VoiceSearch()
voiceObj.voiceSearch()
windowHandle.deiconify()
# creating GUI and placing button with little formatting
mainWindow = tk.Tk()
mainWindow.resizable(0,0)
mainWindow.title('Voice Search')
mainWindow.geometry('200x200')
startButton = tk.Button(mainWindow,text = "Start Conversation", width = 15, height = 5,command = lambda: startVoiceSearch(mainWindow))startButton.place(relx = 0.5,rely = 0.5,anchor = tk.CENTER)mainWindow.mainloop()
Working
The code works as below:
1. First run the voiceSearchGui.py file which will open the GUI.
2. Click on the ‘Start Conversation’ button which will trigger the Voice Recognition function.
3. The Voice Recognition will start after you hear the voice ‘Listening To command’ and will be listening for 15 seconds.
4. Say either ‘Windows Search’ or ‘Google Search’ for the respective search functionality. This keyword can be changed as per your choice! If any other is chosen it won’t work and the program will exit😅.
5. After the assistant confirms the Search command it will ask for the keyword to search.
6. Voila! the Folder search will open for windows search and Google search for any web search.
The obvious drawback is that it requires stable internet connection! Else you will be listening to an endless loop of ‘Unknown Command’ 😅.
In this way I was able to create a simple Voice Assistant program using Python. I hope that this post was able to help you understand module creation, voice recognition, text-to-speech and a little bit of tkinter! Let me know your thoughts about it!
References:
- A lot of googling amongst which the major sources were medium.com, geeksforgeeks.org, stackoverflow.com
Originally published at http://evrythngunder3d.wordpress.com on July 11, 2021.