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

问题描述

所以我有一个相当大的 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.

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

解决方法

问题可能是 cx_Freeze 没有完全包含 mean_date d_id 2021-03-28 x_122 2021-03-29 x_122 2021-03-30 x_122 2021-03-31 x_122

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

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','plugins') build_exe_options = {"packages": ["os"],"excludes": [],"include_files": [(plugins_source_path,plugins_target_path)]} ... ,请参阅 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"],"include_files": [(opencv_lib_src,opencv_lib_dst)]}