pyttsx3 未知语音 ID

问题描述

我已经尝试了 2 多个小时试图解决这个问题,不是 100% 肯定,但我认为这与 sapi5.py 模块中的失败有关,因为我没有安装以下声音:

MSSAM = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\MSSam'
MSMARY = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\MSMary'
MSMIKE = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\MSMike'

我尝试添加到上面的列表中,将第一个条目更改为 MSGeorge,尝试降级到 python 3.6 和 3.7,尝试使用 pyttsx3 2.71,

还尝试更改注册表以添加 MSGeorge,就像本期“https://stackoverflow.com/questions/62756194/pyttsx3-module-is-not-showing-all-installed-voices”

我不认为任何人以前见过这个,因为我搜索了互联网和 YouTube,我找不到任何东西,所以我认为它很简单,

我在 pycharm 上运行带有 python 3.8.6 的 Windows 10 PC,

代码

import pyttsx3
engine = pyttsx3.init() # object creation
voices = engine.getProperty('voices')
print(voices)

错误

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\__init__.py",line 20,in init
    eng = _activeEngines[driverName]
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\weakref.py",line 131,in __getitem__
    o = self.data[key]()
KeyError: None

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/pythonProject/Jarvis/Jarvis.py",line 2,in <module>
    engine = pyttsx3.init() # object creation
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\__init__.py",line 22,in init
    eng = Engine(driverName,debug)
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\engine.py",line 30,in __init__
    self.proxy = driver.DriverProxy(weakref.proxy(self),driverName,debug)
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\driver.py",line 52,in __init__
    self._driver = self._module.buildDriver(weakref.proxy(self))
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\drivers\sapi5.py",line 32,in buildDriver
    return SAPI5Driver(proxy)
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\drivers\sapi5.py",line 49,in __init__
    self.setProperty('voice',self.getProperty('voice'))
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\drivers\sapi5.py",line 102,in setProperty
    token = self._tokenFromId(value)
  File "C:\Users\user\PycharmProjects\venv\lib\site-packages\pyttsx3\drivers\sapi5.py",line 86,in _tokenFromId
    raise ValueError('unkNown voice id %s',id_)
ValueError: ('unkNown voice id %s','HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech_OneCore\\Voices\\Tokens\\MSTTS_V110_enGB_GeorgeM')

解决方法

好吧,我想我终于修复了它,结果是位于下面函数中的 raise 值错误似乎有问题:

def _tokenFromId(self,id_):
    tokens = self._tts.GetVoices()
    for token in tokens:
        if token.Id == id_:
            return token
#    raise ValueError('unknown voice id %s',id_)

为了修复错误,我注释掉了 raise ValueError 行并且代码有效,当我通过 pycharm 进行调试时,我可以看到语音 ID 被传递到函数中,但由于某种原因,if 语句返回了一个 false。

>

无论如何现在似乎都在工作,

文件的位置是驱动器号,例如 F:...\Lib\site-packages\pyttsx3\drivers 向下滚动直到找到上面的函数,然后注释掉上面的代码,