python之語音識(shí)別speech模塊
1.原理
語音操控分為 語音識(shí)別和語音朗讀兩部分。
這兩部分本來是需要自然語言處理技能相關(guān)知識(shí)以及一系列極其復(fù)雜的算法才能搞定,可是這篇文章將會(huì)跳過此處,如果你只是對算法和自然語言學(xué)感興趣的話,就只有請您移步了,下面沒有一個(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í)別)
#!/usr/bin/env python# -*- codinfg:utf-8 -*-’’’@author: Jeff LEE@file: .py@time: 2018-07-19 11:15@desc:’’’from win32com.client import constantsimport osimport win32com.clientimport 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í)別信息對應(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://www.jb51.net/article/195212.htm
到此這篇關(guān)于python之語音識(shí)別speech模塊的文章就介紹到這了,更多相關(guān)python 語音識(shí)別內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法2. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲3. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明4. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向7. asp中response.write("中文")或者js中文亂碼問題8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. CSS hack用法案例詳解10. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
