2
2
// SPDX-License-Identifier: Apache-2.0
3
3
//
4
4
5
- #include < chrono>
6
- #include < string>
7
- #include < vector>
8
-
9
- #include < opencv2/gapi/streaming/cap.hpp>
10
- #include < opencv2/gapi/imgproc.hpp>
11
- #include < opencv2/gapi/core.hpp>
12
-
13
- #include < opencv2/gapi/fluid/core.hpp>
14
- #include < opencv2/gapi/fluid/imgproc.hpp>
15
- #include < opencv2/gapi/cpu/core.hpp>
16
- #include < opencv2/gapi/cpu/imgproc.hpp>
17
- #include < opencv2/highgui.hpp>
18
-
19
- #include < openvino/openvino.hpp>
20
-
21
- #include < monitors/presenter.h>
22
- #include < utils_gapi/stream_source.hpp>
23
- #include < utils/args_helper.hpp>
24
- #include < utils/config_factory.h>
25
- #include < utils/ocv_common.hpp>
26
-
27
- #include " background_subtraction_demo_gapi.hpp"
28
- #include " custom_kernels.hpp"
29
-
5
+ #include < stddef.h> // for size_t
6
+
7
+ #include < algorithm> // for max
8
+ #include < chrono> // for steady_clock
9
+ #include < exception> // for exception
10
+ #include < iomanip> // for operator<<, _Setprecision, setprecision, fixed
11
+ #include < limits> // for numeric_limits
12
+ #include < memory> // for shared_ptr, make_shared, __shared_ptr_access
13
+ #include < stdexcept> // for logic_error
14
+ #include < string> // for allocator, operator==, operator+, string, basic_string, char...
15
+ #include < utility> // for move
16
+ #include < vector> // for vector
17
+
18
+ #include < opencv2/core.hpp> // for Size, Mat, CV_8UC3, Scalar
19
+ #include < opencv2/gapi/core.hpp> // for resize
20
+ #include < opencv2/gapi/cpu/core.hpp> // for kernels
21
+ #include < opencv2/gapi/cpu/imgproc.hpp> // for kernels
22
+ #include < opencv2/gapi/fluid/core.hpp> // for kernels
23
+ #include < opencv2/gapi/fluid/imgproc.hpp> // for kernels
24
+ #include < opencv2/gapi/garg.hpp> // for gin, operator+=, gout
25
+ #include < opencv2/gapi/gcommon.hpp> // for compile_args
26
+ #include < opencv2/gapi/gcomputation.hpp> // for GComputation, GComputation::Generator
27
+ #include < opencv2/gapi/gkernel.hpp> // for combine, GKernelPackage
28
+ #include < opencv2/gapi/gmat.hpp> // for GMat
29
+ #include < opencv2/gapi/gproto.hpp> // for GIn, GOut, operator+=, GIOProtoArgs
30
+ #include < opencv2/gapi/gstreaming.hpp> // for GStreamingCompiled
31
+ #include < opencv2/gapi/imgproc.hpp> // for blur
32
+ #include < opencv2/gapi/infer.hpp> // for networks, Generic (ptr only)
33
+ #include < opencv2/gapi/infer/ie.hpp> // for Params
34
+ #include < opencv2/gapi/own/assert.hpp> // for GAPI_Assert
35
+ #include < opencv2/gapi/streaming/source.hpp> // for make_src
36
+ #include < opencv2/gapi/util/optional.hpp> // for optional, make_optional
37
+ #include < opencv2/highgui.hpp> // for imshow, waitKey
38
+ #include < opencv2/imgproc.hpp> // for FONT_HERSHEY_COMPLEX
39
+ #include < openvino/openvino.hpp> // for get_openvino_version
40
+
41
+ #include < monitors/presenter.h> // for Presenter
42
+ #include < utils/args_helper.hpp> // for stringToSize
43
+ #include < utils/common.hpp> // for fileNameNoExt, operator<<, showAvailableDevices
44
+ #include < utils/config_factory.h> // for ModelConfig, ConfigFactory
45
+ #include < utils/images_capture.h> // for openImagesCapture, ImagesCapture, read_type, read_type::safe
46
+ #include < utils/ocv_common.hpp> // for LazyVideoWriter
47
+ #include < utils/performance_metrics.hpp> // for PerformanceMetrics, PerformanceMetrics::FPS, PerformanceMetr...
48
+ #include < utils/slog.hpp> // for LogStream, endl, info, err
49
+ #include < utils_gapi/stream_source.hpp> // for CommonCapSrc
50
+
51
+ #include " background_subtraction_demo_gapi.hpp" // for FLAGS_m, FLAGS_at, FLAGS_target_bgr, FLAGS_blur_bgr, FLAGS_d
52
+ #include " custom_kernels.hpp" // for BGMattingReplacer, MaskRCNNBGReplacer, kernels, NNBGReplacer
53
+ #include " gflags/gflags.h" // for clstring, ParseCommandLineNonHelpFlags
30
54
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