{"id":169,"date":"2025-10-28T02:27:52","date_gmt":"2025-10-28T02:27:52","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=169"},"modified":"2025-12-17T07:45:37","modified_gmt":"2025-12-17T07:45:37","slug":"27-real-world-python-projects-voice-assistant","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=169","title":{"rendered":"27 &#8211; Real-World Python Projects &#8211; Voice Assistant"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\ud83c\udfaf <strong>Project<\/strong> <strong>Objective<\/strong><\/h2>\n\n\n\n<p>To create a Python-based <strong>voice-controlled assistant<\/strong> that listens to commands, performs actions (like searching the web or opening apps), and speaks responses aloud.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 1. <strong>What You\u2019ll Learn<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83c\udfa4 Capturing voice input using <code>speech_recognition<\/code><\/li>\n\n\n\n<li>\ud83d\udd0a Making Python speak with <code>pyttsx3<\/code><\/li>\n\n\n\n<li>\ud83c\udf10 Performing tasks like opening YouTube, searching Google, checking time, etc.<\/li>\n\n\n\n<li>\u2699\ufe0f Handling multiple commands with simple AI logic<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f 2. <strong>Required Libraries<\/strong><\/h2>\n\n\n\n<p>We\u2019ll auto-install them within the code \ud83d\udc47<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Auto install required modules\nimport os\nimport sys\nimport subprocess\n\ndef install(pkg):\n    subprocess.check_call(&#91;sys.executable, \"-m\", \"pip\", \"install\", pkg])\n\nfor pkg in &#91;\"speechrecognition\", \"pyttsx3\", \"pyaudio\"]:\n    try:\n        __import__(pkg)\n    except ImportError:\n        install(pkg)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf99\ufe0f 3. <strong>Main Voice Assistant Code<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import speech_recognition as sr\nimport pyttsx3\nimport datetime\nimport webbrowser\nimport os\nimport subprocess\n\n# Initialize the text-to-speech engine\nengine = pyttsx3.init()\nengine.setProperty('rate', 175)  # Speaking speed\nengine.setProperty('volume', 1.0)\n\ndef speak(text):\n    print(f\"Assistant \ud83c\udfa7: {text}\")\n    engine.say(text)\n    engine.runAndWait()\n\ndef listen():\n    r = sr.Recognizer()\n    with sr.Microphone() as source:\n        print(\"\ud83c\udfa4 Listening...\")\n        r.pause_threshold = 1\n        audio = r.listen(source)\n\n    try:\n        print(\"\ud83e\udde0 Recognizing...\")\n        query = r.recognize_google(audio, language='en-in')\n        print(f\"You said: {query}\\n\")\n    except Exception:\n        speak(\"Sorry, I didn't catch that.\")\n        return \"\"\n    return query.lower()\n\ndef greet_user():\n    hour = int(datetime.datetime.now().hour)\n    if hour &lt; 12:\n        speak(\"Good morning! I'm your Python Assistant.\")\n    elif hour &lt; 18:\n        speak(\"Good afternoon!\")\n    else:\n        speak(\"Good evening!\")\n    speak(\"How can I help you today?\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 4. <strong>Performing Real-World Actions<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>def perform_task(query):\n    if \"time\" in query:\n        time = datetime.datetime.now().strftime(\"%I:%M %p\")\n        speak(f\"The time is {time}\")\n\n    elif \"date\" in query:\n        date = datetime.datetime.now().strftime(\"%A, %d %B %Y\")\n        speak(f\"Today is {date}\")\n\n    elif \"open youtube\" in query:\n        speak(\"Opening YouTube...\")\n        webbrowser.open(\"https:\/\/www.youtube.com\")\n\n    elif \"open google\" in query:\n        speak(\"Opening Google...\")\n        webbrowser.open(\"https:\/\/www.google.com\")\n\n    elif \"search\" in query:\n        speak(\"What should I search for?\")\n        term = listen()\n        if term:\n            url = f\"https:\/\/www.google.com\/search?q={term}\"\n            speak(f\"Searching for {term}\")\n            webbrowser.open(url)\n\n    elif \"play music\" in query:\n        music_dir = \"C:\\\\Users\\\\Public\\\\Music\"  # change path if needed\n        songs = os.listdir(music_dir)\n        if songs:\n            speak(\"Playing music...\")\n            os.startfile(os.path.join(music_dir, songs&#91;0]))\n        else:\n            speak(\"No music files found.\")\n\n    elif \"shutdown\" in query:\n        speak(\"Shutting down the system. Goodbye!\")\n        os.system(\"shutdown \/s \/t 5\")\n\n    elif \"exit\" in query or \"quit\" in query or \"stop\" in query:\n        speak(\"Goodbye! Have a great day.\")\n        exit()\n\n    else:\n        speak(\"Sorry, I didn\u2019t understand that command.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udded 5. <strong>Main Loop<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>if __name__ == \"__main__\":\n    greet_user()\n    while True:\n        command = listen()\n        if command:\n            perform_task(command)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 6. <strong>How It Works<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>Action<\/th><\/tr><\/thead><tbody><tr><td>\ud83c\udfa4 Listen<\/td><td>Captures your voice using <code>speech_recognition<\/code><\/td><\/tr><tr><td>\ud83e\udde0 Recognize<\/td><td>Converts speech \u2192 text via Google API<\/td><\/tr><tr><td>\u2699\ufe0f Process<\/td><td>Matches text with available commands<\/td><\/tr><tr><td>\ud83d\udd0a Speak<\/td><td>Responds using <code>pyttsx3<\/code> speech engine<\/td><\/tr><tr><td>\ud83d\udcbb Execute<\/td><td>Opens browser, plays music, etc.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 7. <strong>Add More Commands<\/strong><\/h2>\n\n\n\n<p>You can extend it easily:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>elif \"open notepad\" in query:\n    speak(\"Opening Notepad...\")\n    os.system(\"notepad\")\n\nelif \"send email\" in query:\n    speak(\"Feature not yet implemented, but we can add SMTP support later.\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd10 8. <strong>Enhancement Ideas<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>\ud83e\udde9 Custom Wake Word<\/td><td>Activate only when user says \u201cHey Python\u201d<\/td><\/tr><tr><td>\ud83d\udcac ChatGPT Integration<\/td><td>Connect to GPT API for intelligent replies<\/td><\/tr><tr><td>\ud83c\udfe0 Smart Home<\/td><td>Control IoT devices using HTTP\/MQTT APIs<\/td><\/tr><tr><td>\ud83e\uddfe Reminders<\/td><td>Add daily reminder or notes feature<\/td><\/tr><tr><td>\ud83c\udf24\ufe0f Weather Reports<\/td><td>Integrate OpenWeather API<\/td><\/tr><tr><td>\ud83c\udfb5 Spotify Control<\/td><td>Use <code>spotipy<\/code> library to control Spotify<\/td><\/tr><tr><td>\ud83d\udcf1 Mobile Integration<\/td><td>Use Flask API to connect with your phone<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 <strong>Summary<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>\ud83c\udfa4 Voice Recognition<\/td><td>Converts speech to text<\/td><\/tr><tr><td>\ud83d\udd0a Text-to-Speech<\/td><td>Speaks responses aloud<\/td><\/tr><tr><td>\ud83c\udf10 Smart Actions<\/td><td>Opens sites, apps, searches online<\/td><\/tr><tr><td>\u2699\ufe0f Customizable<\/td><td>Easy to add your own commands<\/td><\/tr><tr><td>\ud83d\udca1 Scalable<\/td><td>Can become your personal AI assistant<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf 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. \ud83e\udde9 1. What You\u2019ll Learn \u2699\ufe0f 2. Required Libraries We\u2019ll auto-install them within the code \ud83d\udc47 \ud83c\udf99\ufe0f 3. Main Voice Assistant Code \ud83e\udde9 4. Performing Real-World Actions \ud83e\udded 5. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=169"}],"version-history":[{"count":2,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/169\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}