Shutdown Computer with Voice in Python

assignment advertisement

Today we are going to make a python program to Shutdown Computer with Voice commands.

This assistant can talk to you or communicate with you using your voice and listens to your voice.


There are three requirements–

1. PyAudio — to install pyaudio, go to terminal and type,

pip install pyaudio

2. SpeechRecognition — to install SpeechRecognition, go to terminal and type,

pip install speechrecognition

3. Pyttsx3 — to install Pyttsx3, go to terminal and type,

pip install pyttsx3

Code Shutdown Computer with Voice in Python:


# Importing required modules
# importing pyttsx3
import pyttsx3
# importing speech_recognition
import speech_recognition as sr
# importing os module
import os


# creating take_commands() function which
# can take some audio, Recognize and return
# if there are not any errors
def take_commands():
    # initializing speech_recognition
    r = sr.Recognizer()
    # opening physical microphone of computer
    with sr.Microphone() as source:
        print('Listening')
        r.pause_threshold = 0.7
        # storing audio/sound to audio variable
        audio = r.listen(source)
        try:
            print("Recognizing")
            # Recognizing audio using google api
            Query = r.recognize_google(audio)
            print("the query is printed='", Query, "'")
        except Exception as e:
            print(e)
            print("Say that again sir")
            # returning none if there are errors
            return "None"
    # returning audio as text
    import time
    time.sleep(2)
    return Query


# creating Speak() function to giving Speaking power
# to our voice assistant
def Speak(audio):
    # initializing pyttsx3 module
    engine = pyttsx3.init()
    # anything we pass inside engine.say(),
    # will be spoken by our voice assistant
    engine.say(audio)
    engine.runAndWait()

Speak("Do you want to shutdown your computer sir?")
while True:
    command = take_commands()
    if "no" in command:
        Speak("Thank u sir I will not shut down the computer")
        break
    if "yes" in command:
        # Shutting down
        Speak("Shutting the computer")
        os.system("shutdown /s /t 30")
        break
    Speak("Say that again sir")

Explanation:

We have used three modules of Python here, pyaudio is working in the background and is necessary to be installed.

Here, we have made the take_command() function so that it can be used here as the ear of our voice assistant in Python which ultimately uses the speech_recognition module.

Similarly, we have made the Speak() function so that it can be used here as the mouth of our voice assistant in Python which ultimately uses the pyttsx3 module.


Also Read:


Create Language Translator Using Python

Covid-19 Tracker Application Using Python

YouTube Video Downloader Application Using Python

GUI Calendar using Python

GUI Application To See wifi password in Python

Get WiFi Passwords With Python


Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you

22 thoughts on “Shutdown Computer with Voice in Python

  1. while entering the command its shows me 19.0.2 command and i am using 20.0.2 something so where do i get the correct code

  2. pip install pycharm is not working in my system,,,,,,i have tried diff commands also”PyAudio‑0.2.11‑cp38‑cp38‑win32.whl” i have tried this but it was not showing result,,,,the 2 and 3 pip is downloaded successfully

  3. hi, i’m got error when i say “yes”.
    Traceback (most recently call last):
    AttributeError: ‘str’ object has no attribute ‘Speak’.

    Can you help me? I try for copy fullcode from your web. Thank you sir

  4. At this time it looks like Drupal is the top blogging platform out there right now. Anallese Colver Odette

  5. As a Newbie, I am permanently searching online for articles that can benefit me. Thank you Chloe Hernando Joselyn

Comments are closed.