Restart your Computer with Speech Recognition
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.
Modules Required
- 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.
Why terminal is used?
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-by-step Approach:
Step 1: We have to make Speak function for making the computer communicate with us.
Python3
def Speak( self , audio):
engine = pyttsx3.init( 'sapi5' )
voices = engine.getProperty( 'voices' )
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
def take_commands( self ):
r = sr.Recognizer()
with sr.Microphone() as source:
print ( 'Listening' )
r.pause_threshold = 0.7
audio = r.listen(source)
try :
print ( "Recognizing" )
Query = r.recognize_google(audio, language = 'en-in' )
print ( "the query is printed='" , Query, "'" )
except Exception as e:
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
def 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
if __name__ = = '__main__' :
Maam = Main()
Maam.restart()
|
Below is the complete Program of the above approach:
Python3
import pyttsx3
import time
import speech_recognition as sr
import os
class Main:
def Speak( self , audio):
engine = pyttsx3.init( 'sapi5' )
voices = engine.getProperty( 'voices' )
engine.setProperty( 'voice' , voices[ 1 ]. id )
engine.say(audio)
engine.runAndWait()
def takeCommand( self ):
r = sr.Recognizer()
with sr.Microphone() as source:
print ( 'Listening' )
r.pause_threshold = 0.7
audio = r.listen(source)
try :
print ( "Recognizing" )
Query = r.recognize_google(audio, language = 'en-in' )
print ( "the query is printed='" , Query, "'" )
except Exception as e:
print (e)
print ( "Say that again sir" )
return "None"
return Query
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" )
if __name__ = = '__main__' :
Maam = Main()
Maam.restart()
|
Last Updated :
08 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...