Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / opencv_test_gapi / fluid_test_computations / fluid_test_computations.cpp
1 #include <fluid_test_computations.hpp>
2 #include <opencv2/gapi.hpp>
3 #include <ie_preprocess_gapi_kernels.hpp>
4
5 #define CV_MAT_CHANNELS(flags) (((flags) >> CV_CN_SHIFT) + 1)
6
7 namespace opencv_test
8 {
9 struct FluidComputation::Priv
10 {
11     cv::GComputation m_c;
12     std::vector<cv::gapi::own::Mat> m_v_in;
13     std::vector<cv::gapi::own::Mat> m_v_out;
14 };
15
16 FluidComputation::FluidComputation(Priv *priv)
17     : m_priv(priv)
18 {}
19
20 void FluidComputation::warmUp()
21 {
22     m_priv->m_c.apply(m_priv->m_v_in, m_priv->m_v_out, cv::compile_args(InferenceEngine::gapi::preprocKernels()));
23 }
24
25 void FluidComputation::apply()
26 {
27     m_priv->m_c.apply(m_priv->m_v_in, m_priv->m_v_out);
28 }
29
30 namespace
31 {
32 cv::gapi::own::Mat to_own(test::Mat mat) { return {mat.rows, mat.cols, mat.type, mat.data}; }
33
34 std::vector<cv::gapi::own::Mat> to_own(std::vector<test::Mat> mats)
35 {
36     std::vector<cv::gapi::own::Mat> own_mats(mats.size());
37     for (int i = 0; i < mats.size(); i++) {
38         own_mats[i] = to_own(mats[i]);
39     }
40     return own_mats;
41 }
42
43 template<typename... Ts, int... IIs>
44 std::vector<cv::GMat> to_vec_impl(std::tuple<Ts...> &&gmats, cv::detail::Seq<IIs...>) {
45     return { std::get<IIs>(gmats)... };
46 }
47
48 template<typename... Ts>
49 std::vector<cv::GMat> to_vec(std::tuple<Ts...> &&gmats) {
50     return to_vec_impl(std::move(gmats), typename cv::detail::MkSeq<sizeof...(Ts)>::type());
51 }
52 } // anonymous namespace
53
54 static cv::GComputation buildResizeComputation(test::Mat inMat, test::Mat outMat, int interp)
55 {
56     cv::gapi::own::Size sz_in  { inMat.cols,  inMat.rows};
57     cv::gapi::own::Size sz_out {outMat.cols, outMat.rows};
58     int type = outMat.type;
59     cv::GMat in, out;
60     switch (CV_MAT_CHANNELS(type)) {
61     case 1:
62         out = InferenceEngine::gapi::ScalePlane::on(in, type, sz_in, sz_out, interp);
63         break;
64     case 3:
65         {
66         int depth = CV_MAT_DEPTH(type);
67         int type1 = CV_MAKE_TYPE(depth, 1);
68         cv::GMat in0, in1, in2, out0, out1, out2;
69         std::tie(in0, in1, in2) = InferenceEngine::gapi::Split3::on(in);
70         out0 = InferenceEngine::gapi::ScalePlane::on(in0, type1, sz_in, sz_out, interp);
71         out1 = InferenceEngine::gapi::ScalePlane::on(in1, type1, sz_in, sz_out, interp);
72         out2 = InferenceEngine::gapi::ScalePlane::on(in2, type1, sz_in, sz_out, interp);
73         out = InferenceEngine::gapi::Merge3::on(out0, out1, out2);
74         }
75         break;
76     default: GAPI_Assert(!"ERROR: unsupported number of channels!");
77     }
78
79     return cv::GComputation(in, out);
80 }
81
82 FluidResizeComputation::FluidResizeComputation(test::Mat inMat, test::Mat outMat, int interp)
83     : FluidComputation(new Priv{buildResizeComputation(inMat, outMat, interp)
84                                ,{to_own(inMat)}
85                                ,{to_own(outMat)}
86                                })
87 {}
88
89 static cv::GComputation buildSplitComputation(int planes)
90 {
91     std::vector<cv::GMat> ins(1);
92     std::vector<cv::GMat> outs(planes);
93
94     switch (planes) {
95     case 2: outs = to_vec(InferenceEngine::gapi::Split2::on(ins[0])); break;
96     case 3: outs = to_vec(InferenceEngine::gapi::Split3::on(ins[0])); break;
97     case 4: outs = to_vec(InferenceEngine::gapi::Split4::on(ins[0])); break;
98     default: GAPI_Assert(false);
99     }
100
101     return cv::GComputation(ins, outs);
102 }
103
104 FluidSplitComputation::FluidSplitComputation(test::Mat inMat, std::vector<test::Mat> outMats)
105     : FluidComputation(new Priv{buildSplitComputation(outMats.size())
106                                ,{to_own(inMat)}
107                                ,to_own(outMats)
108                                })
109 {}
110
111 static cv::GComputation buildMergeComputation(int planes)
112 {
113     std::vector<cv::GMat> ins(planes);
114     std::vector<cv::GMat> outs(1);
115
116     switch (planes) {
117     case 2: outs[0] = InferenceEngine::gapi::Merge2::on(ins[0], ins[1]); break;
118     case 3: outs[0] = InferenceEngine::gapi::Merge3::on(ins[0], ins[1], ins[2]); break;
119     case 4: outs[0] = InferenceEngine::gapi::Merge4::on(ins[0], ins[1], ins[2], ins[3]); break;
120     default: GAPI_Assert(false);
121     }
122
123     return cv::GComputation(ins, outs);
124 }
125
126 FluidMergeComputation::FluidMergeComputation(std::vector<test::Mat> inMats, test::Mat outMat)
127     : FluidComputation(new Priv{buildMergeComputation(inMats.size())
128                                ,to_own(inMats)
129                                ,{to_own(outMat)}
130                                })
131 {}
132
133 } // namespace opencv_test