Python Script to Shutdown your PC using Voice Commands

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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


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

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


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.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.