由于 cv2,无法使用 cx_freeze 部署 python 程序

Unable to deploy python program with cx_freeze due to cv2

所以我有一个相当大的 python 程序,我想移植到其他机器 (ubuntu 18.04),而不必为每台机器安装所有 python 包和依赖项,我为此选择使用 cx_Freeze,它似乎可以将项目很好地构建到单个可执行文件中,但可执行文件在调用 cv2.imshow 时崩溃。我设法用这一小段代码重现了错误:

import numpy as np
import cv2
img = cv2.imread('monke.jpg',0)
cv2.imshow("img", img)

这是我的 cx_Freeze 构建脚本:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": []}

# GUI applications require a different base on Windows (the default is for
# a console application).

setup(
    name = "test",
    version = "0.1",
    description = "My GUI application!",
    options = {"build_exe": build_exe_options},
    executables = [Executable("cv2_test.py")]
)

这是我得到的错误:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

Aborted (core dumped)

我还尝试了 运行 带有 QT_DEBUG_PLUGINS=1 的程序以获得更详细的错误输出:

QFactoryLoader::QFactoryLoader() checking directory path "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins" ...
QFactoryLoader::QFactoryLoader() checking directory path "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7" ...
QFactoryLoader::QFactoryLoader() looking at "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/cv2_test"
"Failed to extract plugin meta data from '/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/cv2_test'" 
         not a plugin
QFactoryLoader::QFactoryLoader() looking at "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/monke.jpg"
QElfParser: '/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/monke.jpg' is not an ELF object
"'/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/monke.jpg' is not an ELF object" 
         not a plugin
QFactoryLoader::QFactoryLoader() checking directory path "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms" ...
QFactoryLoader::QFactoryLoader() looking at "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms/libqxcb.so"
Found metadata in lib /home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms/libqxcb.so, metadata=
{
    "IID": "org.qt-project.Qt.QPA.QPlatformIntegrationFactoryInterface.5.3",
    "MetaData": {
        "Keys": [
            "xcb"
        ]
    },
    "archreq": 0,
    "className": "QXcbIntegrationPlugin",
    "debug": false,
    "version": 331520
}


Got keys from plugin meta data ("xcb")
QFactoryLoader::QFactoryLoader() checking directory path "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/platforms" ...
Cannot load library /home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms/libqxcb.so: (libQt5XcbQpa-70670cdb.so.5.15.0: cannot open shared object file: No such file or directory)
QLibraryPrivate::loadPlugin failed on "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms/libqxcb.so" : "Cannot load library /home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins/platforms/libqxcb.so: (libQt5XcbQpa-70670cdb.so.5.15.0: cannot open shared object file: No such file or directory)"
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/vaki/Desktop/cv2_test/build/exe.linux-x86_64-3.7/lib/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

任何 help/suggestions 将不胜感激。我尝试使用 pyInstaller,但它甚至无法构建项目。

问题可能是 cv2/qt/plugins 没有完全包含在 cx_Freeze 中。

尝试按如下方式修改构建脚本的开头:

import sys
from cx_Freeze import setup, Executable
import os
import cv2

# Dependencies are automatically detected, but it might need fine tuning.
plugins_source_path = os.path.join(os.path.dirname(cv2.__file__), 'qt', 'plugins')
plugins_target_path = os.path.join('lib', 'cv2', 'qt', 'plugins')
build_exe_options = {"packages": ["os"],
                     "excludes": [],
                     "include_files": [(plugins_source_path, plugins_target_path)]}
...

(未测试,您可能需要微调)

这应该告诉 cx_Freeze 在正确的位置包含整个文件夹 cv2/qt/plugins,请参阅 cx_Freeze documentation

感谢其他答案中的 jpeg,我通过将以下代码片段添加到我的构建脚本中设法解决了这个问题:

opencv_lib_src = os.path.join(os.path.dirname(cv2.__file__), '..', 'opencv_python.libs')
opencv_lib_dst = os.path.join('lib', 'opencv_python.libs')
build_exe_options = {"packages": ["os"], 
                     "excludes": [],
                     "include_files": [(opencv_lib_src , opencv_lib_dst)]}