π― Project Objective
To create a Python-based voice-controlled assistant that listens to commands, performs actions (like searching the web or opening apps), and speaks responses aloud.
π§© 1. What Youβll Learn
- π€ Capturing voice input using
speech_recognition - π Making Python speak with
pyttsx3 - π Performing tasks like opening YouTube, searching Google, checking time, etc.
- βοΈ Handling multiple commands with simple AI logic
βοΈ 2. Required Libraries
Weβll auto-install them within the code π
# Auto install required modules
import os
import sys
import subprocess
def install(pkg):
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
for pkg in ["speechrecognition", "pyttsx3", "pyaudio"]:
try:
__import__(pkg)
except ImportError:
install(pkg)
ποΈ 3. Main Voice Assistant Code
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
import os
import subprocess
# Initialize the text-to-speech engine
engine = pyttsx3.init()
engine.setProperty('rate', 175) # Speaking speed
engine.setProperty('volume', 1.0)
def speak(text):
print(f"Assistant π§: {text}")
engine.say(text)
engine.runAndWait()
def listen():
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"You said: {query}\n")
except Exception:
speak("Sorry, I didn't catch that.")
return ""
return query.lower()
def greet_user():
hour = int(datetime.datetime.now().hour)
if hour < 12:
speak("Good morning! I'm your Python Assistant.")
elif hour < 18:
speak("Good afternoon!")
else:
speak("Good evening!")
speak("How can I help you today?")
π§© 4. Performing Real-World Actions
def perform_task(query):
if "time" in query:
time = datetime.datetime.now().strftime("%I:%M %p")
speak(f"The time is {time}")
elif "date" in query:
date = datetime.datetime.now().strftime("%A, %d %B %Y")
speak(f"Today is {date}")
elif "open youtube" in query:
speak("Opening YouTube...")
webbrowser.open("https://www.youtube.com")
elif "open google" in query:
speak("Opening Google...")
webbrowser.open("https://www.google.com")
elif "search" in query:
speak("What should I search for?")
term = listen()
if term:
url = f"https://www.google.com/search?q={term}"
speak(f"Searching for {term}")
webbrowser.open(url)
elif "play music" in query:
music_dir = "C:\\Users\\Public\\Music" # change path if needed
songs = os.listdir(music_dir)
if songs:
speak("Playing music...")
os.startfile(os.path.join(music_dir, songs[0]))
else:
speak("No music files found.")
elif "shutdown" in query:
speak("Shutting down the system. Goodbye!")
os.system("shutdown /s /t 5")
elif "exit" in query or "quit" in query or "stop" in query:
speak("Goodbye! Have a great day.")
exit()
else:
speak("Sorry, I didnβt understand that command.")
π§ 5. Main Loop
if __name__ == "__main__":
greet_user()
while True:
command = listen()
if command:
perform_task(command)
π§ 6. How It Works
| Step | Action |
|---|---|
| π€ Listen | Captures your voice using speech_recognition |
| π§ Recognize | Converts speech β text via Google API |
| βοΈ Process | Matches text with available commands |
| π Speak | Responds using pyttsx3 speech engine |
| π» Execute | Opens browser, plays music, etc. |
π‘ 7. Add More Commands
You can extend it easily:
elif "open notepad" in query:
speak("Opening Notepad...")
os.system("notepad")
elif "send email" in query:
speak("Feature not yet implemented, but we can add SMTP support later.")
π 8. Enhancement Ideas
| Feature | Description |
|---|---|
| π§© Custom Wake Word | Activate only when user says βHey Pythonβ |
| π¬ ChatGPT Integration | Connect to GPT API for intelligent replies |
| π Smart Home | Control IoT devices using HTTP/MQTT APIs |
| π§Ύ Reminders | Add daily reminder or notes feature |
| π€οΈ Weather Reports | Integrate OpenWeather API |
| π΅ Spotify Control | Use spotipy library to control Spotify |
| π± Mobile Integration | Use Flask API to connect with your phone |
β Summary
| Feature | Description |
|---|---|
| π€ Voice Recognition | Converts speech to text |
| π Text-to-Speech | Speaks responses aloud |
| π Smart Actions | Opens sites, apps, searches online |
| βοΈ Customizable | Easy to add your own commands |
| π‘ Scalable | Can become your personal AI assistant |

Leave a Reply