cx_Freeze 打包 PRNet 为exe

PRNet:Joint 3D Face Reconstruction and Dense Alignment with Position Map Regression Network (ECCV 2018)
https://github.com/YadiraF/PRNet

一开始使用pyinstaller打包的,但是打包后的exe,运行起来也不报错,也没成功

运行环境
win10
python3.5
tensorflow1.12

(abcdefghijklmn) F:\B_software_3DFaceRec\PRNet>conda list
# packages in environment at D:\Program\Work\Anaconda3\envs\abcdefghijklmn:
#
# Name                    Version                   Build  Channel
absl-py                   0.8.0                    pypi_0    pypi
astor                     0.8.0                    pypi_0    pypi
blas                      1.0                         mkl
bleach                    1.5.0                    pypi_0    pypi
ca-certificates           2019.5.15                     1
certifi                   2018.8.24                py35_1
cffi                      1.11.5           py35h945400d_0
cuda90                    1.0                           0    pytorch
cx-freeze                 6.0                      pypi_0    pypi
cycler                    0.10.0           py35hcc71164_0
cython                    0.29.13                  pypi_0    pypi
decorator                 4.4.0                    pypi_0    pypi
dlib                      19.17.0                  pypi_0    pypi
entrypoints               0.3                      pypi_0    pypi
enum34                    1.1.6                    pypi_0    pypi
flake8                    3.7.8                    pypi_0    pypi
freetype                  2.8                  h51f8f2c_1
gast                      0.2.2                    pypi_0    pypi
grpcio                    1.23.0                   pypi_0    pypi
h5py                      2.9.0                    pypi_0    pypi
html5lib                  0.9999999                pypi_0    pypi
icc_rt                    2017.0.4             h97af966_0
icu                       58.2                 ha66f8fd_1
imageio                   2.5.0                    pypi_0    pypi
intel-openmp              2018.0.3                      0
jpeg                      9b                   hb83a4c4_2
keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.0                    pypi_0    pypi
kiwisolver                1.0.1            py35hc605aed_0
libopencv                 3.4.2                h20b85fd_0
libpng                    1.6.36               h2a8f88b_0
libprotobuf               3.6.0                h1a1b453_0
libtiff                   4.0.10               hb898794_2
markdown                  3.1.1                    pypi_0    pypi
matplotlib                2.2.2            py35h153e9ff_1
mccabe                    0.6.1                    pypi_0    pypi
mkl                       2018.0.3                      1
mkl_fft                   1.0.6            py35hdbbee80_0
mkl_random                1.0.1            py35h9258bd6_0
networkx                  2.3                      pypi_0    pypi
numpy                     1.15.2           py35ha559c80_0
numpy-base                1.15.2           py35h8128ebf_0
opencv-contrib-python     4.1.0.25                 pypi_0    pypi
openssl                   1.1.1c               he774522_1
pillow                    5.2.0                    pypi_0    pypi
pip                       10.0.1                   py35_0
protobuf                  3.9.1                    pypi_0    pypi
py-opencv                 3.4.2            py35hc319ecb_0
pycodestyle               2.5.0                    pypi_0    pypi
pycparser                 2.18             py35h15a15da_1
pyflakes                  2.1.1                    pypi_0    pypi
pyparsing                 2.2.0            py35hcabcaab_1
pyqt                      5.9.2            py35h1aa27d4_0
python                    3.5.5                h0c2934d_2
python-dateutil           2.7.3                    py35_0
pytorch                   0.4.0           py35_cuda90_cudnn7he774522_1  [cuda90]  pytorch
pytz                      2018.5                   py35_0
pywavelets                1.0.3                    pypi_0    pypi
qt                        5.9.7            vc14h73c81de_0
scikit-image              0.15.0                   pypi_0    pypi
scipy                     1.1.0            py35h4f6bf74_1
setuptools                39.2.0                   py35_0
sip                       4.19.8           py35h6538335_0
six                       1.11.0           py35hc1da2df_1
sqlite                    3.26.0               he774522_0
tensorboard               1.12.2                   pypi_0    pypi
tensorflow                1.12.0                   pypi_0    pypi
tensorflow-tensorboard    0.4.0                    pypi_0    pypi
termcolor                 1.1.0                    pypi_0    pypi
torchvision               0.1.8                    pypi_0    pypi
tornado                   5.0.2                    py35_0
vc                        14.1                 h0510ff6_3
vs2015_runtime            15.5.2                        3
werkzeug                  0.15.5                   pypi_0    pypi
wheel                     0.31.1                   py35_0
wincertstore              0.2              py35hfebbdb8_0
xz                        5.2.4                h2fa13f4_4
zlib                      1.2.11               h8395fce_2
zstd                      1.3.7                h508b16e_0

cx_Freeze打包程序有两种,第一种是直接cxfreeze xxx.py,第二种是构建setup.py
这里用的第二种,构建的setup.py来自于https://blog.csdn.net/m0_38052778/article/details/84581429

from cx_Freeze import setup, Executable
# come from https://blog.csdn.net/m0_38052778/article/details/84581429
# Dependencies are automatically detected, but it might need
# fine tuning.
import sys
import os.path

os.environ['TCL_LIBRARY'] = r'D:/Program/Work/Anaconda3/tcl/tcl8.6'
os.environ['TK_LIBRARY'] = r'D:/Program/Work/Anaconda3/tcl/tk8.6'
base = []

build_exe_options = dict(packages=['numpy', 'scipy', 'tensorflow', 'google'],
                         includes=['tensorflow'],
                         include_files=[r"D:/Program/Work/Anaconda3/DLLs/tcl86t.dll",
                                        r"D:/Program/Work/Anaconda3/DLLs/tk86t.dll"])

executables = [
    Executable('F:/B_software_3DFaceRec/PRNet/demo.py', base=base, targetName='demo.exe')
]

setup(name='demo',
      version='1.0',
      description='prnet',
      options=dict(build_exe=build_exe_options),
      executables=executables)

NOTE:这里的绝对路径请改成自己的

然后就执行命令,build是打包后的文件放置的地方

python setup.py build

但是遇到了好些问题,记录如下:

1、ModuleNotFoundError: No module named ‘scipy.spatial.ckdtree’
参考链接:https://stackoverflow.com/a/44061093
就把build/exe.win-amd64-3.5/lib/scipy/spatial/文件夹下面的cKDTree.cp35-win_amd64.pyd改成ckdtree.cp35-win_amd64.pyd

2、ModuleNotFoundError: No module named ‘google’
参考链接:1、https://blog.csdn.net/ibelieveican2015/article/details/79240729
2、https://blog.csdn.net/imliutao2/article/details/80413081
首先安装一个protobuf(来自参考链接1),如果有就不用装了

pip install protobuf

装完后在python下可以import到google,但是cx_Freeze打包出来的exe还是说没有google.protobuf,然后就需要在lib/site-packages/google文件夹下创建一个空的__init__.py(来自参考链接2)

3、RuntimeError: No suitable plugin registered for imread
参考链接:https://blog.csdn.net/ygfrancois/article/details/84556955
好像skimage.io和这些打包的方法总是存在些问题,google的时候发现pyinstaller也有这种问题
然后我就使用opencv来读取图像了,但是由于opencv的读出来的图像是BGR的,而skimage读出来的图像是RGB的,而且,我最后保留的obj文件的纹理也是RGB的,因此就修改一下opencv读出来的图像

image[:,:,[0,1,2]] = image[:,:,[2,1,0]]

mmp,把这个打包后分享给别人,然后在别人电脑里运行的时候,提示要绝对路径下的源码
cx_Freeze 打包 PRNet 为exe_第1张图片
然后我又试着用pyinstaller跑了一遍,结果,居然可以了,应该是上面改的某个东西生效了。
但是还有一些小问题,是在之前使用pyinstaller的时候遇到的。
运行命令:

pyinstaller -F demo.py

-F是生成单个exe,-D是生成文件夹
然后会遇到下面这个错误

RecursionError: maximum recursion depth exceeded

这是因为它在递归包的时候,超过了最大深度,然后就需要在它现在生成的demo.spec里添加

import sys
sys.setrecursionlimit(5000)

然后我之前遇到了一个问题,让在hiddenimports里添加pywt._extensions._cwt,如下所示

hiddenimports=["pywt","pywt._extensions._cwt"],

所以最后得到的demo.spec如下

# -*- mode: python ; coding: utf-8 -*-
import sys
sys.setrecursionlimit(5000)

block_cipher = None

a = Analysis(['demo.py'],
             pathex=['F:\\B_software_3DFaceRec\\PRNet'],
             binaries=[],
             datas=[],
             hiddenimports=["pywt","pywt._extensions._cwt"],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='demo',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )

然后运行这个demo.spec

pyinstaller -F demo.spec

会遇到下面这个问题

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xce in position 123: invalid continuation byte

然后参考https://blog.csdn.net/qq_34106574/article/details/82964782
先在控制台输入chcp 65001,再运行pyinstaller -F demo.spec

你可能感兴趣的:(项目)