Ask Your Question

How Python API is generated?

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

Abid Rahman K gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

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

Vadim Pisarevsky gravatar image

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

edit flag offensive delete link more


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

Abid Rahman K gravatar imageAbid 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 gravatar imageb_m ( 2013-03-04 10:19:38 -0600 )edit

i found the and tried to generate the python wrappers,but having issues. Please see the LINK

Gopi Krishnan gravatar imageGopi Krishnan ( 2015-04-01 10:56:28 -0600 )edit

Is there a discussion to where you can point me as to why you chose not to use any of the third party libraries like SWIG, Boost::Python, or Cython? I'm currently looking at wrapping a fairly large C++ code base and am trying to make an educated guess for which route to take. Thanks for any links.

AcidLeroy gravatar imageAcidLeroy ( 2015-04-03 10:09:03 -0600 )edit

Question Tools



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

Seen: 3,848 times

Last updated: Feb 05 '13