Python入门 —— 语音识别

栏目: Python · 发布时间: 8年前

内容简介:Python入门 —— 语音识别

Python 语音

实现语音操控的原理

语音操控分为语音识别和语音朗读两部分

我们使用speech模块实现语音模块(python 2.7)

SAPI是微软Speech API , 是微软公司推出的语音接口,而细心的人会发现从WINXP开始,系统上就已经有语音识别的功能了,可是用武之地相当之少,他并没有给出一些人性化的自定义方案,仅有的语音操控命令显得相当鸡胁。
  • Python pywin32 ,可以使 Python 调用WIN32COM接口,选择对应版本下载(区分32位/64位),直接双击运行即可

Python入门 —— 语音识别

  • 安装speech模块: pip install speech

实现个简易的控制电脑做事情的小程序:

  • 首先,来个测试文件

此处仅为启动和关闭语音系统

import speech
while True:
    phrase =speech.input()
    speech.say("You said %s"%phrase)
    if phrase =="turn off":
        break

Python入门 —— 语音识别

  • 自制个中文库
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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP and MySQL Web Development

PHP and MySQL Web Development

Luke Welling、Laura Thomson / Sams / July 25, 2007 / $49.99

Book Description PHP and MySQL Web Development teaches you to develop dynamic, secure, commerical Web sites. Using the same accessible, popular teaching style of the three previous editions, this b......一起来看看 《PHP and MySQL Web Development》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试