zoukankan      html  css  js  c++  java
  • 【TTS】如何使用pyinstaller将一个写好的python项目打包成exe可执行文件

    @

    前言

    这篇文章的是上一篇【TTS】文本转语音?如何不调用第三方api来实现,使用pyttsx3,的后续扩展部署。
    在这里插入图片描述
    在使用pyttsx3实现了文本转语音之后,接下来就是部署了,不过可惜的是服务器是centos的,会导致项目里面的pyttsx3无法使用。

    所以只能退而求之,使用window系统算了。本次就是记录一下如何把源码打成不需要python环境依赖的exe文件(当然liunx也行的,大家可以查一查,只不过本次因为项目原因只能使用window了)

    开始发车 ,呸!,发代码咯
    在这里插入图片描述

    一、项目代码

    因为接口非常非常少,所以也就没细分模块了,一个py文件就可以解决了

    在这里插入图片描述
    app.py

    from flask import Flask
    import win32api,win32con
    from threading import Timer
    import pyttsx3
    import time
    import os
    from flask import Flask,  send_file,request
    
    app = Flask(__name__)
    
    """
    启动时提示
    """
    win32api.MessageBox(0, "成功启动语音合成服务", "成功提示", win32con.MB_OK)
    
    """
    定时删除static文件下的数据
    """
    # 删除函数
    def delDir(inc):
        if os.path.exists('.static') is True:
            deal_list = os.listdir(".static")
            # 清空static下面的文件
            for list in deal_list :
                if os.path.isfile(f'.static{list}') is True:
                    os.remove(f'.static{list}')
        t = Timer(inc, delDir, (inc,))
        t.start()
    # 10s
    delDir(10)
    
    """
    调用接口
    """
    @app.route('/voice')
    def Text_To_Speek():
        # 判断该目录下是否存在有static文件
        if os.path.exists('.static') is False:
            os.mkdir('.static')
            print(".static创建成功")
        str =  request.args['str'];
        t = time.time()
        engine = pyttsx3.init()
        # 设置新的语音速率
        engine.setProperty('rate', 200)
    
        # 设置新的语音音量,音量最小为 0,最大为 1
        engine.setProperty('volume', 1.0)
        # 获取当前语音声音的详细信息
        voices = engine.getProperty('voices')
    
        print(f'语音声音详细信息:{voices}')
        # # 设置当前语音声音为女性,当前声音不能读中文
        # engine.setProperty('voice', voices[1].id)
        # 设置当前语音声音为男性,当前声音可以读中文
        engine.setProperty('voice', voices[0].id)
    
        # 语音播报内容
        content = str
    
        # 输出文件格式
        outFile = f'.static{t}.mp3'
    
        engine.save_to_file(content, outFile)
        engine.runAndWait()
        engine.stop()
    
        return send_file(f".static{t}.mp3");
    
    @app.route('/')
    def hello_world():
        return "hello world";
    
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
    
    

    二、项目打包

    (1)打包前提-创建hook-pytts3.py

    这个是处理pytts3打包时会出现的错误的:

    ModuleNotFoundError: No module named 'pyttsx3.drivers'

    1、创建hooks
    在这里插入图片描述
    2、hook-pytts3.py的内容

    #-----------------------------------------------------------------------------
    # Copyright (c) 2013-2017, PyInstaller Development Team.
    #
    # Distributed under the terms of the GNU General Public License with exception
    # for distributing bootloader.
    #
    # The full license is in the file COPYING.txt, distributed with this software.
    #-----------------------------------------------------------------------------
    
    
    """
    pyttsx3 imports drivers module based on specific platform.
    Fount at https://github.com/nateshmbhat/pyttsx3/issues/6
    """
    
    
    hiddenimports = [
        'pyttsx3.drivers',
        'pyttsx3.drivers.dummy',
        'pyttsx3.drivers.espeak',
        'pyttsx3.drivers.nsss',
        'pyttsx3.drivers.sapi5',
    ]
    

    (2)打包命令

    参考:PyInstaller使用教程
    在这里插入图片描述

    在根目录下使用命令:

    pyinstaller -F -w app.py --additional-hooks-dir hooks
    

    在--additional-hooks-dir 后面是我们刚刚创建好的hooks文件夹。
    在这里插入图片描述
    若是出现其他问题请参考
    http://www.lolpzili.com/index.php/archives/20/#comment-2


    (3)项目打包-发现exe文件所在

    在这里插入图片描述


    (4)项目打包-创建unapp.bat关闭脚本

    查看进程:

    tasklist |findstr app
    

    创建一个txt文件,然后填写命令,最后改成bat后缀即可

    unapp.bat

    taskkill -f -im app.exe -t
    @mshta vbscript:msgbox("语言合成服务关闭成功",64,"关闭提示")(window.close)
    
    

    三、项目演示

    双击exe文件,然后输入:

    localhost:5000/voice?str=内容
    

    在这里插入图片描述


    后言

    如果对服务器没什么极大要求的话,这个方法就足以满足语音合成的需要了

    在这里插入图片描述
    至于为什么要搞这个呢,还不是搞个线下的api,如果可以的话,大可使用那些大厂的在线api,简单方便粗暴。

    嘿嘿
    在这里插入图片描述

    参考资料:
    PyInstaller使用教程
    Python 实现定时任务
    python中对文件、文件夹,目录的基本操作
    Pyinstaller IntFlag、vcruntime140.dll及pyttsx3错误的解决办法

  • 相关阅读:
    帮你拿下 Java 面试题!
    JavaGUI编程的宿世此生
    从事Java开发可以往那些方面发展
    java初学者都要掌握的案例
    两数之和(java)
    2020年java就业前景
    300 行代码带你秒懂 Java 多线程!
    安卓程序员如何快速入门后端开发常识
    连夜整理的Java开源项目,有后台管理类、商城类、秒杀类、支付类
    Java面试题及答案,2020年最新面试题集合
  • 原文地址:https://www.cnblogs.com/liwangwang/p/13522053.html
Copyright © 2011-2022 走看看