-
Notifications
You must be signed in to change notification settings - Fork 896
Include headers to support binary linking? #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks! I started this project because I wanted to make the installation as easy as possible and I'm happy to hear that I have reached that target. I haven't tried that, it might be possible. Trying it shouldn't break anything so you may go ahead :) |
TL;DR: it's definitely possible, but it sucks and would need a bunch of integration work + testing I played with this a bunch on Linux today, here's what I've found so far:
It's really surprising to me that nobody has tried to do this sort of thing before, but it seems that people don't mix OpenCV python and OpenCV c++, except as a custom exe. I'm undecided if I'm giving up yet, but I welcome any input from others who may have ideas. |
What about passing |
There's no gain here, and you're tryng to use a Python module for something that it's not designed to -- that's why.
|
@native-api I would like to use a library that is written using OpenCV/C++ from python. Not duplicating a ton of carefully written C++ code seems like a huge gain to me. I agree that python's packaging system isn't designed to do this, and that accomplishing my goal using opencv-python would be tricky (and perhaps not possible). My target audience are high school students primarily using Windows. If I could ship a wheel that bundled/wrapped my OpenCV/C++ dependency that depended on opencv-python, that would be fantastic and significantly lower the bar for users (particularly on Windows) to use such a library. The dependency I'm using is getting Windows support this year, so I'll probably dig into this yet again in the coming months. |
This project ships There are other package managers that work in Windows and can ship C libraries separately -- e.g. Chocolatey and Anaconda -- and already have packages for Python and OpenCV. To achieve that, they are forced to put additional restrictions to avoid dependency hell that Python's ecosystem doesn't -- like same compilers and compiler options for everyone, only a single instance of a library per environment and/or strict file naming and placements conventions and little interoperability with other software installed on the system. |
Yes, I'm aware of all of that. I'd rather use stock python + wheels, I think it's a much simpler proposition from a novice user perspective (though certainly more difficult from a packager perspective). It is possible to build cv2.so/pyd to use shared libraries (perhaps you didn't read above?). And yes, as also mentioned above, it is likely that there are some things that would need to be patched upstream to make this work. Your input is welcome if you have other ideas on how one might be able to get this to work. |
|
Historically |
Just tried it on my mac:
|
Looks like someone already filed a bug for creating a capsule: opencv/opencv#8872 |
Haven't done anything in particular towards this, but recently we've done something similar that is relevant. Our new robot libraries now wrap existing C++ libraries, consisting of lots of different .so/.dll/.dylib files. Our new build system (https://github.com/robotpy/robotpy-build) generates python wrappers for the C++ libraries, embeds the shared library version of the C++ library in the python package, adds include files, and then autogens a snippet that looks like this:
As long as that snippet is imported before the python bindings are imported, the library symbols are resolved and everything works great. We even are shipping wheels that depend on libraries shipped in other wheels. The only exception to this is on OSX, where we have to fix up the rpath to include a site-packages relative This technique could be adapted for opencv-python instead of using statically linked libraries. However, to be truly useful, would really want opencv/opencv#8872 to be resolved (as mentioned above). |
Hi, I found this issue because I am interested in the same thing, it seems silly that you would include OpenCV two times. From my experience, PyTorch is doing something similar, there is an extension that links the core shared libraries, bundled in the Python package. The package also includes C++ headers and some python functions to locate them. You can then compile a PyTorch C++ extension and link it with torch C++ libraries. In Python code you have to first load torch Python C++ module that loads shared libraries and you can also load your extension. Would something like that work for OpenCV as well? |
I also really want to get this to work, I have a Python project that uses Python opencv but also has Python binds for a C++ library that links with opencv. |
FWIW, I have some (arm only, for my specific platform) wheels that allow this sort of thing at https://www.tortall.net/~robotpy/wheels/2022/roborio/ (see robotpy-opencv, robotpy-opencv-core, robotpy-opencv-core-dev, which are all used by robotpy-cscore). They are built at https://github.com/robotpy/roborio-opencv via github actions. The hard part, as stated above, is getting the library and include paths correct. I did it in setup.py (https://github.com/robotpy/robotpy-cscore/blob/7afe6afe5607c27fc1a563c44e0b25278b39b2d7/setup.py#L150) for this specific usage. However, a general purpose build tool would be better at setting all of this up (and in fact is what my robotpy-build tool does for my other packages). So, this is definitely possible. Someone just needs time to do it -- though a deeper integration of cmake or something with python packages might be the right place to do all of this. Either way, I don't have that time. |
pkgconf-pypi is a useful way to publish the information needed by other projects to find the headers and libraries. I think anyone tackling this problem will want to publish a .pc file as part of it. |
First off, thanks for this project, it saves a ton of time and I'm really happy that it exists. It makes it a lot easier to get OpenCV installed for the high school students I work with.
I'd like to take a C++ library that uses OpenCV, wrap it with cython, and link it to the OpenCV bits in the binary provided by the installed wheel. This would allow users to just install the wheel of the other library + the opencv-python wheel, as opposed to having to compile their own version.
Has anyone tried this yet? A cursory look at the linux .so files indicate that it exports all of the normal OpenCV symbols, so in theory if you just include the right headers and link to it then it should just work (though maybe not for Windows)... I think that one would add an additional cv2 module (
cv2.distutils
?) that could provide a list of includes/libraries, similar to how numpy works.If there's no opposition to this, I might try this out in a week or three to see how it goes.
The text was updated successfully, but these errors were encountered: