DEV Community

parmarjatin4911@gmail.com
parmarjatin4911@gmail.com

Posted on

SPEECH RECOGNIZE Voice use PC shutdown.

To create a script that uses speech recognition to shut down or start a Linux PC, you'll need to follow these steps:

Install required packages: You'll need to install the necessary packages for speech recognition and executing system commands.

$pip install SpeechRecognition , os

CODE:

import speech_recognition as sr
import os

Initialize recognizer

recognizer = sr.Recognizer()

def recognize_speech():
with sr.Microphone() as source:
print("Listening for commands...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)

try:
    command = recognizer.recognize_google(audio).lower()
    print("You said:", command)
    return command
except sr.UnknownValueError:
    print("Sorry, I couldn't understand your command.")
    return ""
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))
    return ""
Enter fullscreen mode Exit fullscreen mode

def main():
while True:
command = recognize_speech()
if "shutdown" in command:
os.system("sudo shutdown now")
elif "start" in command or "boot" in command:
os.system("sudo reboot")
elif "exit" in command or "quit" in command:
print("Exiting...")
break
else:
print("Unknown command. Please try again.")

if name == "main":
main()

Make sure you have SpeechRecognition installed (pip install SpeechRecognition). This script listens for voice commands, recognizes them using Google's Speech Recognition service, and shuts down or reboots the system based on the recognized command.

Note:

Make sure to run this script with appropriate permissions, especially if you're executing commands like shutdown or reboot (sudo).
Always be cautious with automated system commands, especially those that can shut down or reboot your system. Ensure proper safeguards are in place.

Top comments (0)