一、简介
准备用基于esp8266的nodemcu开发板做一个天气时钟。
一步一步记录代码编写过程。
竹壳天气时钟
Bamboo shell weather clock
使用基于esp8266的NodeMCU制作。
计划用竹子做最后成品的外壳,所以才有了这个名称。
上一篇文章已经完成了时钟和天气的显示,下一步要做语音控制报时和报天气,今天记录一下我使用 三丰云 免费云服务器 搭建TTS的过程。三丰云 https://www.sanfengyun.com 我已经用了一段时间,感觉还是很不错的,运行也很稳定。
二、TTS服务器搭建
在云服务器上安装Ubuntu Linux,安装docker-ce docker-ce-cli
然后添加docker代理,复制后粘贴到shell终端即可
bash
sudo tee /etc/docker/daemon.json <<-"EOF"
{
"registry-mirrors": ["https://dockerpull.com"]
}
EOF
然后建立一个tts文件夹并进入,以下代码复制后粘贴到shell终端即可
bash
tee ~/tts/gunicorn.conf <<-'EOF'
# gunicorn.conf
# 并行工作进程数
workers = 4
# 指定每个工作者的线程数
threads = 2
# 监听内网端口5000
bind='0.0.0.0:2020'
# 设置守护进程,将进程交给supervisor管理
daemon = 'false'
# 工作模式协程
worker_class = 'gevent'
# 设置最大并发量
worker_connections = 2000
# 设置进程文件目录
pidfile = 'gunicorn.pid'
# 设置访问日志和错误信息日志路径
accesslog = 'gunicorn_acess.log'
errorlog = 'gunicorn_error.log'
# 设置日志记录水平
loglevel = 'debug'
EOF
tee ~/tts/Dockerfile <<-'EOF'
FROM python:3.8.4
COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
COPY . /flask_project/
WORKDIR /flask_project/
EOF
tee ~/tts/dockermake.sh <<-'EOF'
docker build -t python_tts .
EOF
tee ~/tts/requirements.txt <<-'EOF'
Flask
flask_cors
cos-python-sdk-v5
gunicorn
gevent
edge-tts
EOF
tee ~/tts/edge-tts.py <<-'EOF'
'''
原项目作者:https://github.com/lyz1810/edge-tts
'''
import logging
import os
import re
import sys
import uuid
from flask import Flask, request, send_file
from qcloud_cos import CosConfig, CosS3Client
from flask_cors import CORS
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
voiceMap = {
"xiaoxiao": "zh-CN-XiaoxiaoNeural",
"xiaoyi": "zh-CN-XiaoyiNeural",
"yunjian": "zh-CN-YunjianNeural",
"yunxi": "zh-CN-YunxiNeural",
"yunxia": "zh-CN-YunxiaNeural",
"yunyang": "zh-CN-YunyangNeural",
"xiaobei": "zh-CN-liaoning-XiaobeiNeural",
"xiaoni": "zh-CN-shaanxi-XiaoniNeural",
"hiugaai": "zh-HK-HiuGaaiNeural",
"hiumaan": "zh-HK-HiuMaanNeural",
"wanlung": "zh-HK-WanLungNeural",
"hsiaochen": "zh-TW-HsiaoChenNeural",
"hsioayu": "zh-TW-HsiaoYuNeural",
"yunjhe": "zh-TW-YunJheNeural",
}
def getVoiceById(voiceId):
return voiceMap.get(voiceId)
# 删除html标签
def remove_html(string):
regex = re.compile(r'<[^>]+>')
return regex.sub('', string)
def createAudio(text, file_name, voiceId):
new_text = remove_html(text)
print(f"Text without html tags: {new_text}")
voice = getVoiceById(voiceId)
if not voice:
return "error params"
pwdPath = os.getcwd()
filePath = pwdPath + "/" + file_name
dirPath = os.path.dirname(filePath)
if not os.path.exists(dirPath):
os.makedirs(dirPath)
if not os.path.exists(filePath):
# 用open创建文件 兼容mac
open(filePath, 'a').close()
script = 'edge-tts --voice ' + voice + ' --text "' + new_text + '" --write-media ' + filePath
os.system(script)
return filePath # 返回生成的音频文件路径
def getParameter(paramName):
if request.args.__contains__(paramName):
return request.args[paramName]
return ""
@app.route('/dealAudio',methods=['POST','GET'])
def dealAudio():
text = getParameter('text')
file_name = str(uuid.uuid4()) + ".mp3"
voice = getParameter('voice')
audio_file_path = createAudio(text, file_name, voice)
return send_file(audio_file_path, as_attachment=True)
@app.route('/')
def index():
return 'tts!'
if __name__ == "__main__":
EOF
tee ~/tts/run_python_tts <<-'EOF'
docker run -d -p 2020:2020 --name python_tts python_tts gunicorn edge-tts:app -c gunicorn.conf
EOF
容器制作与启动方法
bash
#添加运行权限
chmod +x ./dockermake.sh
#运行脚本制作镜像
./dockermake.sh
#运行容器
bash run_python_tts
API:服务器ip:2020/dealAudio?text=欢迎使用tts&voice=xiaoxiao
text 是你需要转换的文本
voice 是配音员 上面自己选