Skip to content
Related Articles

Related Articles

Improve Article
Python Script to Shutdown your PC using Voice Commands
  • Difficulty Level : Medium
  • Last Updated : 01 Oct, 2020

Yes, it is possible with the help of terminal and some modules in Python through which one can shut down a PC by just using voice commands

Required Modules:

  • OS module: It is an in-built module in python that provides function for interacting with the operating system.
  • Speech Recognition module: It is an external module in python whose functionality depends on the voice commands of the user.
  • Pyttsx3 module: it is a text-to-speech conversion library in Python.

Installation:

pip install SpeechRecognition
pip install pyttsx3

Role of Terminal:

In terminal there are many tags for the shutdown command, however we will use the /s tag with it to shut down the system.

Below are the steps to create a program to shut doe=wn PC using Voice Commands:

Step 1: Create a class Gfg and then create its methods, create takeCommands() method to take commands as input.

Python3






import SpeechRecognition as sr
  
# Create class
class Gfg:
      
    # Method to take voice commands as input
    def takeCommands(self):
          
        # Using Recognizer and Microphone Method for input voice commands
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print('Listening')
              
            # Number pf seconds of non-speaking audio before
            # a phrase is considered complete
            r.pause_threshold = 0.7 
            audio = r.listen(source)
              
            # Voice input is identified
            try:
                  
                # Listening voice commands in indian english
                print("Recognizing")
                Query = r.recognize_google(audio, language='en-in')
                  
                # Displaying the voice command
                print("the query is printed='", Query, "'")
                  
            except Exception as e:
                  
                # Displaying exception
                print(e)  
                print("Say that again sir")
                return "None"
        return Query

Step 2: Create a Speak() method so that the computer can communicate with the user.

Python3




# Method for voice output
def Speak(self, audio):
    
    # Constructor call for pyttsx3.init()
    engine = pyttsx3.init('sapi5')
      
    # Setting voice type and id
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)
    engine.say(audio)
    engine.runAndWait()

Step 3: Now create the quitSelf() to shut down the computer. 

Python3




# Method to self shut down system
def quitSelf(self):
    self.Speak("do u want to switch off the computer sir")
      
    # Input voice command 
    take = self.takeCommand()
    choice = take
    if choice == 'yes':
          
        # Shutting down
        print("Shutting down the computer")
        self.Speak("Shutting the computer")
        os.system("shutdown /s /t 30")
    if choice == 'no':
          
        # Idle
        print("Thank u sir")
        self.Speak("Thank u sir")

Step 4: Now in the driver code create a Gfg object and call the quitSelf() method.

Python3




# Driver code
if __name__ == '__main__':
      
    # Creating gfg object
    Maam = Gfg()
      
    # Calling the methid to self shut down
    Maam.quitSelf()

Below is the complete program to shut down a PC using voice commands:

Python




# Importing required modules
import os
import pyttsx3
import speech_recognition as sr
  
  
  
# Creating class
class Gfg:
      
    # Method to take coice commands as input
    def takeCommands(self):
          
          # Using Recognizer and Microphone Method for input voice commands
          r = sr.Recognizer()
          with sr.Microphone() as source:
              print('Listening')
  
              # Number pf seconds of non-speaking audio before
              # a phrase is considered complete
              r.pause_threshold = 0.7 
              audio = r.listen(source)
  
              # Voice input is identified
              try:
  
                  # Listening voice commands in indian english
                  print("Recognizing")
                  Query = r.recognize_google(audio, language='en-in')
  
                  # Displaying the voice command
                  print("the query is printed='", Query, "'")
  
              except Exception as e:
  
                  # Displaying exception
                  print(e)  
                  # Handling exception
                  print("Say that again sir")
                  return "None"
          return Query
  
      
    # Method for voice output
    def Speak(self, audio):
    
          # Constructor call for pyttsx3.init()
          engine = pyttsx3.init('sapi5')
  
          # Setting voice type and id
          voices = engine.getProperty('voices')
          engine.setProperty('voice', voices[1].id)
          engine.say(audio)
          engine.runAndWait()
  
      
    # Method to self shut down system
    def quitSelf(self):
        self.Speak("do u want to switch off the computer sir")
  
        # Input voice command 
        take = self.takeCommand()
        choice = take
        if choice == 'yes':
  
            # Shutting down
            print("Shutting down the computer")
            self.Speak("Shutting the computer")
            os.system("shutdown /s /t 30")
        if choice == 'no':
  
            # Idle
            print("Thank u sir")
            self.Speak("Thank u sir")
  
              
  
# Driver code            
if __name__ == '__main__':
    Maam = Gfg()
    Maam.quitSelf()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :