cpvrlab/ImagePlay

Generate opencv code

schmk2 opened this issue · 2 comments

A new feature could generate opencv source code from any process graph.

Most opencv Python functions take a frame as their first parameter and return the transformed frame.
With the help of functools.reduce, functools.partial it's should be possible so that the json edges+steps get converted into one function call per edge, containing a list of steps, optionally wrapped by partial() and a composition with some functools magic wrapping frame passing.

In a first step Python code can end up as:

#!/usr/bin/python
pipe_edge1 = compose( webcam(),
                      partial( IPLGammaCorrection, gamma = 0.4 ),
                      partial( IPLCanny, highThreshold = 0.6, ... ),
                      frame )
pipe_edge1() # run!

And then based on an hypothetical IPL<->opencv JSON mapping + documentation, transformed as:

#!/usr/bin/python
pipe_edge1 = compose( ...
                      partial( cv2.pow, gamma = 0.4 ),
                      partial( cv2.Canny, threshold2 = 0.6, ... ),
                      frame )
pipe_edge1()

But that would be even better to do most of this directly from opencv python-binding code itself.
Inside https://github.com/opencv/opencv/blob/master/modules/python/bindings/CMakeLists.txt
it seems a pyopencv_signatures.json is generated. I couldn't get my hand on it nor build it (@skvark) but that could be and interesting file.

l00mi commented

Interesting, we always were thinking of generating C++ code. But we could absolutely (also) go with a python generator.