Skip to content

Commit cb2e276

Browse files
authored
Merge pull request opencv#17741 from aDanPin:dp/add_dinamic_graph_feature
[G-API] Allow building graphs with a dynamic number of inputs and outputs * Add dinamic graph feature and tests * Remove unnecessary file * Review response * Add implementation of operator += for GRunArgs And test for that case * Tests refactoring * Add doxygen Review response * Fix docs * A small documentation fix * Review response * Add tests for more entities * Add typed tests * Another typed tests * Doc fix * Documentation fix * Build fix * Commit for rebuild * The last one
1 parent cd0f038 commit cb2e276

File tree

4 files changed

+455
-1
lines changed

4 files changed

+455
-1
lines changed

modules/gapi/include/opencv2/gapi/garg.hpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ using GRunArg = util::variant<
102102
>;
103103
using GRunArgs = std::vector<GRunArg>;
104104

105+
// TODO: Think about the addition operator
106+
/**
107+
* @brief This operator allows to complement the input vector at runtime.
108+
*
109+
* It's an ordinary overload of addition assignment operator.
110+
*
111+
* Example of usage:
112+
* @snippet dynamic_graph.cpp GRunArgs usage
113+
*
114+
*/
115+
inline GRunArgs& operator += (GRunArgs &lhs, const GRunArgs &rhs)
116+
{
117+
lhs.reserve(lhs.size() + rhs.size());
118+
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
119+
return lhs;
120+
}
121+
105122
namespace gapi
106123
{
107124
namespace wip
@@ -133,10 +150,27 @@ using GRunArgP = util::variant<
133150
>;
134151
using GRunArgsP = std::vector<GRunArgP>;
135152

153+
// TODO: Think about the addition operator
154+
/**
155+
* @brief This operator allows to complement the output vector at runtime.
156+
*
157+
* It's an ordinary overload of addition assignment operator.
158+
*
159+
* Example of usage:
160+
* @snippet dynamic_graph.cpp GRunArgsP usage
161+
*
162+
*/
163+
inline GRunArgsP& operator += (GRunArgsP &lhs, const GRunArgsP &rhs)
164+
{
165+
lhs.reserve(lhs.size() + rhs.size());
166+
lhs.insert(lhs.end(), rhs.begin(), rhs.end());
167+
return lhs;
168+
}
169+
136170
namespace gapi
137171
{
138172
GAPI_EXPORTS cv::GRunArgsP bind(cv::GRunArgs &results);
139-
}
173+
} // namespace gapi
140174

141175
template<typename... Ts> inline GRunArgs gin(const Ts&... args)
142176
{

modules/gapi/include/opencv2/gapi/gproto.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,29 @@ struct GIOProtoArgs
6161
explicit GIOProtoArgs(GProtoArgs &&args) : m_args(std::move(args)) {}
6262

6363
GProtoArgs m_args;
64+
65+
// TODO: Think about the addition operator
66+
/**
67+
* @brief This operator allows to complement the proto vectors at runtime.
68+
*
69+
* It's an ordinary overload of addition assignment operator.
70+
*
71+
* Example of usage:
72+
* @snippet dynamic_graph.cpp GIOProtoArgs usage
73+
*
74+
*/
75+
template<typename Tg>
76+
friend GIOProtoArgs<Tg>& operator += (GIOProtoArgs<Tg> &lhs, const GIOProtoArgs<Tg> &rhs);
6477
};
6578

79+
template<typename Tg>
80+
cv::GIOProtoArgs<Tg>& operator += (cv::GIOProtoArgs<Tg> &lhs, const cv::GIOProtoArgs<Tg> &rhs)
81+
{
82+
lhs.m_args.reserve(lhs.m_args.size() + rhs.m_args.size());
83+
lhs.m_args.insert(lhs.m_args.end(), rhs.m_args.begin(), rhs.m_args.end());
84+
return lhs;
85+
}
86+
6687
struct In_Tag{};
6788
struct Out_Tag{};
6889

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <opencv2/gapi.hpp>
2+
#include <opencv2/gapi/core.hpp>
3+
#include <opencv2/gapi/cpu/core.hpp>
4+
5+
int main(int argc, char *argv[])
6+
{
7+
(void) argc;
8+
(void) argv;
9+
10+
bool need_first_conversion = true;
11+
bool need_second_conversion = false;
12+
13+
cv::Size szOut(4, 4);
14+
cv::GComputation cc([&](){
15+
// ! [GIOProtoArgs usage]
16+
auto ins = cv::GIn();
17+
cv::GMat in1;
18+
if (need_first_conversion)
19+
ins += cv::GIn(in1);
20+
21+
cv::GMat in2;
22+
if (need_second_conversion)
23+
ins += cv::GIn(in2);
24+
25+
auto outs = cv::GOut();
26+
cv::GMat out1 = cv::gapi::resize(in1, szOut);
27+
if (need_first_conversion)
28+
outs += cv::GOut(out1);
29+
30+
cv::GMat out2 = cv::gapi::resize(in2, szOut);
31+
if (need_second_conversion)
32+
outs += cv::GOut(out2);
33+
// ! [GIOProtoArgs usage]
34+
return cv::GComputation(std::move(ins), std::move(outs));
35+
});
36+
37+
// ! [GRunArgs usage]
38+
auto in_vector = cv::gin();
39+
40+
cv::Mat in_mat1( 8, 8, CV_8UC3);
41+
cv::Mat in_mat2(16, 16, CV_8UC3);
42+
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
43+
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
44+
45+
if (need_first_conversion)
46+
in_vector += cv::gin(in_mat1);
47+
if (need_second_conversion)
48+
in_vector += cv::gin(in_mat2);
49+
// ! [GRunArgs usage]
50+
51+
// ! [GRunArgsP usage]
52+
auto out_vector = cv::gout();
53+
cv::Mat out_mat1, out_mat2;
54+
if (need_first_conversion)
55+
out_vector += cv::gout(out_mat1);
56+
if (need_second_conversion)
57+
out_vector += cv::gout(out_mat2);
58+
// ! [GRunArgsP usage]
59+
60+
auto stream = cc.compileStreaming(cv::compile_args(cv::gapi::core::cpu::kernels()));
61+
stream.setSource(std::move(in_vector));
62+
63+
stream.start();
64+
stream.pull(std::move(out_vector));
65+
stream.stop();
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)