How Python bindings are generated?
1 - How is Python API of OpenCV (cv2 version) generated ? Is it created using SWIG ?
1.a) If it is SWIG created, where I can find the SWIG interface files?
2 - What programming skills and knowledge does one require to correct if any bugs or mistakes in the Python bindings?
answered 2013-02-05 10:29:58 -0600Vadim Pisarevsky
We do not use SWIG or any other standard wrapper generation tool. We did not find such tools that would produce satisfying results.
Instead, we use our own purely Python-based solution for parsing OpenCV headers (we do not comprehend all the C++ syntax, just enough to extract declarations of the functions, classes, methods, as well as enumerations constants).
Since C/C++ API does not conveniently map to Python API (e.g. in Python we can have multiple return values, in C just one), we put in C++ headers some helper macros to give parser some high-level information about certain classes or method parameters. For example, CV_EXPORTS_W_MAP in the class declaration is equivalent to CV_EXPORTS for C++ compiler, but it instructs the header parser that the class should be converted to a Python map (dict) (which is very convenient for structures representing algorithm parameters).
The parser is at opencv/modules/python/src2/hdr_parser.py. BTW, our java wrapper generator uses the same technology and the same script to extract the API.
After all the API is extracted, we use some more python code (opencv/modules/python/src2/gen2.py) to produce Python wrappers. We created this script incrementally and used the concept of clear crisp wrapper code. In other words, instead of creating "for compiler eyes only" mess, we generate wrappers as if they were manually written. They are very easy to read, and therefore, it was not difficult to debug the wrapper generator.
For 2.5.x we plan to do some refactoring of the code, but we will continue to use this technology and are very happy with it. Besides the nice warm feeling that we have the full control over the result, the generator is also super-fast and does not impose any extra dependencies.
If you want to participate, it's enough to know C/C++, Python and a bit of the Python C API.
Another strict requirement is that you should not suggest us to switch to another wrapper generator, Boost-Python or whatever :)
Asked: 2013-01-29 03:47:12 -0600
Seen: 383 times
Last updated: Feb 05