1.安装pyttsx3
pip install pyttsx3
2.运行pyttsx3.spaek()
import pyttsx3
pyttsx3.speak("The first photo has been collected!")
pyttsx3.speak("I have finished the task!")
结果出现了错误:OSError: libespeak.so.1: 无法打开共享对象文件: 没有那个文件或目录
这是因为没有libespeak.so.1,既然没有,那就安装(我用的是ubuntu):
sudo apt install espeak
之后再次运行pyttsx3.speak()就可以了
3.使用pyttsx3语音引擎
通过对pyttsx3初始化来获取语音引擎,之后可以对读音速率及音量进行自定义
engine = pyttsx3.init()
engine.setProperty('rate',150) # 速率
engine.setProperty('volume',0.6) # 音量
voices = engine.getProperty('voices') # 获取语音合成器
for voice in voices:
print(voice)
engine.setProperty('voice',voices[11].id) # 选择english语音合成器
engine.say("The first photo has been collected!")
engine.runAndWait()
engine.stop()
这个效果的话可以通过选择不同的语音合成器来改善,不过效果还是... 如果只是简单的用一下python的语音播报也可以了,如果要追求这个质量的话可以去百度和科大讯飞,试试他们的语音功能。
4.可直接调用 ttsxspeak.py
import pyttsx3
class Ttsx:
def __init__(self):
self.engine = pyttsx3.init()
self.engine.setProperty('rate',150) # 速率
self.engine.setProperty('volume',0.6) # 音量
voices = self.engine.getProperty('voices') # 获取语音合成器
# for voice in voices:
# print(voice)
self.engine.setProperty('voice',voices[11].id) # 选择english语音合成器
def speak(self, text):
self.engine.say(text)
self.engine.runAndWait()
def stop(self):
self.engine.stop()
调用:
from ttsxspeak import Ttsx
ttsx = Ttsx()
ttsx.speak("The first photo has been collected!")
ttsx.speak("I have finished the task!")
ttsx.stop()