Welcome to Day One of my 21-day project series!
Today, I’ll be introducing you to my speech command bot.
As humans, we all have a need to be heard.
But just like any normal human, it seems like nobody at my house ever listens to me.
So, I’ve come up with a simple yet powerful solution to make me feel heard and in control.
Contents
Project contents
I am going to keep it simple as I only have time for the day. Also, I can add features later on when I feel something worth adding.
Ok, so I’ll call this speech command bot Doraemon. Because I want it to be like my friend. It will be doing two major tasks:
- Search through the web.
- And open some daily used desktop apps.
It will do both these on voice command and will also answer in voice.
I’ll not be giving a line-by-line explanation. Cause it makes the post too long.
Full Source Code
import webbrowser
import os
import speech_recognition as sr
import pyttsx3
class Doraemon:
"""This is a simple speech command bot. It is made with few functions and elif ladder. It can take speech input.
It will perform two categories of tasks:
1. searching through the web.
2. opening some apps. Both on voice command. It will also reply to user by talking.
It has four major methods:
1. __init__: which is an empty constructor
2. speak: that speaks the text input and also prints it on the console.
3. takeCommand: It listens with the default microphone of the system and returns the spoken text
4. run_bot: This is the interface user will get in the instance of this class. It has an infinite while loop to take commands and execute them until the user asks to stop.
Libraries used:
webbrowser: to browse through web
os: to open applications
speech_recognition: to take voice commands
pyttsx3: to give voice outputs
"""
def __init__(self):
pass
def speak(self, text:str) -> None:
engine = pyttsx3.init()
print(text)
engine.say(text)
engine.runAndWait()
return None
def takeCommand(self) -> str:
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"User said: {query}\n")
return query
except Exception:
print("Say that again please...")
return ""
def open_youtube(self) -> None:
self.speak("What should I search for? ")
cm = input()
self.speak(f"Searching in youtube {cm}")
webbrowser.open(f"https://www.youtube.com/results?search_query={cm}")
def open_google(self) -> None:
self.speak("What should I search for? ")
cm = input()
self.speak(f"Searching in google {cm}")
webbrowser.open(f"https://www.google.com/search?q={cm}")
def open_notepad(self) -> None:
self.speak("Opening Notepad")
os.system("C:\\Windows\\notepad.exe")
def open_vs_code(self) -> None:
self.speak("Opening VS Code")
os.startfile("C:\\Users\\maitr\\AppData\\Local\\Programs\\Microsoft VS Code2\\Code.exe")
def run_bot(self) -> None:
self.speak("How can i help you buddy?")
while True:
instruction = self.takeCommand().lower()
if instruction == "":
continue
elif "youtube" in instruction:
self.open_youtube()
elif "google" in instruction:
self.open_google()
elif "notepad" in instruction:
self.open_notepad()
elif "vs code" in instruction:
self.open_vs_code()
elif "quit" or "exit" in instruction:
self.speak("Ok bye bye have a great time")
break
elif "thanks" in instruction:
self.speak("NO thanks in friendship")
else:
self.speak("Invalid request")
my_buddy = Doraemon()
my_buddy.run_bot()
Experience of the day
In case you don’t know this project is a part of the brand new series that I have started. #21days21projects in python. To read more about it you can check out this page.
And for those who already know here’s my experience of the day.
I was super excited to do the challenge today.
It felt like I can finish 21 projects in the day. But obviously, I can’t and even if I can it’s not about finishing them it’s about being consistent.
Also, think I have made the step-by-step explanation quite long. Do let me know what you think about it in the comment section below.
Tell me how was your experience on the very first day of this challenge.
Would love to listen…
Happy coding…
And wait, if you have some ideas for the upcoming projects do let me know that too.