1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Merge pull request #19322 from TolyaTalamanov:at/python-callbacks

[G-API] Introduce cv.gin/cv.descr_of for python

* Implement cv.gin/cv.descr_of

* Fix macos build

* Fix gcomputation tests

* Add test

* Add using to a void exceeded length for windows build

* Add using to a void exceeded length for windows build

* Fix comments to review

* Fix comments to review

* Update from latest master

* Avoid graph compilation to obtain in/out info

* Fix indentation

* Fix comments to review

* Avoid using default in switches

* Post output meta for giebackend
This commit is contained in:
Anatoliy Talamanov
2021-03-01 18:52:11 +03:00
committed by GitHub
parent 7bcb51eded
commit eb82ba36a3
26 changed files with 825 additions and 220 deletions
+20 -9
View File
@@ -91,8 +91,6 @@ namespace opencv_test
}
};
// NB: Check an apply specifically designed to be called from Python,
// but can also be used from C++
struct GComputationPythonApplyTest: public ::testing::Test
{
cv::Size sz;
@@ -103,22 +101,28 @@ namespace opencv_test
GComputationPythonApplyTest() : sz(cv::Size(300,300)), type(CV_8UC1),
in_mat1(sz, type), in_mat2(sz, type), out_mat_ocv(sz, type),
m_c([&](){
cv::GMat in1, in2;
cv::GMat out = in1 + in2;
return cv::GComputation(cv::GIn(in1, in2), cv::GOut(out));
})
cv::GMat in1, in2;
cv::GMat out = in1 + in2;
return cv::GComputation(cv::GIn(in1, in2), cv::GOut(out));
})
{
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
out_mat_ocv = in_mat1 + in_mat2;
}
};
}
TEST_F(GComputationPythonApplyTest, WithoutSerialization)
{
auto output = m_c.apply(cv::gin(in_mat1, in_mat2));
auto output = m_c.apply(cv::detail::ExtractArgsCallback{[this](const cv::GTypesInfo& info)
{
GAPI_Assert(info[0].shape == cv::GShape::GMAT);
GAPI_Assert(info[1].shape == cv::GShape::GMAT);
return cv::GRunArgs{in_mat1, in_mat2};
}
});
EXPECT_EQ(1u, output.size());
const auto& out_mat_gapi = cv::util::get<cv::Mat>(output[0]);
@@ -130,7 +134,14 @@ namespace opencv_test
auto p = cv::gapi::serialize(m_c);
auto c = cv::gapi::deserialize<cv::GComputation>(p);
auto output = c.apply(cv::gin(in_mat1, in_mat2));
auto output = c.apply(cv::detail::ExtractArgsCallback{[this](const cv::GTypesInfo& info)
{
GAPI_Assert(info[0].shape == cv::GShape::GMAT);
GAPI_Assert(info[1].shape == cv::GShape::GMAT);
return cv::GRunArgs{in_mat1, in_mat2};
}
});
EXPECT_EQ(1u, output.size());
const auto& out_mat_gapi = cv::util::get<cv::Mat>(output[0]);
@@ -1282,7 +1282,7 @@ TEST(Streaming, Python_Pull_Overload)
cv::Mat in_mat(sz, CV_8UC3);
cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar(255));
auto ccomp = c.compileStreaming(cv::descr_of(in_mat));
auto ccomp = c.compileStreaming();
EXPECT_TRUE(ccomp);
EXPECT_FALSE(ccomp.running());
@@ -1895,4 +1895,54 @@ TEST(GAPI_Streaming, AccessBGRFromNV12Frame)
}
}
TEST(GAPI_Streaming, TestPythonAPI)
{
cv::Size sz(200, 200);
cv::Mat in_mat(sz, CV_8UC3);
cv::randu(in_mat, cv::Scalar::all(0), cv::Scalar(255));
const auto crop_rc = cv::Rect(13, 75, 100, 100);
// OpenCV reference image
cv::Mat ocv_mat;
{
ocv_mat = in_mat(crop_rc);
}
cv::GMat in;
auto roi = cv::gapi::crop(in, crop_rc);
cv::GComputation comp(cv::GIn(in), cv::GOut(roi));
// NB: Used by python bridge
auto cc = comp.compileStreaming(cv::detail::ExtractMetaCallback{[&](const cv::GTypesInfo& info)
{
GAPI_Assert(info.size() == 1u);
GAPI_Assert(info[0].shape == cv::GShape::GMAT);
return cv::GMetaArgs{cv::GMetaArg{cv::descr_of(in_mat)}};
}});
// NB: Used by python bridge
cc.setSource(cv::detail::ExtractArgsCallback{[&](const cv::GTypesInfo& info)
{
GAPI_Assert(info.size() == 1u);
GAPI_Assert(info[0].shape == cv::GShape::GMAT);
return cv::GRunArgs{in_mat};
}});
cc.start();
bool is_over = false;
cv::GRunArgs out_args;
// NB: Used by python bridge
std::tie(is_over, out_args) = cc.pull();
ASSERT_EQ(1u, out_args.size());
ASSERT_TRUE(cv::util::holds_alternative<cv::Mat>(out_args[0]));
EXPECT_EQ(0, cvtest::norm(ocv_mat, cv::util::get<cv::Mat>(out_args[0]), NORM_INF));
EXPECT_TRUE(is_over);
cc.stop();
}
} // namespace opencv_test