Voice Recognition or simply referred to as speech recognition is quite fascinating and fun at the same time. Yes, it is possible with the help of terminal and some modules like OS module, Speech Recognition module, and Pyttsx3 module in Python through which one can shut down a PC by just using voice commands we’ll see it in brief in this mini python project.
Required Python Modules:
- OS module: It is an in-built module in python that acts as a bridge between a python program and the operating system.
- Speech Recognition module: It is an external module in python which helps to perform different functions depending totally on the user’s voice commands.
- Pyttsx3 module: It’s a library in Python that helps to convert text-to-speech and vice versa.
If you don’t have the required modules then you can download them using pip which is a package management system written in python. It connects to an online repository of public packages, called the Python Package Index. pip can also be configured to connect to other package repositories, provided that they comply to Python Enhancement Proposal 503.
pip install SpeechRecognition pip install pyttsx3
Below is the source code to create a program to shutdown PC using Voice Commands using Python:
# Importing required modules import os import pyttsx3 import speech_recognition as sr # Creating class with name as codersbuddy class codersbuddy: # create takeCommands() method to take 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 en-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("Can you say that again sir?") return "None" return Query # Create a Speak() method so that the computer can communicate with the user 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.id) engine.say(audio) engine.runAndWait() # Method to self shut down system def quitSelf(self): self.Speak("Do you 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 down the computer") os.system("shutdown /s /t 30") if choice == 'no': # Idle print("Thank you sir!") self.Speak("Thank You sir!") # Driver code # Now in the driver code create a codersbuddy object (codersbuddy is our class name) and call the quitSelf() method if __name__ == '__main__': Maam = codersbuddy() Maam.quitSelf()
Recommended courses for learning Python
- 100 Days of Code – The Complete Python Pro Bootcamp for 2021 by Angela Yu
- Complete Python Bootcamp: Go Beginner To Expert In Python 3
Don’t forget to see our OpenCV tutorials!