首页
学习
活动
专区
工具
TVP
发布

Python开发之调用语音模块时报错及解决方法

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)

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()

报错:

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,开始你的语音识别之旅吧!!!

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180309A0IYOC00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券