内容简介:Python入门 —— 语音识别
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)
发现网上有个语音识别框架:
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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 利用声纹识别技术识别400电话语音性别
- [译] 语音识别 2019 指南
- iOS快速入手语音识别、评测
- 使用Tensorflow识别语音关键词
- 图像识别攻击还没完全解决,语音识别攻击又来了!
- 语音识别语言模型和拼音字典文件制作
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
代码本色:用编程模拟自然系统
Daniel Shiffman / 周晗彬 / 人民邮电出版社 / 2014-10 / 99.00元
本书介绍了用计算机模拟自然系统涉及的编程策略与技术,涵盖了基本的数学和物理概念,以及可视化地展示模拟结果所需的高级算法。读者将从构建基本的物理引擎开始,一步一步地学习如何创建智能移动的物体和复杂的系统,为进一步探索生成设计奠定基础。相关的知识点包括力、三角、分形、细胞自动机、自组织和遗传算法。本书的示例使用基于Java的开源语言及开发环境Processing编写。本书网站http://www.na......一起来看看 《代码本色:用编程模拟自然系统》 这本书的介绍吧!