How to include opencv as local import

Hi,

I’m trying to use opencv within a blender plugin, so I need to import the opencv .pyd locally so as to not force users to install via pip.

I managed to get the standard opencv library working in blender:


after placing the windows installation local to my plugin directory and commenting out

#if hasattr(sys, 'OpenCV_LOADER'):
     #print(sys.path)
        #raise ImportError('ERROR: recursion is detected during loading of "cv2" binary extensions. Check OpenCV installation.')

in opencv\build\python\cv2\ init .py
This seemed to be flagging a recursion error since opencv was already installed via pip.

Unfortunately, the function I require (SIFT) is only included in opencv-python-contrib, and so I then went about trying to get this build of the opencv .pyd working in blender.

Here I had more problems, it seems there was no pre-built version of opencv-python-contrib as there was the standard opencv from sourceforge. I tried using the .pyd file that pip places in the site-packages folder though this gave various errors when trying to work into blender.

Why is the pre-built windows version of opencv only 6MB when the version pip installation is 60MB?

Secondly, why does the opencv init.py file have the line import .cv2? This seems to be causing the recursion errors, in that opencv was trying to import itself.

I was getting errors such as

ModuleNotFoundError: No module named 'cv2.cv2'

and also errors regarding the recursion issue that the prior commented out check guards for. I believe it relates somewhat to this:

if DEBUG: print("Relink everything from native cv2 module to cv2 package")

    py_module = sys.modules.pop("cv2")

    native_module = importlib.import_module("cv2")

I’d really appreciate if someone could explain how exactly the opencv init.py file works and why its so hard to just import the opencv .pyd file without all these issues from it trying to include itself.

Thanks

from 4.4 on, this is no more so.
the patent expired, and SIFT was moved back into the main opencv.
so, you dont need the contrib modules for SIFT, and to get one use

sift = cv2.SIFT_create() # no more .xfeatures2d

the pip version is statically linked, the pre-built one will need the opencv dlls at runtime

Thanks for the reply, you’re right in that I don’t need the contrib version for SIFT now… I got my feature detection working in blender for a short while before it decided to throw a hissy fit for no reason…

I’m now getting the error

ImportError: DLL load failed while importing cv2: The specified module could not be found.

on line

import cv2

It was working perfectly for a minute but it seems now even the pre-built version is having issues.

Would really appreciate help on getting a stable local import working since all the threads I’ve found just say run pip install x, which is not what I need.

Thanks

Update, the error was caused from me moving the cv2 folder from opencv/build/python straight into my plugin directory since I thought it needed only this built cv2 folder… Apparently this references dlls that are above this folder (gross).

Hi I’m about to start on this journey myself.

It’s not safe to use pip install as you found, because often the site-packages isn’t writeable if the user has installed blender via the exe (rather than using the portable zip version). Also using pip to install to another location isn’t good either, because blender works in it’s own python environment, so it’s likely pip will install to a location out of blender’s scope, leading to the import error.

Probably the safest option is to bundle opencv with the addon? What is the minimum I need to include? I’m just interested in using the DNN modules such as dnn_superres from the contrib. Is that OK by the way, I couldn’t find any licence information indicating that it’s OK for me to ship he contrib modules/dlls as part of a paid product.