2
2
// SPDX-License-Identifier: Apache-2.0
3
3
//
4
4
5
+ #include < stddef.h>
6
+
7
+ #include < algorithm>
5
8
#include < chrono>
9
+ #include < exception>
10
+ #include < iomanip>
11
+ #include < limits>
12
+ #include < memory>
13
+ #include < stdexcept>
6
14
#include < string>
15
+ #include < utility>
7
16
#include < vector>
8
17
9
- #include < opencv2/gapi/streaming/cap.hpp >
10
- #include < opencv2/gapi/imgproc .hpp>
18
+ #include < gflags/gflags.h >
19
+ #include < opencv2/core .hpp>
11
20
#include < opencv2/gapi/core.hpp>
12
-
13
- #include < opencv2/gapi/fluid/core.hpp>
14
- #include < opencv2/gapi/fluid/imgproc.hpp>
15
21
#include < opencv2/gapi/cpu/core.hpp>
16
22
#include < opencv2/gapi/cpu/imgproc.hpp>
23
+ #include < opencv2/gapi/fluid/core.hpp>
24
+ #include < opencv2/gapi/fluid/imgproc.hpp>
25
+ #include < opencv2/gapi/garg.hpp>
26
+ #include < opencv2/gapi/gcommon.hpp>
27
+ #include < opencv2/gapi/gcomputation.hpp>
28
+ #include < opencv2/gapi/gkernel.hpp>
29
+ #include < opencv2/gapi/gmat.hpp>
30
+ #include < opencv2/gapi/gproto.hpp>
31
+ #include < opencv2/gapi/gstreaming.hpp>
32
+ #include < opencv2/gapi/imgproc.hpp>
33
+ #include < opencv2/gapi/infer.hpp>
34
+ #include < opencv2/gapi/infer/ie.hpp>
35
+ #include < opencv2/gapi/own/assert.hpp>
36
+ #include < opencv2/gapi/streaming/source.hpp>
37
+ #include < opencv2/gapi/util/optional.hpp>
17
38
#include < opencv2/highgui.hpp>
18
-
39
+ # include < opencv2/imgproc.hpp >
19
40
#include < openvino/openvino.hpp>
20
41
21
42
#include < monitors/presenter.h>
22
- #include < utils_gapi/stream_source.hpp>
23
43
#include < utils/args_helper.hpp>
44
+ #include < utils/common.hpp>
24
45
#include < utils/config_factory.h>
46
+ #include < utils/images_capture.h>
25
47
#include < utils/ocv_common.hpp>
48
+ #include < utils/performance_metrics.hpp>
49
+ #include < utils/slog.hpp>
50
+ #include < utils_gapi/stream_source.hpp>
26
51
27
52
#include " background_subtraction_demo_gapi.hpp"
28
53
#include " custom_kernels.hpp"
29
54
30
-
31
55
namespace util {
32
- bool ParseAndCheckCommandLine (int argc, char * argv[]) {
56
+ bool ParseAndCheckCommandLine (int argc, char * argv[]) {
33
57
/* * ---------- Parsing and validating input arguments ----------**/
34
58
gflags::ParseCommandLineNonHelpFlags (&argc, &argv, true );
35
59
if (FLAGS_h) {
@@ -49,20 +73,18 @@ bool ParseAndCheckCommandLine(int argc, char *argv[]) {
49
73
50
74
static cv::gapi::GKernelPackage getKernelPackage (const std::string& type) {
51
75
if (type == " opencv" ) {
52
- return cv::gapi::combine (cv::gapi::core::cpu::kernels (),
53
- cv::gapi::imgproc::cpu::kernels ());
76
+ return cv::gapi::combine (cv::gapi::core::cpu::kernels (), cv::gapi::imgproc::cpu::kernels ());
54
77
} else if (type == " fluid" ) {
55
- return cv::gapi::combine (cv::gapi::core::fluid::kernels (),
56
- cv::gapi::imgproc::fluid::kernels ());
78
+ return cv::gapi::combine (cv::gapi::core::fluid::kernels (), cv::gapi::imgproc::fluid::kernels ());
57
79
} else {
58
80
throw std::logic_error (" Unsupported kernel package type: " + type);
59
81
}
60
82
GAPI_Assert (false && " Unreachable code!" );
61
83
}
62
84
63
- } // namespace util
85
+ } // namespace util
64
86
65
- int main (int argc, char * argv[]) {
87
+ int main (int argc, char * argv[]) {
66
88
try {
67
89
PerformanceMetrics metrics;
68
90
@@ -87,12 +109,16 @@ int main(int argc, char *argv[]) {
87
109
}
88
110
89
111
/* * Get information about frame **/
90
- std::shared_ptr<ImagesCapture> cap = openImagesCapture (FLAGS_i, FLAGS_loop, read_type::safe, 0 ,
91
- std::numeric_limits<size_t >::max (), stringToSize (FLAGS_res));
112
+ std::shared_ptr<ImagesCapture> cap = openImagesCapture (FLAGS_i,
113
+ FLAGS_loop,
114
+ read_type::safe,
115
+ 0 ,
116
+ std::numeric_limits<size_t >::max (),
117
+ stringToSize (FLAGS_res));
92
118
const auto tmp = cap->read ();
93
119
cv::Size frame_size = cv::Size {tmp.cols , tmp.rows };
94
120
95
- cv::GComputation comp ([&]{
121
+ cv::GComputation comp ([&] {
96
122
cv::GMat in;
97
123
// NB: target_bgr is optional second input which implies a background
98
124
// that will change user video background. If user don't specify
@@ -108,9 +134,8 @@ int main(int argc, char *argv[]) {
108
134
bgr_resized = cv::gapi::resize (target_bgr.value (), frame_size);
109
135
}
110
136
111
- auto background = is_blur
112
- ? cv::gapi::blur (bgr_resized, cv::Size (FLAGS_blur_bgr, FLAGS_blur_bgr))
113
- : bgr_resized;
137
+ auto background =
138
+ is_blur ? cv::gapi::blur (bgr_resized, cv::Size (FLAGS_blur_bgr, FLAGS_blur_bgr)) : bgr_resized;
114
139
115
140
auto result = model->replace (in, frame_size, background);
116
141
@@ -123,35 +148,41 @@ int main(int argc, char *argv[]) {
123
148
});
124
149
125
150
/* * Configure network **/
126
- auto config = ConfigFactory::getUserConfig (FLAGS_d, FLAGS_nireq,
127
- FLAGS_nstreams, FLAGS_nthreads);
128
- const auto net = cv::gapi::ie::Params<cv::gapi::Generic> {
129
- model->getName (),
130
- FLAGS_m, // path to topology IR
131
- fileNameNoExt (FLAGS_m) + " .bin" , // path to weights
132
- FLAGS_d // device specifier
133
- }.cfgNumRequests (config.maxAsyncRequests )
134
- .pluginConfig (config.getLegacyConfig ());
135
- slog::info << " The background matting model " << FLAGS_m << " is loaded to " << FLAGS_d << " device." << slog::endl;
136
-
137
- auto kernels = cv::gapi::combine (custom::kernels (),
138
- util::getKernelPackage (FLAGS_kernel_package));
139
- auto pipeline = comp.compileStreaming (cv::compile_args (kernels,
140
- cv::gapi::networks (net)));
151
+ auto config = ConfigFactory::getUserConfig (FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads);
152
+ // clang-format off
153
+ const auto net =
154
+ cv::gapi::ie::Params<cv::gapi::Generic>{
155
+ model->getName (),
156
+ FLAGS_m, // path to topology IR
157
+ fileNameNoExt (FLAGS_m) + " .bin" , // path to weights
158
+ FLAGS_d // device specifier
159
+ }.cfgNumRequests (config.maxAsyncRequests )
160
+ .pluginConfig (config.getLegacyConfig ());
161
+ // clang-format on
162
+
163
+ slog::info << " The background matting model " << FLAGS_m << " is loaded to " << FLAGS_d << " device."
164
+ << slog::endl;
165
+
166
+ auto kernels = cv::gapi::combine (custom::kernels (), util::getKernelPackage (FLAGS_kernel_package));
167
+ auto pipeline = comp.compileStreaming (cv::compile_args (kernels, cv::gapi::networks (net)));
141
168
142
169
/* * Output container for result **/
143
170
cv::Mat output;
144
171
145
172
/* * ---------------- The execution part ---------------- **/
146
- cap = openImagesCapture (FLAGS_i, FLAGS_loop, read_type::safe, 0 ,
147
- std::numeric_limits<size_t >::max (), stringToSize (FLAGS_res));
173
+ cap = openImagesCapture (FLAGS_i,
174
+ FLAGS_loop,
175
+ read_type::safe,
176
+ 0 ,
177
+ std::numeric_limits<size_t >::max (),
178
+ stringToSize (FLAGS_res));
148
179
auto pipeline_inputs = cv::gin (cv::gapi::wip::make_src<custom::CommonCapSrc>(cap));
149
180
if (!is_blur && FLAGS_target_bgr.empty ()) {
150
181
cv::Scalar default_color (155 , 255 , 120 );
151
182
pipeline_inputs += cv::gin (cv::Mat (frame_size, CV_8UC3, default_color));
152
183
} else if (!FLAGS_target_bgr.empty ()) {
153
- std::shared_ptr<ImagesCapture> target_bgr_cap = openImagesCapture (FLAGS_target_bgr, true , read_type::safe, 0 ,
154
- std::numeric_limits<size_t >::max ());
184
+ std::shared_ptr<ImagesCapture> target_bgr_cap =
185
+ openImagesCapture (FLAGS_target_bgr, true , read_type::safe, 0 , std::numeric_limits<size_t >::max ());
155
186
pipeline_inputs += cv::gin (cv::gapi::wip::make_src<custom::CommonCapSrc>(target_bgr_cap));
156
187
}
157
188
@@ -168,16 +199,27 @@ int main(int argc, char *argv[]) {
168
199
const auto startTime = std::chrono::steady_clock::now ();
169
200
pipeline.start ();
170
201
171
- while (pipeline.pull (cv::gout (output))) {
202
+ while (pipeline.pull (cv::gout (output))) {
172
203
presenter.drawGraphs (output);
173
204
if (isStart) {
174
- metrics.update (startTime, output, { 10 , 22 }, cv::FONT_HERSHEY_COMPLEX,
175
- 0.65 , { 200 , 10 , 10 }, 2 , PerformanceMetrics::MetricTypes::FPS);
205
+ metrics.update (startTime,
206
+ output,
207
+ {10 , 22 },
208
+ cv::FONT_HERSHEY_COMPLEX,
209
+ 0.65 ,
210
+ {200 , 10 , 10 },
211
+ 2 ,
212
+ PerformanceMetrics::MetricTypes::FPS);
176
213
isStart = false ;
177
- }
178
- else {
179
- metrics.update ({}, output, { 10 , 22 }, cv::FONT_HERSHEY_COMPLEX,
180
- 0.65 , { 200 , 10 , 10 }, 2 , PerformanceMetrics::MetricTypes::FPS);
214
+ } else {
215
+ metrics.update ({},
216
+ output,
217
+ {10 , 22 },
218
+ cv::FONT_HERSHEY_COMPLEX,
219
+ 0.65 ,
220
+ {200 , 10 , 10 },
221
+ 2 ,
222
+ PerformanceMetrics::MetricTypes::FPS);
181
223
}
182
224
183
225
videoWriter.write (output);
@@ -196,12 +238,10 @@ int main(int argc, char *argv[]) {
196
238
slog::info << " Metrics report:" << slog::endl;
197
239
slog::info << " \t FPS: " << std::fixed << std::setprecision (1 ) << metrics.getTotal ().fps << slog::endl;
198
240
slog::info << presenter.reportMeans () << slog::endl;
199
- }
200
- catch (const std::exception & error) {
241
+ } catch (const std::exception & error) {
201
242
slog::err << error.what () << slog::endl;
202
243
return 1 ;
203
- }
204
- catch (...) {
244
+ } catch (...) {
205
245
slog::err << " Unknown/internal exception happened." << slog::endl;
206
246
return 1 ;
207
247
}
0 commit comments