Skip to content

Commit a9bef2a

Browse files
authored
Merge pull request #3418 from ivikhrev/style-fix
Fix code style for gapi demos
2 parents 8583688 + 31edcc0 commit a9bef2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2635
-1759
lines changed

demos/background_subtraction_demo/cpp_gapi/background_subtraction_demo_gapi.hpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
///////////////////////////////////////////////////////////////////////////////////////////////////
66
#pragma once
7+
#include <iostream>
78

89
#include <gflags/gflags.h>
10+
911
#include <utils/default_flags.hpp>
1012

1113
DEFINE_INPUT_FLAGS
@@ -15,17 +17,21 @@ static const char help_message[] = "Print a usage message.";
1517
static const char camera_resolution_message[] = "Optional. Set camera resolution in format WxH.";
1618
static const char at_message[] = "Required. Architecture type: maskrcnn, background-matting.";
1719
static const char model_message[] = "Required. Path to an .xml file with a trained model.";
18-
static const char kernel_package_message[] = "Optional. G-API kernel package type: opencv, fluid (by default opencv is used).";
19-
static const char device_message[] = "Optional. Target device for network (the list of available devices is shown below). "
20-
"The demo will look for a suitable plugin for a specified device. Default value is \"CPU\".";
21-
static const char nireq_message[] = "Optional. Number of infer requests. If this option is omitted, number of infer requests is determined automatically.";
20+
static const char kernel_package_message[] =
21+
"Optional. G-API kernel package type: opencv, fluid (by default opencv is used).";
22+
static const char device_message[] =
23+
"Optional. Target device for network (the list of available devices is shown below). "
24+
"The demo will look for a suitable plugin for a specified device. Default value is \"CPU\".";
25+
static const char nireq_message[] = "Optional. Number of infer requests. If this option is omitted, number of infer "
26+
"requests is determined automatically.";
2227
static const char num_threads_message[] = "Optional. Number of threads.";
2328
static const char num_streams_message[] = "Optional. Number of streams to use for inference on the CPU or/and GPU in "
24-
"throughput mode (for HETERO and MULTI device cases use format "
25-
"<device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>)";
29+
"throughput mode (for HETERO and MULTI device cases use format "
30+
"<device1>:<nstreams1>,<device2>:<nstreams2> or just <nstreams>)";
2631
static const char no_show_message[] = "Optional. Don't show output.";
2732
static const char blur_bgr_message[] = "Optional. Blur background.";
28-
static const char target_bgr_message[] = "Optional. Background onto which to composite the output (by default to green field).";
33+
static const char target_bgr_message[] =
34+
"Optional. Background onto which to composite the output (by default to green field).";
2935
static const char utilization_monitors_message[] = "Optional. List of monitors to show initially.";
3036

3137
DEFINE_bool(h, false, help_message);
@@ -43,8 +49,8 @@ DEFINE_uint32(blur_bgr, 0, blur_bgr_message);
4349
DEFINE_string(u, "", utilization_monitors_message);
4450

4551
/**
46-
* \brief This function shows a help message
47-
*/
52+
* \brief This function shows a help message
53+
*/
4854

4955
static void showUsage() {
5056
std::cout << std::endl;

demos/background_subtraction_demo/cpp_gapi/include/custom_kernels.hpp

+18-9
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44

55
#pragma once
66

7-
#include <opencv2/gapi/gkernel.hpp>
8-
#include <opencv2/gapi/infer/ie.hpp>
7+
#include <string>
8+
#include <vector>
99

10-
#include <inference_engine.hpp>
10+
#include <cpp/ie_cnn_network.h>
11+
#include <ie_allocator.hpp>
12+
#include <ie_common.h>
13+
#include <ie_input_info.hpp>
14+
#include <opencv2/core.hpp>
15+
#include <opencv2/gapi/gkernel.hpp>
16+
#include <opencv2/gapi/gmat.hpp>
1117

1218
namespace IE = InferenceEngine;
1319

1420
namespace custom {
21+
// clang-format off
1522
G_API_OP(GTensorToImg, <cv::GMat(cv::GMat)>, "custom.tensorToImg") {
1623
static cv::GMatDesc outMeta(const cv::GMatDesc& in) {
1724
// NB: Input is ND mat.
@@ -30,19 +37,21 @@ G_API_OP(GCalculateMaskRCNNBGMask,
3037
return cv::GMatDesc{CV_8U, 1, in_sz};
3138
}
3239
};
33-
40+
// clang-format on
3441
class NNBGReplacer {
3542
public:
3643
NNBGReplacer() = default;
3744
virtual ~NNBGReplacer() = default;
3845
NNBGReplacer(const std::string& model_path);
3946
virtual cv::GMat replace(cv::GMat, const cv::Size&, cv::GMat) = 0;
40-
const std::string& getName() { return m_tag; }
47+
const std::string& getName() {
48+
return m_tag;
49+
}
4150

4251
protected:
43-
IE::CNNNetwork m_cnn_network;
44-
std::string m_tag;
45-
IE::InputsDataMap m_inputs;
52+
IE::CNNNetwork m_cnn_network;
53+
std::string m_tag;
54+
IE::InputsDataMap m_inputs;
4655
IE::OutputsDataMap m_outputs;
4756
};
4857

@@ -70,4 +79,4 @@ class BGMattingReplacer : public NNBGReplacer {
7079

7180
cv::gapi::GKernelPackage kernels();
7281

73-
} // namespace custom
82+
} // namespace custom

demos/background_subtraction_demo/cpp_gapi/main.cpp

+91-51
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,58 @@
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-
3155
namespace 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

5074
static 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 << "\tFPS: " << 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

Comments
 (0)