Python Script to Shutdown your PC using Voice Commands

08 Oct, 2021
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
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.
pip install SpeechRecognition
pip install pyttsx3
In terminal there are many tags for the shutdown command, however we will use the /s tag with it to shut down the system.
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 classclass 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 outputdef 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 systemdef 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 codeif __name__ == '__main__':         # Creating gfg object    Maam = Gfg()         # Calling the method to self shut down    Maam.quitSelf()
Python
# Importing required modulesimport osimport pyttsx3import speech_recognition as sr   # Creating classclass Gfg:         # Method to take choice 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:
kalrap615
prachisoda1234
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function