如何在Python制作语音解锁系统?
我们计算机的基于语音的解锁系统将是一种应用程序,它将我们的语音作为输入并处理语音将其转换为我们计算机的基于文本的指令,然后根据该指令执行操作。这个过程在语音到文本中使用了最先进的过程,在文本到语音中使用了自然语言理解和深度学习。
在我们的应用程序中构建基于语音的应用程序的第一步是不断聆听用户的声音,然后将该声音转录为基于文本的指令。
这很难创建具有更高准确性的语音到文本转录引擎,因为我们需要为此训练我们的数据模型,而且还有许多行业领导者可用,因为谷歌、微软、苹果和其他一些公司正在提供基于 API 的可以轻松与应用程序集成的服务。 Google 还提供语音操作,这是一项基于 API 的服务,可使用语音无缝地在应用程序内执行操作。
如果您有兴趣开发自己的语音转文本应用程序,请仔细阅读。现在要构建您自己的基于Python的应用程序,请按照以下步骤操作:
1. 导入库
Python3
import sys
import ctypes
import speech_recognition as speech
Python3
voice = speech.Recognizer()
with speech.Microphone() as source:
print("Say something!")
voice_command = voice.listen(source)
Python3
try:
command=voice.recognize_google(voice_command)
# handle the exceptions
except speech.UnknownValueError:
print("Google Speech Recognition system could not understand your \
instructions please give instructions carefully")
except speech.RequestError as e:
print("Could not request results from Google Speech Recognition\
service; {0}".format(e))
Python3
if command == "lock my PC":
ctypes.windll.user32.LockWorkStation()
elif command == "stop":
sys.exit(0)
else:
print("Not a command.")
fxn()
Python3
# importing packages
import sys
import ctypes
import speech_recognition as speech
# define function
def fxn():
# voice reconizer object
voice = speech.Recognizer()
# use microphone
with speech.Microphone() as source:
print("Say something!")
voice_command = voice.listen(source)
# check input
try:
command = voice.recognize_google(voice_command)
print(command)
# handle the exceptions
except speech.UnknownValueError:
print("Google Speech Recognition system could not\
understand your instructions please give instructions carefully")
except speech.RequestError as e:
print(
"Could not request results from Google Speech Recognition \
service; {0}".format(e))
# validate input
if command == "lock my PC":
ctypes.windll.user32.LockWorkStation()
elif command == "stop":
sys.exit(0)
else:
print("Not a command.")
fxn()
# execute
fxn()
- 库 ctypes 是Python的外部函数库。它提供 C 兼容的数据类型并允许调用 DLL 或共享库中的函数。它可用于将这些库包装在纯Python。
- 语音识别是家庭自动化、人工智能等多种应用中的重要功能。
2. 麦克风音频
现在使用Recognizer()函数从麦克风获取音频 python的speech_recognition模块。这样,我们就从系统中捕获了语音命令。
蟒蛇3
voice = speech.Recognizer()
with speech.Microphone() as source:
print("Say something!")
voice_command = voice.listen(source)
3.检查命令
现在是使用谷歌语音识别引擎的时候了,注意这一点,而不是使用这个 API 你可以使用你自己的语音识别系统。并且您需要通过代码处理来自 Google 引擎的错误的一些异常。
蟒蛇3
try:
command=voice.recognize_google(voice_command)
# handle the exceptions
except speech.UnknownValueError:
print("Google Speech Recognition system could not understand your \
instructions please give instructions carefully")
except speech.RequestError as e:
print("Could not request results from Google Speech Recognition\
service; {0}".format(e))
4. 验证命令
现在是匹配和比较语音命令模式的时候了。并加载并执行锁定PC的命令。 Windows 的默认库(user32.dll) 中提供了锁定PC 的函数。您也可以使用 Linux,但此操作的命令会有所不同,默认库也将在 (cell.user32) 上可用。
蟒蛇3
if command == "lock my PC":
ctypes.windll.user32.LockWorkStation()
elif command == "stop":
sys.exit(0)
else:
print("Not a command.")
fxn()
下面是实现。
在这里,我们将形成一个函数(方法),该函数(方法)会反复调用自己,直到需要的命令。
- 如果命令是锁定 pc,则它锁定 pc。
- 如果命令是停止程序,则它停止程序。
- 否则,它说不是编码命令并再次运行。
蟒蛇3
# importing packages
import sys
import ctypes
import speech_recognition as speech
# define function
def fxn():
# voice reconizer object
voice = speech.Recognizer()
# use microphone
with speech.Microphone() as source:
print("Say something!")
voice_command = voice.listen(source)
# check input
try:
command = voice.recognize_google(voice_command)
print(command)
# handle the exceptions
except speech.UnknownValueError:
print("Google Speech Recognition system could not\
understand your instructions please give instructions carefully")
except speech.RequestError as e:
print(
"Could not request results from Google Speech Recognition \
service; {0}".format(e))
# validate input
if command == "lock my PC":
ctypes.windll.user32.LockWorkStation()
elif command == "stop":
sys.exit(0)
else:
print("Not a command.")
fxn()
# execute
fxn()
输出: