日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 调用event handler_扣丁学堂Python开发之调用语音模块时报错及解决方法

發布時間:2024/1/1 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 调用event handler_扣丁学堂Python开发之调用语音模块时报错及解决方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

扣丁學堂Python開發之調用語音模塊時報錯及解決方法

2018-03-09 11:45:57

3346瀏覽

python調用語音模塊時,遇見TypeError:NoneTypetakesnoarguments這種錯誤類型該如何解決

下面是一個用python實現語音識別的例子:

fromwin32com.clientimportconstants

importwin32com.client

importpythoncom

speaker=win32com.client.Dispatch("SAPI.SPVOICE")

classSpeechRecognition:

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)forwordinwordsToAdd]

self.grammar.Rules.Commit()

self.grammar.CmdSetRuleState("wordsRule",1)

self.grammar.Rules.Commit()

self.eventHandler=ContextEvents(self.context)

self.say("Startedsuccessfully")

defsay(self,phrase):

self.speaker.Speak(phrase)

classContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):

defOnRecognition(self,StreamNumber,StreamPosition,RecognitionType,Result):

newResult=win32com.client.Dispatch(Result)

print("說:",newResult.PhraseInfo.GetText())

s=newResult.PhraseInfo.GetText()

ifs=="生日快樂":

speaker.Speak("HappyBirthday")

else:

pass

if__name__=='__main__':

speaker.Speak("語音識別開啟")

wordsToAdd=["生日快樂"]

speechReco=SpeechRecognition(wordsToAdd)

whileTrue:

pythoncom.PumpWaitingMessages()

報錯:

classContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):

TypeError:NoneTypetakesnoarguments

報錯的原因是:不能調用語音開發包

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

1.在python36目錄中找到pythonwin文件夾

python36/lib/site-packages/pythonwin

2.在pythonwin文件夾下找到Pythonwin文件

3.雙擊Pythonwin運行

4.然后選擇工具tools/commakepyutility

5.然后選擇MicrosoftSpeechObjectLibrary5.4,點擊OK鍵

6。運行結果

好了,問題解決,現在可以執行代碼了,執行結果:(彈出語音識別設置)

OK,開始你的語音識別之旅吧,最后想要了解更多關于Python發展前景趨勢,請關注扣丁學堂python培訓官網、微信等平臺,扣丁學堂IT職業在線學習教育平臺為您提供最新的Python視頻教程系統,通過千鋒扣丁學堂金牌講師在線錄制的Python視頻教程課程,讓你快速掌握Python從入門到精通開發實戰技能。扣丁學堂Python開發工程師技術交流群:279521237。

【關注微信公眾號獲取更多學習資料】

標簽:

Python視頻教程

Python基礎教程

Python爬蟲

Python培訓

Python開發工程師

總結

以上是生活随笔為你收集整理的python 调用event handler_扣丁学堂Python开发之调用语音模块时报错及解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。