Ask Your Question

How Python API is generated?

asked 2013-01-29 03:47:12 -0600

Abid Rahman K gravatar image Abid Rahman K flag of India
533 3 8 22

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?

delete close flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-02-05 10:29:58 -0600

Vadim Pisarevsky gravatar image Vadim Pisarevsky
241 2 4 3

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/ 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/ 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 :)

link delete flag offensive edit


+1 - Thanks for the reply. I won't ask for boost-python. I am happy with present API :)

Abid Rahman K ( 2013-02-05 10:56:56 -0600 )edit

I'm looking into using some functions of the C++ API, and generating the proper Python bindings seems to be the best solution so far. Here's what I wanna do: I followed your instructions and got a few header files. I don't know what to do next. Can you elaborate more?

b_m ( 2013-03-04 10:19:38 -0600 )edit

Login/Signup to Answer

Question tools


subscribe to rss feed


Asked: 2013-01-29 03:47:12 -0600

Seen: 1,062 times

Last updated: Feb 05 '13