来源:CSDN
发布时间:Nov 17, 2020, 7:38:44 AM
原地址:https://blog.csdn.net/liuyuehui110/article/details/109735786
1,实现语音操控的原理
语音操控分为语音识别和语音朗读两部分
我们使用speech模块实现语音模块(python 3.8.6)
用pip进行安装
2,启动语音识别,进行相关设置
此处仅为启动和关闭语音系统
import speech
while True:
phrase =speech.input()
speech.say("You said %s"%phrase)
if phrase =="turn off":
break
3,用speech进行语音识别
import os
import sys
import speech
import webbrowser
phrase = {"closeMainSystem" : "关闭人机交互"
, "film" : "我要看电影"
, "listenMusic" : "我好累啊"
, "blog" : "看博客"
, "cmd" : "cmd" }
def callback(phr, phrase):
if phr == phrase["closeMainSystem"]:
speech.say("Goodbye. 人机交互即将关闭,谢谢使用")
speech.stoplistening()
sys.exit()
elif phr == phrase["film"]:
speech.say("正在为您打开优酷")
webbrowser.open_new("http://www.youku.com/")
elif phr == phrase["listenMusic"]:
speech.say("即将为你启动豆瓣电台")
webbrowser.open_new("http://douban.fm/")
elif phr == phrase["blog"]:
speech.say("即将进入Dreamforce.me")
webbrowser.open_new("http://www.cnblogs.com/darksouls/")
elif phr == phrase["cmd"]:
speech.say("即将打开CMD")
os.popen("C:\Windows\System32\cmd.exe")
# 可以继续用 elif 写对应的自制中文库中的对应操作
while True:
phr = speech.input()
speech.say("You said %s" % phr)
callback(phr, phrase)