Today we are going to make a python program to Shutdown Computer with Voice commands.
This assistant can talk to you or communicate with you using your voice and listens to your voice.
There are three requirements–
1. PyAudio — to install pyaudio, go to terminal and type,
pip install pyaudio
2. SpeechRecognition — to install SpeechRecognition, go to terminal and type,
pip install speechrecognition
3. Pyttsx3 — to install Pyttsx3, go to terminal and type,
pip install pyttsx3
Code Shutdown Computer with Voice in Python:
# Importing required modules # importing pyttsx3 import pyttsx3 # importing speech_recognition import speech_recognition as sr # importing os module import os # creating take_commands() function which # can take some audio, Recognize and return # if there are not any errors def take_commands(): # initializing speech_recognition r = sr.Recognizer() # opening physical microphone of computer with sr.Microphone() as source: print('Listening') r.pause_threshold = 0.7 # storing audio/sound to audio variable audio = r.listen(source) try: print("Recognizing") # Recognizing audio using google api Query = r.recognize_google(audio) print("the query is printed='", Query, "'") except Exception as e: print(e) print("Say that again sir") # returning none if there are errors return "None" # returning audio as text import time time.sleep(2) return Query # creating Speak() function to giving Speaking power # to our voice assistant def Speak(audio): # initializing pyttsx3 module engine = pyttsx3.init() # anything we pass inside engine.say(), # will be spoken by our voice assistant engine.say(audio) engine.runAndWait() Speak("Do you want to shutdown your computer sir?") while True: command = take_commands() if "no" in command: Speak("Thank u sir I will not shut down the computer") break if "yes" in command: # Shutting down Speak("Shutting the computer") os.system("shutdown /s /t 30") break Speak("Say that again sir")
Explanation:
We have used three modules of Python here, pyaudio is working in the background and is necessary to be installed.
Here, we have made the take_command() function so that it can be used here as the ear of our voice assistant in Python which ultimately uses the speech_recognition module.
Similarly, we have made the Speak() function so that it can be used here as the mouth of our voice assistant in Python which ultimately uses the pyttsx3 module.
Also Read:
Create Language Translator Using Python
Covid-19 Tracker Application Using Python
YouTube Video Downloader Application Using Python
GUI Application To See wifi password in Python
Get WiFi Passwords With Python