Python入门 —— 06语音识别
2024-09-04 09:35:57
Python 语音
实现语音操控的原理
语音操控分为语音识别和语音朗读两部分
我们使用speech模块实现语音模块(python 2.7)
SAPI是微软Speech API , 是微软公司推出的语音接口,而细心的人会发现从WINXP开始,系统上就已经有语音识别的功能了,可是用武之地相当之少,他并没有给出一些人性化的自定义方案,仅有的语音操控命令显得相当鸡胁。
- Python pywin32,可以使Python调用WIN32COM接口,选择对应版本下载(区分32位/64位),直接双击运行即可
- 安装speech模块:
pip install speech
实现个简易的控制电脑做事情的小程序:
- 首先,来个测试文件
此处仅为启动和关闭语音系统
import speech
while True:
phrase =speech.input()
speech.say("You said %s"%phrase)
if phrase =="turn off":
break
- 自制个中文库
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)
- 完整代码
# _*_ coding:utf-8 _*_
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)
发现网上有个语音识别框架:
# _*_ coding:utf-8 _*_
from win32com.client import constants
import os
import win32com.client
import pythoncom
speaker = win32com.client.Dispatch("SAPI.SPVOICE")
class SpeechRecognition:
def __init__(self, wordsToAdd):
self.speaker = win32com.client.Dispatch("SAPI.SpVoice")
self.listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
self.context = self.listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.wordsRule = self.grammar.Rules.Add("wordsRule", constants.SRATopLevel + constants.SRADynamic, 0)
self.wordsRule.Clear()[self.wordsRule.InitialState.AddWordTransition(None, word) for word in wordsToAdd]
self.grammar.Rules.Commit()
self.grammar.CmdSetRuleState("wordsRule", 1)
self.grammar.Rules.Commit()
self.eventHandler = ContextEvents(self.context)
self.say("Started successfully")
def say(self, phrase):
self.speaker.Speak(phrase)
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
newResult = win32com.client.Dispatch(Result)
print("你在说 ", newResult.PhraseInfo.GetText())
speechstr=newResult.PhraseInfo.GetText()
# 下面即为语音识别信息对应
if speechstr=="张三":
speaker.Speak("lisi")
elif speechstr=="你好":
speaker.Speak("hello world")
elif speechstr=="国庆快乐":
speaker.Speak("Happy nationalday")
elif speechstr=="新年快乐":
speaker.Speak("happy New Year")
elif speechstr=="李四":
speaker.Speak("a beauty baby")
elif speechstr=="王五":
speaker.Speak("a little boy")
elif speechstr=="赵六":
speaker.Speak("a boy can coding")
else:
pass
if __name__ == '__main__':
speaker.Speak("语音识别开启")
wordsToAdd = ["张三",
"你好",
"国庆快乐",
"新年快乐",
"李四",
"王五",
"赵六",]
speechReco = SpeechRecognition(wordsToAdd)
while True:
pythoncom.PumpWaitingMessages()
最新文章
- 关于httpd服务的安装、配置
- 【Android】[转] ANR的分析和问题处理
- java”伪“批量上传
- Clr Via C#读书笔记---线程基础
- 微信支付(APP)集成时碰到的问题(.net提示“无权限”、iOS跳转到微信支付页面中间只有一个“确定”按钮)
- JavaScript API 设计原则
- Java 并发之线程安全
- mysql 中文乱码的解决办法
- Android Activity与Service的交互方式
- jQuery Pagination Ajax分页插件中文详解(摘)
- JNI之HelloWorld
- msbuildtoolspath is not specified for the toolsversion xx
- ROS_Kinetic_20 ROS基础补充
- Dom4j修改xml文档引入
- java中Char到底是什么格式的编码
- IDEA使用SpringBoot 、maven创建微服务的简单过程
- 【九天教您南方cass 9.1】 09 提取坐标的几种方法
- 解決 Android Studio 不停 Indexing 的問題(Updating Indices: Indexing paused due to batch update)
- Java如何显示工作日(周一至周五)的名称?
- destoon 分页
热门文章
- HTTP协议(持续更新)
- 定制化移动办公APP:打造企业专属的“钉钉”“纷享销客”,实现企业办公管理一体化
- 安装busybox玩玩
- 阿里云安装jdk报错gzip: stdin: unexpected end of file
- classifier.cc-recv() [ns2.35]
- lua的面向对象实现
- .net 面向对象程序设计深入](2)UML
- window搭建svn服务器,本地提交至服务器后,直接同步
- Hadoop学习---Zookeeper+Hbase配置学习
- Oracle表空间和用户常用语句