zoukankan      html  css  js  c++  java
  • Python入门 —— 06语音识别

    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()
    
    
    ### ||| || | Detanima in Dark
  • 相关阅读:
    数据库:数据操作-数据的增删改
    数据库:表操作-完整性约束
    数据库:表操作-枚举类型与集合类型
    数据库:表操作-字符串类型
    数据库:表操作-数据类型(日期类型)
    数据库:表操作-数据类型(数值类型)
    python并发编程:IO模型比较分析
    java学习(二)--excel导出
    格式化字段
    myeclipse的常用快捷键
  • 原文地址:https://www.cnblogs.com/darksouls/p/8260762.html
Copyright © 2011-2022 走看看