Sunday, April 13, 2014

Solved: cx_Freeze and OpenCV, app is portable!

Just managed to port my python OpenCv face detection app, Behave, to another mac and run ok using cx_freeze.

The trick is to copy the /lib directory from the opencv installation, into the application directory, then tell cx_freeze to use it.

In the base directory of your app:
mkdir lib
cp /usr/opencv-64/lib .
Change /usr/opencv-64 for the path to where opencv has been installed.

And then modify your setup.py:
from cx_Freeze import setup, Executable

setup(  name = "behave",
        version = "0.1",
        description = "Behave, face detection to help you",
        options = {'build_exe':
                    {'includes': ['numpy'],
                        'include_files': ['cascades/', 'lib/']}},
        executables = [Executable("capturebasic.py")])
The las entry in 'include_files' is the bit that tells OpenCv to bundle the whole folder with the app.
I'm sure there must be a more elegant way of doing this, but this one works: happy days!


3 comments:

  1. Does the executable work also if OpenCV isn't installed in the system? Is opencv bundled inside the exe file? Is the file size big or acceptable?

    ReplyDelete
  2. Does the executable work also if OpenCV isn't installed in the system? Is opencv bundled inside the exe file? Is the file size big or acceptable?

    ReplyDelete
  3. Thanks a lot,

    Now I can also create Mac distributable from Python3.6 + numpy + OpenCV3.2 + cx_Freeze.
    Don’t have an elegant method such as entering "cv" or "opens" keywords inside setup.py.
    However, build process can be improved slightly by copying only once the opencv .dylib files into the opencv package source directory.

    Running python setup.py build on my Mac Sierra,

    error: [Errno 2] No such file or directory: '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/libopencv_reg.3.2.dylib'


    Following the error, all libopencv*.dylib files were copied only once into
    /usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/
    directory and build was successful.

    Didn’t try it on another Mac yet but file structure of the build seems as a standalone dir.

    Size of the build target is 80.2 Mbytes for 829 files.

    Many thanks again,

    Itai

    ReplyDelete