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开发之调用语音模块时报错及解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python计时器代码_Python功能
- 下一篇: 过年回家啦用python写一个宝石消消乐