Everyone's been awed by Tony Stark's JARVIS, the AI assistant that's every techie's dream. But what if I told you that you can create your own voice-activated AI assistant inspired by JARVIS using Python? Let's embark on this coding journey together!
Getting Started:
Before diving into the coding part, it's essential to set up the required Python libraries:
pyttsx3: Enables text-to-speech conversion.
speech_recognition: Powers the voice recognition feature.
os: Allows interfacing with the operating system.
webbrowser: Helps in launching and controlling the web browser.
pyautogui: Simulates keyboard and mouse actions.
Simply run "pip install pyttsx3 speechrecognition pyautogui to get started."
Building Blocks of Our JARVIS:
Text-to-Speech Configuration:
Setting up the TTS engine ensures that our JARVIS has a voice. We initialize it and set its properties using the pyttsx3 library.
Voice Commands:
Using the speech_recognition library, we can capture voice input. This becomes the core of our assistant, allowing us to interact with JARVIS seamlessly.
Implementing Functionality:
JARVIS isn't just about understanding voice commands. It's about acting on them! We'll set up commands to:
Search the web
Open applications like Chrome, Notepad, and WhatsApp
Type in Notepad using voice
import pyttsx3
import speech_recognition as sr
import os
import webbrowser
import pyautogui
from datetime import datetime
# Initialize the TTS engine
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
def speak(audio):
"""Convert text to speech."""
engine.say(audio)
engine.runAndWait()
def greet():
"""Greet the user based on the time of day."""
hour = int(datetime.now().hour)
if 0 <= hour < 12:
speak("Good Morning, Boss!")
elif 12 <= hour < 18:
speak("Good Afternoon, Boss!")
else:
speak("Good Evening, Boss!")
speak("Jarvis here. Ready to assist you.")
def commands():
"""Capture voice input from the microphone and convert it to text."""
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
print("Recognizing....!")
query = r.recognize_google(audio, language='en-in')
print(f"You said: {query}\n")
return query
except Exception as e:
return "none"
def main():
greet()
active = False # Variable to check if Jarvis is actively listening for commands
while True:
if active:
query = commands().lower()
# Logic for executing tasks
if 'search' in query:
search_query = query.split("search", 1)[1]
url = f"https://www.google.com/search?q={search_query.strip()}"
webbrowser.open(url)
speak(f"Searching for {search_query} on Google...")
elif 'sleep jarvis' in query:
speak("Goodbye, Boss!")
break
elif 'open' in query:
software_name = query.split(' ', 1)[1]
software_paths = {
'notepad': 'C:\\Windows\\System32\\notepad.exe',
'chrome': 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
'whatsapp': 'C:\\Path\\To\\WhatsApp.exe' # Adjust this to the path of your WhatsApp executable
}
if software_name in software_paths:
os.startfile(software_paths[software_name])
speak(f"Opening {software_name} for you, Boss.")
else:
speak(f"Sorry, I don't have the information to open {software_name}.")
# Check for typing command
elif 'type this in notepad' in query:
speak("Sure, Boss. I'm listening. Please start speaking, and I'll type it in Notepad. Say 'stop jarvis' when done.")
while True:
text_to_type = commands().lower()
if 'stop jarvis' in text_to_type:
speak("Stopped typing.")
break
pyautogui.write(text_to_type) # Type the recognized speech in Notepad
pyautogui.press('enter') # Simulate pressing the "Enter" key
elif 'jarvis' in query:
speak("Yes, Boss?")
else:
query = commands().lower()
if 'jarvis wake up' in query or 'jarvis' in query:
active = True
speak("Yes, Boss?")
Seeing JARVIS in Action:
After coding, it's time to watch the magic unfold. Running the Python script awakens JARVIS, ready to greet you based on the time of day and await your voice commands.
Conclusion & Future Explorations:
Creating a JARVIS-inspired AI assistant is just the beginning. Python offers limitless possibilities. Imagine integrating Machine Learning to make JARVIS predict your needs or expanding its capabilities to control smart home devices.
For a detailed step-by-step guide and a complete code walkthrough, check out the accompanying video tutorial
The sky's the limit, so let your imagination run wild and make JARVIS truly your own!
0 Comments