由于cv2,无法使用cx\U冻结部署python程序

2024-10-01 04:59:21 发布

您现在位置: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,但它甚至无法构建项目


Tags: testbuildhomelinuxlibpluginsqtcv2
2条回答

因此,多亏了另一个答案中的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)]}
 

问题可能是cv2/qt/plugins没有完全包含在cx\u 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_Freezedocumentation

相关问题 更多 >