2

下午好,我正在学习并使用 pyttsx 进行语音,问题是我想将其用作“女性”声音,但我无法使用以下代码:

import pyttsx as pt
from pyttsx import voice
engine = pt.init()
voices = engine.getProperty('voices')
#engine.setProperty('gender', 'female') # also does not work
engine.setProperty('female', voice.Voice.gender) #not even
engine.setProperty('female', voice.gender) #does not work
engine.setProperty('voice', voices[4].id)
engine.say("Hello World")
engine.runAndWait()


class Voice(object):
    def __init__(self, id, name=None, languages=[], gender=None, age=None):
        self.id = id
        self.name = name
        self.languages = languages
        self.gender = gender
        self.age = age
4

7 回答 7

5

我使用以下代码遍历声音以找到女性声音

import pyttsx
engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
   print voice.id
   engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

在我的 Windows 10 机器上,女性声音是 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0

所以我改变了我的代码看起来像这样

import pyttsx
engine = pyttsx.init()
engine.setProperty('voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait() 
于 2017-03-18T18:42:46.010 回答
5

如果你使用 linux/espeak...

在此处更改代码engine.setProperty('voice', 'english+f1')

您可以通过添加 f1 直到 f4 来更改声音

于 2017-05-12T07:43:47.590 回答
2

这是一个更简单的解决方案:

engine = pyttsx.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
于 2017-05-14T04:02:49.627 回答
1

使用sound=getProperty ('voices'); engine.setProperty('voice','sound [1].id') 这肯定会奏效。0代表男性,1代表女性。

于 2019-04-19T18:54:49.163 回答
0

您必须检查您的计算机是否有其他讲述人选项,然后转到控制面板 - > 轻松访问中心 - > 讲述人选项,您可以选择有多少个讲述人。其他选项转到 Windows 搜索旁白。如果您没有用代码编写的其他旁白,则默认旁白将仅起作用。

于 2020-10-04T11:59:42.493 回答
0

我使用这个辅助函数,它迭代声音。如果存在特定语言和性别的声音,它将变为该声音,否则将引发异常。

# language  : en_US, de_DE, ...
# gender    : VoiceGenderFemale, VoiceGenderMale
def change_voice(engine, language, gender='VoiceGenderFemale'):
    for voice in engine.getProperty('voices'):
        if language in voice.languages and gender == voice.gender:
            engine.setProperty('voice', voice.id)
            return True

    raise RuntimeError("Language '{}' for gender '{}' not found".format(language, gender))

最后它将像这样使用:

engine = pt.init()
change_voice(engine, "en_US", "VoiceGenderFemale")
engine.say("Hello World")
engine.runAndWait()
于 2021-02-21T20:14:28.883 回答
-1
import pyttsx as pt
from pyttsx import voice
engine = pt.init()
voices = engine.getProperty('voices')
#engine.setProperty('gender', 'female') # also does not work
#engine.setProperty('female', voice.Voice.gender) #not even
#engine.setProperty('female', voice.gender) #does not work
engine.setProperty('voice', voices[1].id)
engine.say("Hello World")
engine.runAndWait
于 2019-04-15T12:49:39.130 回答