Home Python Project Shutdown Computer with Voice Using Python

Shutdown Computer with Voice Using Python

by anupmaurya
39 minutes read

In this article, you’ll learn 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.

Required Modules

1. PyAudio

PyAudio provides Python bindings for PortAudio v19, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple macOS. To install pyaudio, go to terminal and type,

pip install pyaudio

2. SpeechRecognition

It is an external module in python whose functionality depends on the voice commands of the user. To install SpeechRecognition, go to terminal and type,

pip install speechrecognition

3. Pyttsx3 

It is a text-to-speech conversion library in Python. To install Pyttsx3, go to terminal and type,

pip install pyttsx3

Code with comments

# 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
        command.Speak("Shutting the computer")
        os.system("shutdown /s /t 30")
        break
    Speak("Say that again sir")

Explanation of Code For Shutdown Computer with Voice Using Python

Let’s break down the code step-by-step:

1. Importing Modules:

  • pyttsx3: This module allows the program to use the Text-to-Speech functionality of your computer, making the assistant speak.
  • speech_recognition: This module enables the program to recognize speech from your microphone.
  • os: This module provides functions to interact with the operating system, allowing the program to shut down the computer.

2. Functions:

  • take_commands():
    • Initializes a Recognizer object from the speech_recognition library.
    • Opens your computer’s microphone.
    • Listens for audio input and stores it in the audio variable.
    • Tries to recognize the speech using Google Speech-to-Text API and stores the result in the Query variable.
    • If there’s an error, it prints the error and prompts you to speak again, returning “None”.
    • Otherwise, it returns the recognized text (query).
  • Speak(audio):
    • Initializes a pyttsx3 engine.
    • Sets the engine to speak the provided audio text.
    • Uses engine.runAndWait() to speak the text and wait for it to finish before continuing.

3. Main Script:

  • The script starts by using the Speak function to ask you if you want to shut down your computer.
  • It enters a loop (while True) that continues until you give a valid response.
  • Inside the loop:
    • It calls take_commands() to listen for your response.
    • If “no” is found in your response, it thanks you and exits the loop.
    • If “yes” is found, it speaks a confirmation message and shuts down the computer using os.system("shutdown /s /t 30"). This command initiates a system shutdown in 30 seconds (/t 30).
    • If neither “no” nor “yes” is found, it asks you to speak again.

Overall, this code demonstrates a basic voice assistant that can understand simple yes/no responses for shutting down your computer.

More Python Projects

You may also like

1 comment

Kashish April 25, 2021 - 11:25 am

Hey ,Really fun to make this projects. thanks Techarge of this!

Comments are closed.

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.