22// SPDX-License-Identifier: Apache-2.0
33//
44
5+ #include < stddef.h>
6+
7+ #include < algorithm>
58#include < chrono>
9+ #include < exception>
10+ #include < iomanip>
11+ #include < limits>
12+ #include < memory>
13+ #include < stdexcept>
614#include < string>
15+ #include < utility>
716#include < vector>
817
9- #include < opencv2/gapi/streaming/cap.hpp >
10- #include < opencv2/gapi/imgproc .hpp>
18+ #include < gflags/gflags.h >
19+ #include < opencv2/core .hpp>
1120#include < opencv2/gapi/core.hpp>
12-
13- #include < opencv2/gapi/fluid/core.hpp>
14- #include < opencv2/gapi/fluid/imgproc.hpp>
1521#include < opencv2/gapi/cpu/core.hpp>
1622#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>
1738#include < opencv2/highgui.hpp>
18-
39+ # include < opencv2/imgproc.hpp >
1940#include < openvino/openvino.hpp>
2041
2142#include < monitors/presenter.h>
22- #include < utils_gapi/stream_source.hpp>
2343#include < utils/args_helper.hpp>
44+ #include < utils/common.hpp>
2445#include < utils/config_factory.h>
46+ #include < utils/images_capture.h>
2547#include < utils/ocv_common.hpp>
48+ #include < utils/performance_metrics.hpp>
49+ #include < utils/slog.hpp>
50+ #include < utils_gapi/stream_source.hpp>
2651
2752#include " background_subtraction_demo_gapi.hpp"
2853#include " custom_kernels.hpp"
2954
30-
3155namespace util {
32- bool ParseAndCheckCommandLine (int argc, char * argv[]) {
56+ bool ParseAndCheckCommandLine (int argc, char * argv[]) {
3357 /* * ---------- Parsing and validating input arguments ----------**/
3458 gflags::ParseCommandLineNonHelpFlags (&argc, &argv, true );
3559 if (FLAGS_h) {
@@ -49,20 +73,18 @@ bool ParseAndCheckCommandLine(int argc, char *argv[]) {
4973
5074static cv::gapi::GKernelPackage getKernelPackage (const std::string& type) {
5175 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 ());
5477 } 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 ());
5779 } else {
5880 throw std::logic_error (" Unsupported kernel package type: " + type);
5981 }
6082 GAPI_Assert (false && " Unreachable code!" );
6183}
6284
63- } // namespace util
85+ } // namespace util
6486
65- int main (int argc, char * argv[]) {
87+ int main (int argc, char * argv[]) {
6688 try {
6789 PerformanceMetrics metrics;
6890
@@ -87,12 +109,16 @@ int main(int argc, char *argv[]) {
87109 }
88110
89111 /* * 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));
92118 const auto tmp = cap->read ();
93119 cv::Size frame_size = cv::Size{tmp.cols , tmp.rows };
94120
95- cv::GComputation comp ([&]{
121+ cv::GComputation comp ([&] {
96122 cv::GMat in;
97123 // NB: target_bgr is optional second input which implies a background
98124 // that will change user video background. If user don't specify
@@ -108,9 +134,8 @@ int main(int argc, char *argv[]) {
108134 bgr_resized = cv::gapi::resize (target_bgr.value (), frame_size);
109135 }
110136
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;
114139
115140 auto result = model->replace (in, frame_size, background);
116141
@@ -123,35 +148,41 @@ int main(int argc, char *argv[]) {
123148 });
124149
125150 /* * 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)));
141168
142169 /* * Output container for result **/
143170 cv::Mat output;
144171
145172 /* * ---------------- 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));
148179 auto pipeline_inputs = cv::gin (cv::gapi::wip::make_src<custom::CommonCapSrc>(cap));
149180 if (!is_blur && FLAGS_target_bgr.empty ()) {
150181 cv::Scalar default_color (155 , 255 , 120 );
151182 pipeline_inputs += cv::gin (cv::Mat (frame_size, CV_8UC3, default_color));
152183 } 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 ());
155186 pipeline_inputs += cv::gin (cv::gapi::wip::make_src<custom::CommonCapSrc>(target_bgr_cap));
156187 }
157188
@@ -168,16 +199,27 @@ int main(int argc, char *argv[]) {
168199 const auto startTime = std::chrono::steady_clock::now ();
169200 pipeline.start ();
170201
171- while (pipeline.pull (cv::gout (output))) {
202+ while (pipeline.pull (cv::gout (output))) {
172203 presenter.drawGraphs (output);
173204 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);
176213 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);
181223 }
182224
183225 videoWriter.write (output);
@@ -196,12 +238,10 @@ int main(int argc, char *argv[]) {
196238 slog::info << " Metrics report:" << slog::endl;
197239 slog::info << " \t FPS: " << std::fixed << std::setprecision (1 ) << metrics.getTotal ().fps << slog::endl;
198240 slog::info << presenter.reportMeans () << slog::endl;
199- }
200- catch (const std::exception& error) {
241+ } catch (const std::exception& error) {
201242 slog::err << error.what () << slog::endl;
202243 return 1 ;
203- }
204- catch (...) {
244+ } catch (...) {
205245 slog::err << " Unknown/internal exception happened." << slog::endl;
206246 return 1 ;
207247 }
0 commit comments