In this article, you’ll learn 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.
PyAudio provides Python bindings for PortAudio v19, the cross-platform audio I/O library. With PyAudio, you can easily use Python to play and record audio on a variety of platforms, such as GNU/Linux, Microsoft Windows, and Apple macOS. To install pyaudio, go to terminal and type,
pip install pyaudio
It is an external module in python whose functionality depends on the voice commands of the user. To install SpeechRecognition, go to terminal and type,
pip install speechrecognition
It is a text-to-speech conversion library in Python. To install Pyttsx3, go to terminal and type,
pip install pyttsx3
Code with comments
# 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 command.Speak("Shutting the computer") os.system("shutdown /s /t 30") break Speak("Say that again sir")
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.
More Python Projects
- PYTHON ROCK PAPER SCISSORS GAME
- CURRENCY CONVERTER IN PYTHON
- COVID-19 TRACKER APPLICATION USING PYTHON
Hey ,Really fun to make this projects. thanks Techarge of this!
Comments are closed.