Pyttsx语音性别

时间:2017-03-16 22:28:01

标签: python python-2.7 voice pyttsx

下午好,我正在学习并使用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

6 个答案:

答案 0 :(得分:2)

我使用以下代码迭代声音以找到女声

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

答案 1 :(得分:0)

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

engine = pyttsx.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

答案 5 :(得分: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()