2017-03-16 74 views
0

下午好,我在學習和使用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 

回答

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 \微軟\語音\聲音\令牌\ 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

,如果你使用Linux/espeak的...

改變這裏engine.setProperty('voice', 'english+f1')

代碼,你可以通過添加F1至F4

更改語音
0

這是一個更簡單的解決方案:

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