使用语音命令关闭电脑的Python脚本
是的,可以借助终端和Python中的一些模块,通过这些模块,只需使用语音命令就可以关闭 PC
所需模块:
- OS模块:它是Python中的一个内置模块,提供与操作系统交互的函数。
- 语音识别模块:它是Python中的一个外部模块,其功能取决于用户的语音命令。
- Pyttsx3 模块:它是Python中的文本到语音转换库。
安装:
pip install SpeechRecognition
pip install pyttsx3
终端的作用:
在终端中有许多用于关闭命令的标签,但是我们将使用/s标签来关闭系统。
以下是使用语音命令创建关闭 doe=wn PC 的程序的步骤:
步骤1:创建一个类Gfg ,然后创建它的方法,创建takeCommands()方法以将命令作为输入。
Python3
import SpeechRecognition as sr
# Create class
class Gfg:
# Method to take voice 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 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)
print("Say that again sir")
return "None"
return Query
Python3
# Method for voice output
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[1].id)
engine.say(audio)
engine.runAndWait()
Python3
# Method to self shut down system
def quitSelf(self):
self.Speak("do u 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 the computer")
os.system("shutdown /s /t 30")
if choice == 'no':
# Idle
print("Thank u sir")
self.Speak("Thank u sir")
Python3
# Driver code
if __name__ == '__main__':
# Creating gfg object
Maam = Gfg()
# Calling the method to self shut down
Maam.quitSelf()
Python
# Importing required modules
import os
import pyttsx3
import speech_recognition as sr
# Creating class
class Gfg:
# Method to take choice 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 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("Say that again sir")
return "None"
return Query
# Method for voice output
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[1].id)
engine.say(audio)
engine.runAndWait()
# Method to self shut down system
def quitSelf(self):
self.Speak("do u 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 the computer")
os.system("shutdown /s /t 30")
if choice == 'no':
# Idle
print("Thank u sir")
self.Speak("Thank u sir")
# Driver code
if __name__ == '__main__':
Maam = Gfg()
Maam.quitSelf()
第 2 步:创建一个Speak()方法,以便计算机可以与用户进行通信。
蟒蛇3
# Method for voice output
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[1].id)
engine.say(audio)
engine.runAndWait()
第 3 步:现在创建quitSelf()来关闭计算机。
蟒蛇3
# Method to self shut down system
def quitSelf(self):
self.Speak("do u 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 the computer")
os.system("shutdown /s /t 30")
if choice == 'no':
# Idle
print("Thank u sir")
self.Speak("Thank u sir")
第 4 步:现在在驱动程序代码中创建一个Gfg对象并调用quitSelf()方法。
蟒蛇3
# Driver code
if __name__ == '__main__':
# Creating gfg object
Maam = Gfg()
# Calling the method to self shut down
Maam.quitSelf()
以下是使用语音命令关闭 PC 的完整程序:
Python
# Importing required modules
import os
import pyttsx3
import speech_recognition as sr
# Creating class
class Gfg:
# Method to take choice 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 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("Say that again sir")
return "None"
return Query
# Method for voice output
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[1].id)
engine.say(audio)
engine.runAndWait()
# Method to self shut down system
def quitSelf(self):
self.Speak("do u 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 the computer")
os.system("shutdown /s /t 30")
if choice == 'no':
# Idle
print("Thank u sir")
self.Speak("Thank u sir")
# Driver code
if __name__ == '__main__':
Maam = Gfg()
Maam.quitSelf()
输出: