Merge pull request #18343 from TolyaTalamanov:at/support-return-tuple

[G-API] Support std::tuple for return type
pull/18365/head
Alexander Alekhin 4 years ago committed by GitHub
commit 7163781639
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      modules/gapi/include/opencv2/gapi/core.hpp
  2. 2
      modules/gapi/include/opencv2/gapi/gcomputation.hpp
  3. 42
      modules/gapi/misc/python/test/test_gapi_sample_pipelines.py
  4. 35
      modules/python/src2/cv2.cpp
  5. 2
      modules/python/test/test_copytomask.py

@ -1446,7 +1446,7 @@ All output matrices must be in @ref CV_8UC1.
@sa merge3, merge4
*/
GAPI_EXPORTS std::tuple<GMat, GMat, GMat,GMat> split4(const GMat& src);
GAPI_EXPORTS std::tuple<GMat, GMat, GMat> split3(const GMat& src);
GAPI_EXPORTS_W std::tuple<GMat, GMat, GMat> split3(const GMat& src);
/** @brief Applies a generic geometrical transformation to an image.

@ -172,7 +172,7 @@ public:
* @param in input GMat of the defined unary computation
* @param out output GMat of the defined unary computation
*/
GComputation(GMat in, GMat out); // Unary overload
GAPI_WRAP GComputation(GMat in, GMat out); // Unary overload
/**
* @brief Defines an unary (one input -- one output) computation

@ -0,0 +1,42 @@
#!/usr/bin/env python
import numpy as np
import cv2 as cv
from tests_common import NewOpenCVTests
# Plaidml is an optional backend
pkgs = [
cv.gapi.core.ocl.kernels(),
cv.gapi.core.cpu.kernels(),
cv.gapi.core.fluid.kernels()
# cv.gapi.core.plaidml.kernels()
]
class gapi_sample_pipelines(NewOpenCVTests):
# NB: This test check multiple outputs for operation
def test_mean_over_r(self):
sz = (100, 100, 3)
in_mat = np.random.randint(0, 100, sz).astype(np.uint8)
# # OpenCV
_, _, r_ch = cv.split(in_mat)
expected = cv.mean(r_ch)
# G-API
g_in = cv.GMat()
b, g, r = cv.gapi.split3(g_in)
g_out = cv.gapi.mean(r)
comp = cv.GComputation(g_in, g_out)
for pkg in pkgs:
actual = comp.apply(in_mat, args=cv.compile_args(pkg))
# Comparison
self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
if __name__ == '__main__':
NewOpenCVTests.bootstrap()

@ -1486,6 +1486,41 @@ template<typename _Tp> static inline PyObject* pyopencv_from_generic_vec(const s
return seq;
}
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type
convert_to_python_tuple(const std::tuple<Tp...>&, PyObject*) { }
template<std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I < sizeof...(Tp), void>::type
convert_to_python_tuple(const std::tuple<Tp...>& cpp_tuple, PyObject* py_tuple)
{
PyObject* item = pyopencv_from(std::get<I>(cpp_tuple));
if (!item)
return;
PyTuple_SET_ITEM(py_tuple, I, item);
convert_to_python_tuple<I + 1, Tp...>(cpp_tuple, py_tuple);
}
template<typename... Ts>
PyObject* pyopencv_from(const std::tuple<Ts...>& cpp_tuple)
{
size_t size = sizeof...(Ts);
PyObject* py_tuple = PyTuple_New(size);
convert_to_python_tuple(cpp_tuple, py_tuple);
size_t actual_size = PyTuple_Size(py_tuple);
if (actual_size < size)
{
Py_DECREF(py_tuple);
return NULL;
}
return py_tuple;
}
template<>
PyObject* pyopencv_from(const std::pair<int, double>& src)
{

@ -7,8 +7,8 @@ Test for copyto with mask
# Python 2/3 compatibility
from __future__ import print_function
import cv2 as cv
import numpy as np
import cv2 as cv
import sys
from tests_common import NewOpenCVTests

Loading…
Cancel
Save