python 利用pyttsx3文字转语音 适用于macOS windows树莓派
2019-04-04 by 崔斐然
python 利用pyttsx3文字轉語音
更新于2019-05-08:
樹莓派安裝pyodbc失敗參考這里:
安裝 unixodbc-dev,
https://github.com/mkleehammer/pyodbc/wiki/Install#centos-7
踩坑:
報錯
>>> engine = pyttsx3.init() Traceback (most recent call last):File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyttsx3/__init__.py", line 44, in initeng = _activeEngines[driverName]File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/weakref.py", line 137, in __getitem__o = self.data[key]() KeyError: None解決辦法:
安裝 pyobjc pip install pyobjc或者使用python2 這個方法我沒試過,因為安裝pyobjc就已經解決問題了
解決辦法來源:https://github.com/nateshmbhat/pyttsx3/issues/1
適用于macOS,當前10.14.2可用
正式教程
安裝:
pip install pyttsx3anaconde下如何安裝第三方包詳見另一篇博文:
使用方法:
朗讀文字
import pyttsx3 engine = pyttsx3.init() engine.say('Sally sells seashells by the seashore.') engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()文字過長時打斷發音
import pyttsx3 def onWord(name, location, length):print('word', name, location, length)if location > 10:engine.stop()engine = pyttsx3.init() engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()使用真人發音
engine = pyttsx3.init() voices = engine.getProperty('voices') for voice in voices:engine.setProperty('voice', voice.id)engine.say('The quick brown fox jumped over the lazy dog.')engine.runAndWait()語速控制
engine = pyttsx3.init() rate = engine.getProperty('rate') engine.setProperty('rate', rate+50) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()音量控制
engine = pyttsx3.init() volume = engine.getProperty('volume') engine.setProperty('volume', volume-0.25) engine.say('The quick brown fox jumped over the lazy dog.') engine.runAndWait()語音引擎參數解釋
????類似于設計模式中的“工廠模式”,pyttsx3通過初始化來獲取語音引擎。當我們第一次調用init操作的時候,會返回一個pyttsx3的engine對象,再次調用的時候,如果存在engine對象實例,就會使用現有的,否則再重新創建一個。
pyttsx.init([driverName : string, debug : bool]) → pyttsx.Engine????從方法聲明上來看,第一個參數指定的是語音驅動的名稱,這個在底層適合操作系統密切相關的。如下:
drivename:由pyttsx3.driver模塊根據操作系統類型來調用,默認使用當前操作系統可以使用的最好的驅動
sapi5 - SAPI5 on Windows
nsss - NSSpeechSynthesizer on Mac OS X
espeak - eSpeak on every other platform
debug: 這第二個參數是指定要不要以調試狀態輸出,建議開發階段設置為True
引擎接口
| 方法簽名 | 參數列表 | 返回值 | 簡單釋義 |
| connect(topic : string, cb : callable)? | topic:要描述的事件名稱;cb:回調函數? | →?? dict? | 在給定的topic上添加回調通知 |
| disconnect(token : dict)? | token:回調失聯的返回標記? | Void | 結束連接 |
| endLoop() | None | → None? | 簡單來說就是結束事件循環 |
| getProperty(name : string)? | name有這些枚舉值“rate, vioce,vioces,volumn? | → object? | 獲取當前引擎實例的屬性值 |
| setProperty(name : string)? | name有這些枚舉值“rate, vioce,vioces,volumn | → object? | 設置當前引擎實例的屬性值 |
| say(text : unicode, name : string)? | text:要進行朗讀的文本數據; name: 關聯發音人,一般用不到 | → None | 預設要朗讀的文本數據,這也是“萬事俱備,只欠東風”中的“萬事俱備” |
| runAndWait() | None | → None | ?這個方法就是“東風”了。當事件隊列中事件全部清空的時候返回 |
| startLoop([useDriverLoop : bool])? | useDriverLoop:是否啟用驅動循環 | → None? | 開啟事件隊列 |
元數據音調在pyttsx3.voice.Voice中,處理合成器的發音。
age 發音人的年齡,默認為None
gender 以字符串為類型的發音人性別: male, female, or neutral.默認為None
id 關于Voice的字符串確認信息. 通過 pyttsx3.engine.Engine.setPropertyValue()來設置活動發音簽名. 這個屬性總是被定義。
languages 發音支持的語言列表,如果沒有,則為一個空的列表。
name 發音人名稱,默認為None.
總結
以上是生活随笔為你收集整理的python 利用pyttsx3文字转语音 适用于macOS windows树莓派的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 指针部分笔记~
- 下一篇: opencv摄像头 vmware虚拟机出