For image processing I tend to use python to speed up my workflow. But what if there is missing functionality and you only find a library written in c++?
For this special case, one could write a c++ extension and import it in python. The extension relies on openCV which would be bundled into the extension. But python can already use openCV on its own. So I would end up with 2 versions of openCV in my programm. Is there a way around this?
I am not sure I understand what you mean. So you have a third library with C++ that has some functionality which is lacking in OpenCV. You then want to talk to this library in Python together with the Python API from OpenCV. The extension you linked is just a way to include python in a C++ program and does not necessarly need OpenCV. Does the third library use OpenCV as well?
In general you can say:
If you do not modify OpenCV you do not need two versions. You install one and just have to make sure that another library that uses OpenCV as well links to the version you have used in the Python API when you build it.
If you do modify OpenCV you should build it yourself with your changes and then use the Python API from this self build OpenCV to be used in your applications.
Related
From the internet I have learnt that Python does not have any standard modules to access the webcam. In order to capture a photo using the WebCam, we need to use an open source module called opencv (cv2 for python) which is written in C++. My question is why does python interpreter not throw an error when a piece of C++ code is used inside a Python code? How can it interpret something which is not python?
The python functions act as wrappers, meaning they just provide a python interface but behind the scenes they just execute the C++ code. So all you need is some code that lets you interpret the data that is sent from C++ and find the python equivalent like https://docs.python.org/3/library/ctypes.html and you're done. Though in case of OpenCV people have already done that for you so you can use the python stuff as if it were written in python.
I have a use case where I want to deploy an application on end-user machines that don't necessarily have Python installed. Most of the application is written in Python, but the entry point is a legacy C++ application. This is why using tools like PyInstaller are not an option (although I'm certainly not an expert on this, so I could be wrong).
I am using Cython to embed the Python interpreter into the application. Everything is going more or less fine, but my Python code uses NumPy, which means there is still an external dependency. Ideally, I'd like to have a single binary that includes NumPy, as well as the Python interpreter, and my application code. Is it possible, using Cython or some other tool, to compile NumPy (or some portions of it that I am using) to a library that I can then link to my final executable binary? Or is it possible to somehow link to a wheel (or egg) containing the NumPy binary???
I have a .cpp and .h source file pair which is a manager (I guess a wrapper also) for a c++ library I have made. I want to let people use this manager to work with my library in python. I have heard about several different ways to wrap this library into python like cython and boost.python but Im having trouble with understanding the process.
If I want to make this manager usable in python, do I need to wrap it in a different way for each version of python? (2.7 vs 3.4) Do I also need to wrap it in a different way for each operating system for each version? So 2.7/3.4 for Windows vs 2.7/3.4 for Linux?
Concerning your confusion about the process, just follow any tutorial for any of the wrapper libs you found or where suggested in the comments.
If I want to make this manager usable in python, do I need to wrap it in a different way for each version of python? (2.7 vs 3.4)
Yes. You might be able to load binary modules compiled for Python 3.4 into Python 3.5, but it's unlikely to work across major versions.
Do I also need to wrap it in a different way for each operating system for each version?
Yes. Just as you need to compile your C++ code for different operating systems (and possibly versions) and CPU architectures, Python modules are not different. However, the "wrap it in a different way" just means "compile for the target environment".
I can't seem to find a way to create a standalone package for image recognition. I have a project I'm writing in python, and I found a way to do what I need using OpenCV, but I can't find a way to import the library into my project unless it is installed at the system level on Ubuntu. In other words, I can't seem to plop the build folder into my project after building the OpenCV library. And I can't find the equivalent of cv2.matchTemplate() in PIL or Pillow. So really there are two questions here.
1) How can I attach the build folder to my project, in order to avoid installing the OpenCV at the system level.
2) Is there an equivalent of cv2.matchTemplate() in PIL or Pillow that I can't seem to find?
Thanks.
You need to:
Download OpenCV
Use CMake to tell it to compile statically and to tell it to compile the Python module
Compile, and install into a directory you want.
Find in that directory the file under a directory called python, called cv2.so
Distribute that file with your Python code.
Now that I told you how to do it, let me tell you why your approach isn't a very good idea:
If the version of Python changes, you need to recompile (the so file) and redistribute your entire application
If the version of OpenCV changes you will need to recompile (the so file) and redistribute your entire application
You don't control what version of Python your users have
There can be important subtleties in version of libjpg, libtiff, zlib and others that could prevent your application from working, all outside your control.
You are converting a multi-platform application into a platform specific solution.
What's the appropriate way of constructing a Python package via disutils when that Python package relies on a large system library?
I found this similar question, but it refers to an installable Python app, not a generic package.
I've written a package that relies on OpenCV. I'm only concerned with supporting Linux distros, but most distros either don't provide OpenCV or provide a version that's too old to use. Unfortunately, OpenCV is to large and cumbersome (and depends on several other system libraries) to include in the package and compile during the build step.
My current approach is to simply do nothing special in my setup.py and just import its Python modules in a try/except, showing a detailed error message if the import fails. Is there a better way?
You could use zc.buildout: http://www.buildout.org/
You should be able to extend the buildout config for your project from this one: https://github.com/cpsaltis/opencv-buildout