Search code examples
pyinstalleropencvnvidia-jetsonpython

Pyinstaller doesnt package cv2 in jetson


I have a software which I packaged in x86 processors using pyinstaller and it packages all libraries including cv2, however, when I try to package the same software in Jetson TX2, it doesn't package cv2 and throws error on executing the binary file:

OpenCV loader: missing configuration file: ['config.py']

The reason is cv2 comes preinstalled in TX2 at a different location (/usr/lib/python3.6/dist-packages). However, rest of the libraries which we self-installed are in (/home/mnauf/.local/lib/python3.6/site-packages) and maybe this is why pyinstaller fails to package it.

The pyinstaller tries to find cv2 at /home/mnauf/.local/lib/python3.6/site-packages and doesn't find there and correspondingly doesn't package, however cv2 imports fine if you do it with python. The reason why cv2 works fine with python is that I suppose python first tries to find a library in /home/mnauf/.local/lib/python3.6/site-packages and if unsuccessful, finds in /usr/lib/python3.6/dist-packages.

To solve the packaging problem, I tried the following methodologies and the errors discussed below all come when executing the binary file and don't come at the time of packaging:

  1. Copying cv2 from /usr/lib/python3.6/dist-packages to /home/mnauf/.local/lib/python3.6/site-packages. It gives:

ImportError: ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.

  1. I try to copy cv2 directory from /usr/lib/python3.6/dist-packages to dist/main folder created by pyinstaller after packaging but I get the same Import error.

  2. Adding the cv2 directory path as a data file in main.spec also only copies the folder to dist/main and hence gives the same Import error.

  3. Adding the cv2.cpython-36m-aarch64-linux-gnu.so path only as a data file in main.spec gives Opencv loader error.

  4. Adding the cv2 directory as a binary file path in main.spec gives the Import error.

  5. Adding the cv2.cpython-36m-aarch64-linux-gnu.so path only as a binary file in main.spec gives the Opencv loader error.

Please help me with packing cv2. Thanks


Solution

  • I copied /usr/lib/python3.6/dist-packages/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so to /home/mnauf/.local/lib/python3.6/site-packages/ and then imported cv2 from python and checked its location where is it importing from by doing this:

    import cv2
    cv2.__file__
    

    and it returned me the path I wanted i.e /home/mnauf/.local/lib/python3.6/site-packages/. Once confident that cv2 is indeed using the directory I want, I ran pyinstaller and did package the cv2 dependency.

    Previously, I had copied the entire folder before creating its executables. This time I only copied the .so file, before creating executables and it worked.

    And I think "before creating executables" is also the trick. You can't just copy the .so file to dist/main and expect it to work. Also, we concluded that giving .so file path as data file or binary file in main.spec doesn't work.