feat: 切换后端至PaddleOCR-NCNN,切换工程为CMake

1.项目后端整体迁移至PaddleOCR-NCNN算法,已通过基本的兼容性测试
2.工程改为使用CMake组织,后续为了更好地兼容第三方库,不再提供QMake工程
3.重整权利声明文件,重整代码工程,确保最小化侵权风险

Log: 切换后端至PaddleOCR-NCNN,切换工程为CMake
Change-Id: I4d5d2c5d37505a4a24b389b1a4c5d12f17bfa38c
This commit is contained in:
wangzhengyang
2022-05-10 09:54:44 +08:00
parent ecdd171c6f
commit 718c41634f
10018 changed files with 3593797 additions and 186748 deletions

View File

@ -0,0 +1,170 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/rmat.hpp>
#include "rmat_test_common.hpp"
#include <opencv2/gapi/fluid/imgproc.hpp>
#include <opencv2/gapi/cpu/imgproc.hpp>
namespace opencv_test
{
// This test set takes RMat type as a template parameter and launces simple
// blur(isl1) -> blur(isl2) computation passing RMat as input, as output
// and both input and output
template<typename RMatAdapterT>
struct RMatIntTestBase {
cv::Mat in_mat;
cv::Mat out_mat;
cv::Mat out_mat_ref;
cv::GComputation comp;
bool inCallbackCalled;
bool outCallbackCalled;
static constexpr int w = 8;
static constexpr int h = 8;
RMatIntTestBase()
: in_mat(h, w, CV_8UC1)
, out_mat(h, w, CV_8UC1)
, out_mat_ref(h, w, CV_8UC1)
, comp([](){
cv::GMat in;
auto tmp = cv::gapi::blur(in, {3,3});
auto out = cv::gapi::blur(tmp, {3,3});
cv::gapi::island("test", cv::GIn(in), cv::GOut(tmp));
return cv::GComputation(in, out);
})
, inCallbackCalled(false)
, outCallbackCalled(false) {
cv::randu(in_mat, cv::Scalar::all(127), cv::Scalar::all(40));
}
void check() {
comp.apply(in_mat, out_mat_ref);
EXPECT_EQ(0, cvtest::norm(out_mat_ref, out_mat, NORM_INF));
}
RMat createRMat(cv::Mat& mat, bool& callbackCalled) {
return {cv::make_rmat<RMatAdapterT>(mat, callbackCalled)};
}
};
template<typename RMatAdapterT>
struct RMatIntTest : public RMatIntTestBase<RMatAdapterT>
{
template<typename In, typename Out>
void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) {
for (int i = 0; i < 2; i++) {
EXPECT_FALSE(this->inCallbackCalled);
EXPECT_FALSE(this->outCallbackCalled);
auto compile_args_copy = compile_args;
this->comp.apply(cv::gin(in), cv::gout(out), std::move(compile_args_copy));
EXPECT_FALSE(this->inCallbackCalled);
if (std::is_same<RMat,Out>::value) {
EXPECT_TRUE(this->outCallbackCalled);
} else {
EXPECT_FALSE(this->outCallbackCalled);
}
this->outCallbackCalled = false;
}
this->check();
}
};
template<typename RMatAdapterT>
struct RMatIntTestStreaming : public RMatIntTestBase<RMatAdapterT>
{
template <typename M>
cv::GMatDesc getDesc(const M& m) { return cv::descr_of(m); }
void checkOutput(const cv::Mat&) { this->check(); }
void checkOutput(const RMat& rm) {
auto view = rm.access(RMat::Access::R);
this->out_mat = cv::Mat(view.size(), view.type(), view.ptr());
this->check();
}
template<typename In, typename Out>
void run(const In& in, Out& out, cv::GCompileArgs&& compile_args) {
auto sc = this->comp.compileStreaming(getDesc(in), std::move(compile_args));
sc.setSource(cv::gin(in));
sc.start();
std::size_t frame = 0u;
constexpr std::size_t num_frames = 10u;
EXPECT_FALSE(this->inCallbackCalled);
EXPECT_FALSE(this->outCallbackCalled);
while (sc.pull(cv::gout(out)) && frame < num_frames) {
frame++;
this->checkOutput(out);
EXPECT_FALSE(this->inCallbackCalled);
EXPECT_FALSE(this->outCallbackCalled);
}
EXPECT_EQ(num_frames, frame);
}
};
struct OcvKernels {
cv::gapi::GKernelPackage kernels() { return cv::gapi::imgproc::cpu::kernels(); }
};
struct FluidKernels {
cv::gapi::GKernelPackage kernels() { return cv::gapi::imgproc::fluid::kernels(); }
};
struct RMatIntTestCpuRef : public
RMatIntTest<RMatAdapterRef>, OcvKernels {};
struct RMatIntTestCpuCopy : public
RMatIntTest<RMatAdapterCopy>, OcvKernels {};
struct RMatIntTestCpuRefStreaming : public
RMatIntTestStreaming<RMatAdapterRef>, OcvKernels {};
struct RMatIntTestCpuCopyStreaming : public
RMatIntTestStreaming<RMatAdapterCopy>, OcvKernels {};
struct RMatIntTestCpuRefFluid : public
RMatIntTest<RMatAdapterRef>, FluidKernels {};
struct RMatIntTestCpuCopyFluid : public
RMatIntTest<RMatAdapterCopy>, FluidKernels {};
struct RMatIntTestCpuRefStreamingFluid : public
RMatIntTestStreaming<RMatAdapterRef>, FluidKernels {};
struct RMatIntTestCpuCopyStreamingFluid : public
RMatIntTestStreaming<RMatAdapterCopy>, FluidKernels {};
template<typename T>
struct RMatIntTypedTest : public ::testing::Test, public T {};
using RMatIntTestTypes = ::testing::Types< RMatIntTestCpuRef
, RMatIntTestCpuCopy
, RMatIntTestCpuRefStreaming
, RMatIntTestCpuCopyStreaming
, RMatIntTestCpuRefFluid
, RMatIntTestCpuCopyFluid
, RMatIntTestCpuRefStreamingFluid
, RMatIntTestCpuCopyStreamingFluid
>;
TYPED_TEST_CASE(RMatIntTypedTest, RMatIntTestTypes);
TYPED_TEST(RMatIntTypedTest, In) {
auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled);
this->run(in_rmat, this->out_mat, cv::compile_args(this->kernels()));
}
TYPED_TEST(RMatIntTypedTest, Out) {
auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled);
this->run(this->in_mat, out_rmat, cv::compile_args(this->kernels()));
}
TYPED_TEST(RMatIntTypedTest, InOut) {
auto in_rmat = this->createRMat(this->in_mat, this->inCallbackCalled);
auto out_rmat = this->createRMat(this->out_mat, this->outCallbackCalled);
this->run(in_rmat, out_rmat, cv::compile_args(this->kernels()));
}
} // namespace opencv_test

View File

@ -0,0 +1,69 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation
#ifndef OPENCV_GAPI_RMAT_TESTS_COMMON_HPP
#define OPENCV_GAPI_RMAT_TESTS_COMMON_HPP
#include "../test_precomp.hpp"
#include <opencv2/gapi/rmat.hpp>
namespace opencv_test {
class RMatAdapterRef : public RMat::Adapter {
cv::Mat& m_mat;
bool& m_callbackCalled;
public:
RMatAdapterRef(cv::Mat& m, bool& callbackCalled)
: m_mat(m), m_callbackCalled(callbackCalled)
{}
virtual RMat::View access(RMat::Access access) override {
RMat::View::stepsT steps(m_mat.dims);
for (int i = 0; i < m_mat.dims; i++) {
steps[i] = m_mat.step[i];
}
if (access == RMat::Access::W) {
return RMat::View(cv::descr_of(m_mat), m_mat.data, steps,
[this](){
EXPECT_FALSE(m_callbackCalled);
m_callbackCalled = true;
});
} else {
return RMat::View(cv::descr_of(m_mat), m_mat.data, steps);
}
}
virtual cv::GMatDesc desc() const override { return cv::descr_of(m_mat); }
};
class RMatAdapterCopy : public RMat::Adapter {
cv::Mat& m_deviceMat;
cv::Mat m_hostMat;
bool& m_callbackCalled;
public:
RMatAdapterCopy(cv::Mat& m, bool& callbackCalled)
: m_deviceMat(m), m_hostMat(m.clone()), m_callbackCalled(callbackCalled)
{}
virtual RMat::View access(RMat::Access access) override {
RMat::View::stepsT steps(m_hostMat.dims);
for (int i = 0; i < m_hostMat.dims; i++) {
steps[i] = m_hostMat.step[i];
}
if (access == RMat::Access::W) {
return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, steps,
[this](){
EXPECT_FALSE(m_callbackCalled);
m_callbackCalled = true;
m_hostMat.copyTo(m_deviceMat);
});
} else {
m_deviceMat.copyTo(m_hostMat);
return RMat::View(cv::descr_of(m_hostMat), m_hostMat.data, steps);
}
}
virtual cv::GMatDesc desc() const override { return cv::descr_of(m_hostMat); }
};
} // namespace opencv_test
#endif // OPENCV_GAPI_RMAT_TESTS_COMMON_HPP

View File

@ -0,0 +1,126 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/rmat.hpp>
#include "rmat_test_common.hpp"
namespace opencv_test {
namespace {
void randomizeMat(cv::Mat& m) {
auto ref = m.clone();
while (cv::norm(m, ref, cv::NORM_INF) == 0) {
cv::randu(m, cv::Scalar::all(127), cv::Scalar::all(40));
}
}
template <typename RMatAdapterT>
struct RMatTest {
using AdapterT = RMatAdapterT;
RMatTest()
: m_deviceMat(cv::Mat::zeros(8,8,CV_8UC1))
, m_rmat(make_rmat<RMatAdapterT>(m_deviceMat, m_callbackCalled)) {
randomizeMat(m_deviceMat);
expectNoCallbackCalled();
}
RMat& rmat() { return m_rmat; }
cv::Mat cloneDeviceMat() { return m_deviceMat.clone(); }
void expectCallbackCalled() { EXPECT_TRUE(m_callbackCalled); }
void expectNoCallbackCalled() { EXPECT_FALSE(m_callbackCalled); }
void expectDeviceDataEqual(const cv::Mat& mat) {
EXPECT_EQ(0, cv::norm(mat, m_deviceMat, NORM_INF));
}
void expectDeviceDataNotEqual(const cv::Mat& mat) {
EXPECT_NE(0, cv::norm(mat, m_deviceMat, NORM_INF));
}
private:
cv::Mat m_deviceMat;
bool m_callbackCalled = false;
cv::RMat m_rmat;
};
} // anonymous namespace
template<typename T>
struct RMatTypedTest : public ::testing::Test, public T { using Type = T; };
using RMatTestTypes = ::testing::Types< RMatTest<RMatAdapterRef>
, RMatTest<RMatAdapterCopy>
>;
TYPED_TEST_CASE(RMatTypedTest, RMatTestTypes);
TYPED_TEST(RMatTypedTest, Smoke) {
auto view = this->rmat().access(RMat::Access::R);
auto matFromDevice = cv::Mat(view.size(), view.type(), view.ptr());
EXPECT_TRUE(cv::descr_of(this->cloneDeviceMat()) == this->rmat().desc());
this->expectDeviceDataEqual(matFromDevice);
}
static Mat asMat(RMat::View& view) {
return Mat(view.size(), view.type(), view.ptr(), view.step());
}
TYPED_TEST(RMatTypedTest, BasicWorkflow) {
{
auto view = this->rmat().access(RMat::Access::R);
this->expectDeviceDataEqual(asMat(view));
}
this->expectNoCallbackCalled();
cv::Mat dataToWrite = this->cloneDeviceMat();
randomizeMat(dataToWrite);
this->expectDeviceDataNotEqual(dataToWrite);
{
auto view = this->rmat().access(RMat::Access::W);
dataToWrite.copyTo(asMat(view));
}
this->expectCallbackCalled();
this->expectDeviceDataEqual(dataToWrite);
}
TEST(RMat, TestEmptyAdapter) {
RMat rmat;
EXPECT_ANY_THROW(rmat.get<RMatAdapterCopy>());
}
TYPED_TEST(RMatTypedTest, CorrectAdapterCast) {
using T = typename TestFixture::Type::AdapterT;
EXPECT_NE(nullptr, this->rmat().template get<T>());
}
class DummyAdapter : public RMat::Adapter {
virtual RMat::View access(RMat::Access) override { return {}; }
virtual cv::GMatDesc desc() const override { return {}; }
};
TYPED_TEST(RMatTypedTest, IncorrectAdapterCast) {
EXPECT_EQ(nullptr, this->rmat().template get<DummyAdapter>());
}
class RMatAdapterForBackend : public RMat::Adapter {
int m_i;
public:
RMatAdapterForBackend(int i) : m_i(i) {}
virtual RMat::View access(RMat::Access) override { return {}; }
virtual GMatDesc desc() const override { return {}; }
int deviceSpecificData() const { return m_i; }
};
// RMat's usage scenario in the backend:
// we have some specific data hidden under RMat,
// test that we can obtain it via RMat.as<T>() method
TEST(RMat, UsageInBackend) {
int i = 123456;
auto rmat = cv::make_rmat<RMatAdapterForBackend>(i);
auto adapter = rmat.get<RMatAdapterForBackend>();
ASSERT_NE(nullptr, adapter);
EXPECT_EQ(i, adapter->deviceSpecificData());
}
} // namespace opencv_test

View File

@ -0,0 +1,271 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation
#include "../test_precomp.hpp"
#include <opencv2/gapi/rmat.hpp>
#include <opencv2/gapi/util/compiler_hints.hpp>
#include "../src/backends/common/gbackend.hpp"
namespace opencv_test
{
using cv::GMatDesc;
using View = cv::RMat::View;
using cv::Mat;
using cv::gimpl::asMat;
using cv::gimpl::asView;
using namespace ::testing;
static void expect_eq_desc(const GMatDesc& desc, const View& view) {
EXPECT_EQ(desc.size, view.size());
EXPECT_EQ(desc.dims, view.dims());
EXPECT_EQ(desc.size.width, view.cols());
EXPECT_EQ(desc.size.height, view.rows());
EXPECT_EQ(desc.depth, view.depth());
EXPECT_EQ(desc.chan, view.chan());
EXPECT_EQ(desc.depth, view.depth());
EXPECT_EQ(desc.chan, view.chan());
}
TEST(RMatView, TestDefaultConstruction) {
View view;
GMatDesc desc{};
expect_eq_desc(desc, view);
EXPECT_EQ(nullptr, view.ptr());
EXPECT_EQ(0u, view.step());
}
struct RMatViewTest : public TestWithParam<int /*dataType*/>{};
TEST_P(RMatViewTest, ConstructionFromMat) {
auto type = GetParam();
Mat mat(8,8,type);
const auto desc = cv::descr_of(mat);
View view = asView(mat);
expect_eq_desc(desc, view);
EXPECT_EQ(mat.ptr(), view.ptr());
EXPECT_EQ(mat.step, view.step());
}
TEST(RMatView, TestConstructionFromMatND) {
std::vector<int> dims(4, 8);
Mat mat(dims, CV_8UC1);
const auto desc = cv::descr_of(mat);
View view(cv::descr_of(mat), mat.ptr());
expect_eq_desc(desc, view);
EXPECT_EQ(mat.ptr(), view.ptr());
}
TEST_P(RMatViewTest, DefaultStep) {
auto type = GetParam();
GMatDesc desc;
desc.chan = CV_MAT_CN(type);
desc.depth = CV_MAT_DEPTH(type);
desc.size = {8,8};
std::vector<unsigned char> data(desc.size.width*desc.size.height*CV_ELEM_SIZE(type));
View view(desc, data.data());
EXPECT_EQ(static_cast<size_t>(desc.size.width)*CV_ELEM_SIZE(type), view.step());
}
struct RMatViewNDTest : public TestWithParam<
std::tuple<int /*depth*/, int /*ndims*/>>{};
TEST_P(RMatViewNDTest, DefaultStep) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
GAPI_Assert(desc.chan == -1);
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
View view(desc, data.data());
auto step = static_cast<size_t>(total/dims[0]);
EXPECT_EQ(step, view.step(0));
for (int i = 1; i < ndims; i++) {
step /= dims[i];
EXPECT_EQ(step, view.step(i));
}
}
TEST_P(RMatViewNDTest, StepFromMat) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
cv::Mat mat(dims, depth);
auto view = asView(mat);
EXPECT_EQ(mat.ptr(), view.ptr());
for (int i = 0; i < ndims; i++) {
EXPECT_EQ(mat.step[i], view.step(i));
}
}
TEST_P(RMatViewNDTest, StepFromView) {
int depth = 0, ndims = 0;
std::tie(depth, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
std::vector<int> aligned(ndims, 16);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
GAPI_Assert(desc.chan == -1);
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(aligned.begin(), aligned.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
View::stepsT steps(ndims);
auto step = static_cast<size_t>(total/aligned[0]);
steps[0] = step;
for (int i = 1; i < ndims; i++) {
step /= aligned[i];
steps[i] = step;
}
View view(desc, data.data(), steps);
auto mat = asMat(view);
EXPECT_EQ(mat.ptr(), view.ptr());
for (int i = 0; i < ndims; i++) {
EXPECT_EQ(mat.step[i], view.step(i));
}
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTest,
Combine(Values(CV_8U, CV_32F), // depth
Values(1,2,3,4,7))); // ndims
struct RMatViewNDTestNegative : public TestWithParam<
std::tuple<int /*depth*/, int /*chan*/, int /*ndims*/>>{};
TEST_P(RMatViewNDTestNegative, DefaultStep) {
int depth = 0, chan = 0, ndims = 0;
std::tie(depth, chan, ndims) = GetParam();
std::vector<int> dims(ndims, 12);
GMatDesc desc;
desc.dims = dims;
desc.depth = depth;
desc.chan = chan;
auto elemSize = CV_ELEM_SIZE(depth);
auto total = std::accumulate(dims.begin(), dims.end(), elemSize, std::multiplies<int>());
std::vector<unsigned char> data(total);
EXPECT_ANY_THROW(View view(desc, data.data()));
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewNDTestNegative,
Combine(Values(CV_8U, CV_32F), // depth
Values(1,2,3,4), // chan
Values(2,4,7))); // ndims
TEST_P(RMatViewTest, NonDefaultStepInput) {
auto type = GetParam();
Mat bigMat(16,16,type);
cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat mat = bigMat(cv::Rect{4,4,8,8});
View view = asView(mat);
const auto viewMat = asMat(view);
Mat ref, out;
cv::Size ksize{1,1};
cv::blur(viewMat, out, ksize);
cv::blur( mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStepOutput) {
auto type = GetParam();
Mat mat(8,8,type);
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat bigMat = Mat::zeros(16,16,type);
Mat out = bigMat(cv::Rect{4,4,8,8});
View view = asView(out);
auto viewMat = asMat(view);
Mat ref;
cv::Size ksize{1,1};
cv::blur(mat, viewMat, ksize);
cv::blur(mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStep2DInput) {
auto type = GetParam();
Mat bigMat(16,16,type);
cv::randn(bigMat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat mat = bigMat(cv::Rect{4,4,8,8});
View view(cv::descr_of(mat), mat.data, mat.step);
const auto viewMat = asMat(view);
Mat ref, out;
cv::Size ksize{1,1};
cv::blur(viewMat, out, ksize);
cv::blur( mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
TEST_P(RMatViewTest, NonDefaultStep2DOutput) {
auto type = GetParam();
Mat mat(8,8,type);
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
Mat bigMat = Mat::zeros(16,16,type);
Mat out = bigMat(cv::Rect{4,4,8,8});
View view(cv::descr_of(out), out.data, out.step);
auto viewMat = asMat(view);
Mat ref;
cv::Size ksize{1,1};
cv::blur(mat, viewMat, ksize);
cv::blur(mat, ref, ksize);
EXPECT_EQ(0, cvtest::norm(ref, out, NORM_INF));
}
INSTANTIATE_TEST_CASE_P(Test, RMatViewTest,
Values(CV_8UC1, CV_8UC3, CV_32FC1));
struct RMatViewCallbackTest : public ::testing::Test {
RMatViewCallbackTest()
: mat(8,8,CV_8UC1) {
cv::randn(mat, cv::Scalar::all(127), cv::Scalar::all(40));
}
View getView() { return asView(mat, [this](){ callbackCalls++; }); }
int callbackCalls = 0;
Mat mat;
};
TEST_F(RMatViewCallbackTest, MoveCtor) {
{
View copy(getView());
cv::util::suppress_unused_warning(copy);
EXPECT_EQ(0, callbackCalls);
}
EXPECT_EQ(1, callbackCalls);
}
TEST_F(RMatViewCallbackTest, MoveCopy) {
{
View copy;
copy = getView();
cv::util::suppress_unused_warning(copy);
EXPECT_EQ(0, callbackCalls);
}
EXPECT_EQ(1, callbackCalls);
}
static int firstElement(const View& view) { return *view.ptr(); }
static void setFirstElement(View& view, uchar value) { *view.ptr() = value; }
TEST_F(RMatViewCallbackTest, MagazineInteraction) {
cv::gimpl::magazine::Class<View> mag;
constexpr int rc = 1;
constexpr uchar value = 11;
mag.slot<View>()[rc] = getView();
{
auto& mag_view = mag.slot<View>()[rc];
setFirstElement(mag_view, value);
auto mag_el = firstElement(mag_view);
EXPECT_EQ(value, mag_el);
}
{
const auto& mag_view = mag.slot<View>()[rc];
auto mag_el = firstElement(mag_view);
EXPECT_EQ(value, mag_el);
}
EXPECT_EQ(0, callbackCalls);
mag.slot<View>().erase(rc);
EXPECT_EQ(1, callbackCalls);
}
} // namespace opencv_test