使用 pyttsx3 模块向语句添加语音时出现“VoiceAge”错误

2024-04-12

我基本上是在聊天机器人的帮助下开发一个聊天机器人。现在为了向机器人给出的响应添加语音,我使用了 pyttsx3 模块,但最终结果是这样的。 我什至尝试将这个模块实现为其他声音,因为它显示有 3 个声音可用。

engine = pp.init()
voices=engine.getProperty('voices')
print(voices)
engine.setProperty('voice',voices[0].id)#


def speak(word):
  engine.say(word)
  engine.runAndWait()
    

对于这段代码我得到了这个错误

KeyError                                  Traceback (most recent call last)
Input In [2], in <cell line: 2>()
      1 engine = pp.init()
----> 2 voices=engine.getProperty('voices')
      3 print(voices)
      4 engine.setProperty('voice',voices[0].id)#

File /opt/anaconda3/lib/python3.9/site-packages/pyttsx3/engine.py:146, in Engine.getProperty(self, name)
    128 def getProperty(self, name):
    129     """
    130     Gets the current value of a property. Valid names and values include:
    131 
   (...)
    144     @raise KeyError: When the property name is unknown
    145     """
--> 146     return self.proxy.getProperty(name)

File /opt/anaconda3/lib/python3.9/site-packages/pyttsx3/driver.py:173, in DriverProxy.getProperty(self, name)
    164 def getProperty(self, name):
    165     '''
    166     Called by the engine to get a driver property value.
    167 
   (...)
    171     @rtype: object
    172     '''
--> 173     return self._driver.getProperty(name)

File /opt/anaconda3/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py:69, in NSSpeechDriver.getProperty(self, name)
     66 @objc.python_method
     67 def getProperty(self, name):
     68     if name == 'voices':
---> 69         return [self._toVoice(NSSpeechSynthesizer.attributesForVoice_(v))
     70                 for v in list(NSSpeechSynthesizer.availableVoices())]
     71     elif name == 'voice':
     72         return self._tts.voice()

File /opt/anaconda3/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py:69, in <listcomp>(.0)
     66 @objc.python_method
     67 def getProperty(self, name):
     68     if name == 'voices':
---> 69         return [self._toVoice(NSSpeechSynthesizer.attributesForVoice_(v))
     70                 for v in list(NSSpeechSynthesizer.availableVoices())]
     71     elif name == 'voice':
     72         return self._tts.voice()

File /opt/anaconda3/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py:64, in NSSpeechDriver._toVoice(self, attr)
     60 except KeyError:
     61     lang = attr['VoiceLanguage']
     62 return Voice(attr['VoiceIdentifier'], attr['VoiceName'],
     63              [lang], attr['VoiceGender'],
---> 64              attr['VoiceAge'])

File /opt/anaconda3/lib/python3.9/site-packages/objc/_convenience_mapping.py:18, in __getitem__objectForKey_(self, key)
     16 def __getitem__objectForKey_(self, key):
     17     res = self.objectForKey_(container_wrap(key))
---> 18     return container_unwrap(res, KeyError, key)

File /opt/anaconda3/lib/python3.9/site-packages/objc/_convenience.py:134, in container_unwrap(v, exc_type, *exc_args)
    132 def container_unwrap(v, exc_type, *exc_args):
    133     if v is None:
--> 134         raise exc_type(*exc_args)
    135     elif v is _NULL:
    136         return None

KeyError: 'VoiceAge'

接下来我能做什么...


我今天遇到了同样的问题。 更好的方法是在 nsss.py 中处理 VoiceAge。 但我的解决方案比较粗暴,只是编辑pyttsx3的nsss.py文件,删除,attr['语音时代'] part.

根据您的情况,您可以编辑/opt/anaconda3/lib/python3.9/site-packages/pyttsx3/drivers/nsss.py在64行。

然后它就会起作用。 更多详细信息可以参见https://github.com/nateshmbhat/pyttsx3/issues/248 https://github.com/nateshmbhat/pyttsx3/issues/248 .

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 pyttsx3 模块向语句添加语音时出现“VoiceAge”错误 的相关文章

随机推荐