Restart your Computer with Speech Recognition

08 Jun, 2021
We can do this with the help of Python. Python has many libraries that can help many things to be done were easy. We need the help of the terminal for doing this task. Python’s one of the best library Speech Recognition will help us to do this so.
Pyttsx3: This is a text to speech Library in Python . We have to use pip for it.
pip install pyttsx3
Speech Recognition module: It is module that will help us to recognize the Speech and the commands given to the computer. We have to use pip to install Speech Recognition.
pip install SpeechRecognition
OS module: It is module in python that provides function for interacting with the operating system . OS module comes with the Python packages. Pip is not required.
When we type shutdown in the terminal we will get many options with the help of which we can restart our computer. As we will have many tags in it. We can use “/r” with shutdown to restart.
Terminal with shutdown command . We can see that there is /r for completely shutdown and restart. 
Step 1: We have to make Speak function for making the computer communicate with us.
Python3
# Method to give # output voice commands def Speak(self, audio):    engine = pyttsx3.init('sapi5')         # initial constructor of pyttsx3    voices = engine.getProperty('voices')         # getter and setter method    engine.setProperty('voice', voices[1].id)    engine.say(audio)    engine.runAndWait()
Step 2: We have to now make a function for taking our commands.
Python3
# Method to take# input voice commandsdef take_commands(self):      r = sr.Recognizer()         # Making the use of Recognizer    # and Microphone method from    # Speech Recognition for taking commands    with sr.Microphone() as source:          print('Listening')        r.pause_threshold = 0.7        # seconds of non-speaking audio        # before a phrase is considered complete        audio = r.listen(source)        try:            print("Recognizing")            Query = r.recognize_google(audio, language='en-in')                         # For listening the command in indian english            print("the query is printed='", Query, "'")                         # For printing the query or the            # command that we give        except Exception as e:                         # This is for printing the exception            print(e)            print("Say that again sir")            return "None"    return Query
Step 3: Now we will make a Restart method for making the computer restart.
Python3
# Method to restart PCdef restart(self):    self.Speak("do u want to restart the computer sir")    take = self.takeCommand()    choice = take    if choice == 'yes':        print("Restarting  the computer")        self.Speak("Restarting the computer")        os.system("shutdown /r /t 30")    if choice == 'no':        print("Thank u sir")        self.Speak("Thank u sir")
Step 4: Now we will have the main method.
Python3
# Driver Codeif __name__ == '__main__':    Maam = Main()    Maam.restart()
Below is the complete Program of the above approach:
Python3
# Import required modulesimport pyttsx3import timeimport speech_recognition as srimport os   # Creating classclass Main:         # Method to give output commands    def Speak(self, audio):        engine = pyttsx3.init('sapi5')        voices = engine.getProperty('voices')        engine.setProperty('voice', voices[1].id)        engine.say(audio)        engine.runAndWait()         # Method to take input voice commands    def takeCommand(self):         # This method is for taking        # the commands and recognizing the command         r = sr.Recognizer()        # from the speech_Recognition module        # we will use the recongizer method        # for recognizing         with sr.Microphone() as source:            # from the speech_Recognition module            # we will use the Microphone module for            # listening the command             print('Listening')            # seconds of non-speaking audio            # before a phrase is considered complete            r.pause_threshold = 0.7            audio = r.listen(source)             try:                print("Recognizing")                Query = r.recognize_google(audio, language='en-in')                 # for listening the command                # in indian english                print("the query is printed='", Query, "'")                 # for printing the query or the                # command that we give            except Exception as e:                 # this method is for handling                # the exception and so that                # assistant can ask for telling                # again the command                print(e)                                 print("Say that again sir")                return "None"            return Query     # Method to restart PC    def restart(self):        self.Speak("do u want to switch off the computer sir")        take = self.takeCommand()        choice = take        if choice == 'yes':            print("Shutting down the computer")            os.system("shutdown /s /t 30")            self.Speak("Shutting the computer")        if choice == 'no':            print("Thank u sir")            self.Speak("Thank u sir")   # Driver Codeif __name__ == '__main__':    Maam = Main()    Maam.restart()
sweetyty
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.