feat: G-API: Custom stream sources in Python (#27276) #27443
+272
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implemented:
A C++ proxy class PythonCustomStreamSource that implements the IStreamSource interface. This class acts as a bridge between G-API’s internal streaming engine and user-defined Python objects. Internally, it stores a reference to a Python object (PyObject*) and is responsible for: calling the Python object’s pull() method to retrieve the next frame, calling the descr_of() method to obtain the frame format description, acquiring and releasing the Python GIL as needed, converting the returned numpy.ndarray into cv::Mat, and handling any exceptions or conversion errors with proper diagnostics.
A Python-facing factory function, cv.gapi.wip.make_py_src(), which takes a Python object as an argument and wraps it into a cv::Ptr. Internally, this function constructs a PythonCustomStreamSource instance and passes the Python object to it. This design allows Python users to define any class that implements two methods: pull() and descr_of(). No subclassing or special decorators are required on the Python side. The user simply needs to implement the expected interface.
This design enables any Python class with
pull()
anddescr_of()
to act as a custom stream source, no decorators or inheritance needed.GSoC 2023 idea 15: https://github.com/opencv/opencv/wiki/GSoC_2023#idea-g-api-implement-custom-stream-sources-in-python
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.