Skip to content
Advertisement

Unable to deploy python program with cx_freeze due to cv2

So I have a fairly large python program that I want to port to other machines (ubuntu 18.04) without having to install all the python packages and dependencies for each machine, I chose to use cx_Freeze for this and it seems to build the project fine into a single executable but the executable crashes when calling cv2.imshow. I managed to reproduce the error with this small snippet of code:

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

this is my cx_Freeze build script:

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")]
)

and this is the error I get:

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)

I also tried running the program with QT_DEBUG_PLUGINS=1 for a more detailed error output:

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.

Any help/suggestions would be greatly appreciated. I tried using pyInstaller, but it doesn’t manage to even build the project.

Advertisement

Answer

So thanks to jpeg from the other answer I managed to fix the issue by adding the following snippet of code to my builds script:

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)]}
 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement