小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

python之語音識(shí)別(speech模塊)

 刮骨劍 2019-06-23

1.原理

語音操控分為 語音識(shí)別和語音朗讀兩部分。

這兩部分本來是需要自然語言處理技能相關(guān)知識(shí)以及一系列極其復(fù)雜的算法才能搞定,可是這篇文章將會(huì)跳過此處,如果你只是對(duì)算法和自然語言學(xué)感興趣的話,就只有請(qǐng)您移步了,下面沒有一個(gè)字會(huì)講述到這些內(nèi)容。

早在上世紀(jì)90年代的時(shí)候,IBM就推出了一款極為強(qiáng)大的語音識(shí)別系統(tǒng)-vio voice , 而其后相關(guān)產(chǎn)品層出不窮,不斷的進(jìn)化和演變著。 我們這里將會(huì)使用SAPI實(shí)現(xiàn)語音模塊。

2. 什么是SAPI?

SAPI是微軟Speech API , 是微軟公司推出的語音接口,而細(xì)心的人會(huì)發(fā)現(xiàn)從WINXP開始,系統(tǒng)上就已經(jīng)有語音識(shí)別的功能了,可是用武之地相當(dāng)之少,他并沒有給出一些人性化的自定義方案,僅有的語音操控命令顯得相當(dāng)雞脅。 那么這篇文章的任務(wù)就是利用SAPI進(jìn)行個(gè)性化的語音識(shí)別

代碼

前提:打開win7的語音自動(dòng)識(shí)別(控制面板--輕松訪問--語音識(shí)別)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: .py
@time: 2018-07-19 11:15
@desc:
'''
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()
        # 下面即為語音識(shí)別信息對(duì)應(yīng),打開響應(yīng)操作
        if  speechstr=="記事本":
            os.system('notepad'
        elif  speechstr=="寫字板":
            os.system('write')
        elif  speechstr=="畫圖板":
            os.system('mspaint')
        else:
            pass
if __name__ == '__main__':
    speaker.Speak("語音識(shí)別開啟")
    wordsToAdd = ["記事本", "寫字板","畫圖板",]
    speechReco = SpeechRecognition(wordsToAdd)
    while True:
        pythoncom.PumpWaitingMessages()

  調(diào)試遇到問題

python調(diào)用語音模塊時(shí),遇見TypeError:NoneTypetakesnoarguments這種錯(cuò)誤類型該如何解決

報(bào)錯(cuò)的原因是:不能調(diào)用語音開發(fā)包

解決方法:(如果你已經(jīng)安裝了pyWin32,它也安裝了PythonWin)

1.在python35目錄中找到pythonwin文件夾下的pythonwin.exe

2.雙擊Pythonwin運(yùn)行,然后選擇工具tools/commakepyutility

3.然后選擇MicrosoftSpeechObjectLibrary5.4,點(diǎn)擊OK鍵

4.運(yùn)行結(jié)果如下,問題解決

 

后記

推薦一個(gè)不錯(cuò)的語音識(shí)別文檔:https://blog.csdn.net/j2IaYU7Y/article/details/79878310

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多