Python Web版语音合成实例详解


Posted in Python onJuly 16, 2019

前言

语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速、音调、音量设置,打破传统文字式人机交互的方式,让人机沟通更自然。

应用场景

将游戏场景中的公告、任务或派单信息通过语音播报,让玩家玩游戏或配送员送货的同时,也可接听新任务。

文学小说类软件,可以利用百度语音合成技术将文学小说作品进行高质量的朗读,流畅清晰,解放双眼,畅听世界。

软件架构

Python3.7.2、Django2.1.7、baidu-aip(百度语音API)

案例

这里只展示部分代码,有兴趣的同学可以自行下载源码安装调试。

import os
import time
import codecs
from aip import AipSpeech
from django.shortcuts import render
from django.http import HttpResponse


'''
pip install --upgrade pip
pip install django
pip install baidu-aip
'''


def main(request):
 return render(request, 'index.html')


def m_main(request):
 return render(request, 'm_index.html')


def convert(request):
 message = request.POST.get("message")
 switch = request.POST.get("switch")
 mp3 = du_say(message, switch)
 return HttpResponse(mp3)


def du_say(message, switch):
 write_txt(message)
 app_id = '*****'
 api_key = '*****'
 secret_key = '*****'
 client = AipSpeech(app_id, api_key, secret_key)
 if switch == "true":
  switch = 3
 else:
  switch = 4
 result = client.synthesis(message, 'zh', 1, {
  'vol': 5, 'per': switch,
 })
 t = time.time()
 now_time = lambda: int(round(t * 1000))
 path = os.getcwd() + os.path.sep + "static" + os.path.sep + "audio"+os.path.sep
 audio = path+str(now_time())+'.mp3'
 # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
 if not isinstance(result, dict):
  with open(audio, 'wb') as f:
   f.write(result)
 return str(now_time())+'.mp3'


def write_txt(message):
 t = time.time()
 now_time = lambda: int(round(t * 1000))
 path = os.getcwd() + os.path.sep + "static" + os.path.sep + "text"+os.path.sep
 text = path+str(now_time())+'.txt'
 with codecs.open(text, 'a', encoding='utf8')as f:
  f.write(message)

本地部署

从码云拉取项目到本地:

https://gitee.com/52itstyle/baidu-speech.git

配置百度语音API:

# 自行注册申请
https://console.bce.baidu.com/ai/#/ai/speech/app/list

启动项目:

# 切换到项目根目录,执行
manage.py runserver

Python Web版语音合成实例详解 

外网部署

这里以Linux为例,代理使用 openresty。

安装 Python3

wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz

事先安装依赖,否则后期安装会报错:

yum -y install zlib*
yum -y install libffi-devel

下面开始正式安装:

# 解压
tar -xvf Python-3.7.1.tar.xz
# 切换大目录
cd Python-3.7.1
# 配置编译
./configure
# 编译安装
make && make install

安装 Django

pip install Django

安装成功以后需要重新配置并编译安装 Python3:

# 配置编译
./configure
# 编译安装
make && make install

安装服务器 uwsgi

pip3 install uwsgi

上传项目到服务器,并切换到 speech 目录:

# 目录下新建文件夹
mkdir script

在 script 下新增 uwsgi.ini (项目中已经配置好,自行修改路径即可):

# uwsig使用配置文件启动
[uwsgi]
# 项目目录
chdir=/www/speech/
# 指定项目的application
module=speech.wsgi:application
# 指定sock的文件路径  
socket=/www/speech/script/uwsgi.sock
# 进程个数  
workers=5
pidfile=/www/speech/script/uwsgi.pid
# 指定IP端口  
http=127.0.0.1:8001
# 指定静态文件
static-map=/static=/www/speech/static
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 启用主进程
master=true
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容,如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=/www/speech/script/uwsgi.log

然后使用以下命令启动:

uwsgi --ini uwsgi.ini

执行命令,查看是否启动成功:

[root@AY140216131049Z script]# ps -ef|grep uwsgi 
root  3040  1 0 Nov21 ?  00:00:03 uwsgi --ini uwsgi.ini
root  3041 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  3042 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  3043 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  3044 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  3045 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  3046 3040 0 Nov21 ?  00:00:00 uwsgi --ini uwsgi.ini
root  6606 6580 0 18:13 pts/0 00:00:00 grep --color=auto uwsgi

重启:

uwsgi --reload uwsgi.pid

配置Nginx代理:

server {
  listen 80;
  server_name speech.52itstyle.vip;
  charset utf-8;
  location / {
   include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
   uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
   uwsgi_pass unix:/www/speech/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
  }
  # 动静分离 Nginx 处理静态请求
  location /static {
   root /www/speech/;
  }
 }

如果启动HTTPS:

server {
   listen 80;
   listen 443 ssl;
   server_name speech.52itstyle.vip;
   #ssl on;
   #证书路径
   ssl_certificate /usr/local/openresty/nginx/cert/1901523_speech.52itstyle.vip.pem;
   #私钥路径
   ssl_certificate_key /usr/local/openresty/nginx/cert/1901523_speech.52itstyle.vip.key;
   #缓存有效期
   ssl_session_timeout 5m;
   #可选的加密算法,顺序很重要,越靠前的优先级越高.
   ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
   #安全链接可选的加密协议
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   ssl_prefer_server_ciphers on;

  location / {
   include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
   uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
   uwsgi_pass unix:/www/speech/script/uwsgi.sock; # 指定uwsgi的sock文件所有动态请求就会直接丢给他
  }
  # 动静分离 Nginx 处理静态请求
  location /static {
   root /www/speech/;
  }
 }

演示地址

https://speech.52itstyle.vip/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现socket端口重定向示例
Feb 10 Python
Python3爬虫学习之将爬取的信息保存到本地的方法详解
Dec 12 Python
在python中使用xlrd获取合并单元格的方法
Dec 26 Python
Django 实现admin后台显示图片缩略图的例子
Jul 28 Python
python实现邮件发送功能
Aug 10 Python
python使用PIL和matplotlib获取图片像素点并合并解析
Sep 10 Python
python中bytes和str类型的区别
Oct 21 Python
Python3和pyqt5实现控件数据动态显示方式
Dec 13 Python
Python3和PyCharm安装与环境配置【图文教程】
Feb 14 Python
python的链表基础知识点
Sep 13 Python
python通过cython加密代码
Dec 11 Python
Python编解码问题及文本文件处理方法详解
Jun 20 Python
windows下python虚拟环境virtualenv安装和使用详解
Jul 16 #Python
Pandas中DataFrame的分组/分割/合并的实现
Jul 16 #Python
Python的matplotlib绘图如何修改背景颜色的实现
Jul 16 #Python
python调用其他文件函数或类的示例
Jul 16 #Python
Python PyQt5 Pycharm 环境搭建及配置详解(图文教程)
Jul 16 #Python
用Anaconda安装本地python包的方法及路径问题(图文)
Jul 16 #Python
python查找重复图片并删除(图片去重)
Jul 16 #Python
You might like
PHP 裁剪图片成固定大小代码方法
2009/09/09 PHP
php计算两个日期时间差(返回年、月、日)
2014/06/19 PHP
PHP中使用curl入门教程
2015/07/02 PHP
CodeIgniter多语言实现方法详解
2016/01/20 PHP
php实现数据库的增删改查
2017/02/26 PHP
jQuery过滤选择器:not()方法使用介绍
2014/04/20 Javascript
简单的jquery左侧导航栏和页面选中效果
2014/08/21 Javascript
jquery.validate 自定义验证方法及validate相关参数
2016/01/18 Javascript
基于JS代码实现图片在页面中旋转效果
2016/06/16 Javascript
使用grunt合并压缩js和css文件的方法
2017/03/02 Javascript
jQuery插件echarts实现的去掉X轴、Y轴和网格线效果示例【附demo源码下载】
2017/03/04 Javascript
JS实现的简单拖拽购物车功能示例【附源码下载】
2018/01/03 Javascript
详解ECMAScript typeof用法
2018/07/25 Javascript
Vue.js中该如何自己维护路由跳转记录
2019/05/19 Javascript
js实现固定区域内的不重叠随机圆
2019/10/24 Javascript
解决vue初始化项目时,一直卡在Project description上的问题
2019/10/31 Javascript
解决vue自定义全局消息框组件问题
2019/11/22 Javascript
python使用urllib2实现发送带cookie的请求
2015/04/28 Python
利用Python如何实现数据驱动的接口自动化测试
2018/05/11 Python
python中单下划线_的常见用法总结
2018/07/10 Python
Python中 map()函数的用法详解
2018/07/10 Python
安装PyInstaller失败问题解决
2019/12/14 Python
python函数定义和调用过程详解
2020/02/09 Python
使用Python第三方库pygame写个贪吃蛇小游戏
2020/03/06 Python
聊聊python中的异常嵌套
2020/09/01 Python
推荐10个CSS3 制作的创意下拉菜单效果
2014/02/11 HTML / CSS
利用纯CSS3实现动态的自行车特效源码
2017/01/20 HTML / CSS
Ralph Lauren意大利官方网站:时尚界最负盛名的品牌之一
2018/10/18 全球购物
密封类可以有虚函数吗
2014/08/11 面试题
大课间活动制度
2014/01/18 职场文书
创业计划书的写作技巧及要点
2014/01/31 职场文书
2014年幼师工作总结
2014/11/22 职场文书
2015年行政人事部工作总结
2015/05/13 职场文书
忠诚教育学习心得体会
2016/01/23 职场文书
《有余数的除法》教学反思
2016/02/22 职场文书
Golang 实现WebSockets
2022/04/24 Golang