Ask Your Question
2

How Python API is generated?

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

Abid Rahman K gravatar image Abid Rahman K flag of India
513 3 7 21
http://www.opencvpython.b...

How Python bindings are generated?

Hi,

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
8

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

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

link delete flag offensive edit

Comments

+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 -0500 )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: http://answers.opencv.org/question/8099/how-to-use-the-c-functions-of-opencv-from-python/ 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 -0500 )edit

Login/Signup to Answer

Question tools

Follow
3 followers

subscribe to rss feed

Stats

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

Seen: 928 times

Last updated: Feb 05 '13