国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:24日期:2022-07-11 18:27:25

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

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

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

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

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

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

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

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

后記

推薦一個(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)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 威宁| 沿河| 清丰县| 来凤县| 广饶县| 丹巴县| 文安县| 财经| 原平市| 武夷山市| 方城县| 小金县| 稷山县| 舟山市| 噶尔县| 桃源县| 甘泉县| 宜章县| 石门县| 同德县| 姜堰市| 平凉市| 修水县| 阳信县| 竹山县| 桐梓县| 宜宾县| 赣州市| 广平县| 靖西县| 荆门市| 东乡族自治县| 平定县| 崇左市| 祁连县| 南宫市| 洪江市| 措美县| 玛纳斯县| 东平县| 平舆县|