From 23c8a75adc0f2920adb799b609e56804d21058b3 Mon Sep 17 00:00:00 2001 From: DariaMityagina Date: Mon, 29 Apr 2024 12:23:22 +0000 Subject: [PATCH] Port G-API demos to API2.0 - background_subtraction_demo/cpp_gapi - rebase and port demo to OV20 (custom_kernels) --- .github/workflows/cpp_gapi-demos.yml | 2 +- .../cpp_gapi/CMakeLists.txt | 5 ---- .../cpp_gapi/include/custom_kernels.hpp | 14 ++++------- .../cpp_gapi/main.cpp | 2 ++ .../cpp_gapi/src/custom_kernels.cpp | 25 ++++++++----------- demos/tests/cases.py | 7 ++---- 6 files changed, 21 insertions(+), 34 deletions(-) diff --git a/.github/workflows/cpp_gapi-demos.yml b/.github/workflows/cpp_gapi-demos.yml index c0628ad2b96..4faa2bf2b43 100644 --- a/.github/workflows/cpp_gapi-demos.yml +++ b/.github/workflows/cpp_gapi-demos.yml @@ -35,7 +35,7 @@ jobs: cd opencv-with-fix git clone https://github.com/DariaMityagina/opencv.git cd opencv - git checkout icv/dm/add-media-frame-support-to-govbackend + git checkout icv/dm/govbackend-partial-shape-support - name: Compile OpenCV run: | cd cache diff --git a/demos/background_subtraction_demo/cpp_gapi/CMakeLists.txt b/demos/background_subtraction_demo/cpp_gapi/CMakeLists.txt index 5f12122c89d..dbba68ba05d 100644 --- a/demos/background_subtraction_demo/cpp_gapi/CMakeLists.txt +++ b/demos/background_subtraction_demo/cpp_gapi/CMakeLists.txt @@ -7,11 +7,6 @@ if (NOT TARGET utils_gapi) return() endif() -if(OpenVINO_VERSION_MAJOR VERSION_GREATER_EQUAL 2024) - message(WARNING "background_subtraction_demo_gapi skipped. G-API is not compatible with OpenVINO 2024.0.") - return() -endif() - file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp) diff --git a/demos/background_subtraction_demo/cpp_gapi/include/custom_kernels.hpp b/demos/background_subtraction_demo/cpp_gapi/include/custom_kernels.hpp index 098f291b678..be3eac857b6 100644 --- a/demos/background_subtraction_demo/cpp_gapi/include/custom_kernels.hpp +++ b/demos/background_subtraction_demo/cpp_gapi/include/custom_kernels.hpp @@ -7,16 +7,12 @@ #include #include -#include -#include -#include -#include +#include + #include #include #include -namespace IE = InferenceEngine; - namespace custom { // clang-format off G_API_OP(GTensorToImg, , "custom.tensorToImg") { @@ -49,10 +45,10 @@ class NNBGReplacer { } protected: - IE::CNNNetwork m_cnn_network; + std::shared_ptr model; std::string m_tag; - IE::InputsDataMap m_inputs; - IE::OutputsDataMap m_outputs; + std::vector> m_inputs; + std::vector> m_outputs; }; class MaskRCNNBGReplacer : public NNBGReplacer { diff --git a/demos/background_subtraction_demo/cpp_gapi/main.cpp b/demos/background_subtraction_demo/cpp_gapi/main.cpp index a8041c1a57a..10a74676679 100644 --- a/demos/background_subtraction_demo/cpp_gapi/main.cpp +++ b/demos/background_subtraction_demo/cpp_gapi/main.cpp @@ -241,6 +241,8 @@ int main(int argc, char* argv[]) { } else { presenter.handleKey(key); } + } else { + cv::imwrite("./background-substr-out.jpg", output); } } slog::info << "Metrics report:" << slog::endl; diff --git a/demos/background_subtraction_demo/cpp_gapi/src/custom_kernels.cpp b/demos/background_subtraction_demo/cpp_gapi/src/custom_kernels.cpp index 952c50828f2..25d8d6a8797 100644 --- a/demos/background_subtraction_demo/cpp_gapi/src/custom_kernels.cpp +++ b/demos/background_subtraction_demo/cpp_gapi/src/custom_kernels.cpp @@ -10,9 +10,6 @@ #include #include -#include -#include -#include #include #include #include @@ -137,16 +134,16 @@ GAPI_OCV_KERNEL(OCVCalculateMaskRCNNBGMask, custom::GCalculateMaskRCNNBGMask) { // clang-format on custom::NNBGReplacer::NNBGReplacer(const std::string& model_path) { - IE::Core core; - m_cnn_network = core.ReadNetwork(model_path); - m_tag = m_cnn_network.getName(); - m_inputs = m_cnn_network.getInputsInfo(); - m_outputs = m_cnn_network.getOutputsInfo(); + ov::Core core; + model = core.read_model(model_path); + m_tag = model->get_name(); + m_inputs = model->inputs(); + m_outputs = model->outputs(); } custom::MaskRCNNBGReplacer::MaskRCNNBGReplacer(const std::string& model_path) : custom::NNBGReplacer(model_path) { for (const auto& p : m_outputs) { - const auto& layer_name = p.first; + const auto& layer_name = p.get_any_name(); if (layer_name.rfind("TopK") != std::string::npos) { continue; } @@ -154,9 +151,9 @@ custom::MaskRCNNBGReplacer::MaskRCNNBGReplacer(const std::string& model_path) : if (m_inputs.size() != 1) { throw std::logic_error("Supported only single input MaskRCNN models!"); } - m_input_name = m_inputs.begin()->first; + m_input_name = m_inputs[0].get_any_name(); - const auto dims_size = p.second->getTensorDesc().getDims().size(); + const auto dims_size = p.get_partial_shape().size(); if (dims_size == 1) { m_labels_name = layer_name; } else if (dims_size == 2) { @@ -177,7 +174,7 @@ cv::GMat custom::MaskRCNNBGReplacer::replace(cv::GFrame in, cv::GMat bgr, const auto boxes = outputs.at(m_boxes_name); auto masks = outputs.at(m_masks_name); - const auto& dims = m_inputs.at(m_input_name)->getTensorDesc().getDims(); + const auto& dims = model->input(m_input_name).get_partial_shape().get_max_shape(); GAPI_Assert(dims.size() == 4u); auto mask = custom::GCalculateMaskRCNNBGMask::on(in_size, cv::Size(dims[3], dims[2]), labels, boxes, masks); auto mask3ch = cv::gapi::medianBlur(cv::gapi::merge3(mask, mask, mask), 11); @@ -188,12 +185,12 @@ custom::BGMattingReplacer::BGMattingReplacer(const std::string& model_path) : NN if (m_inputs.size() != 1) { throw std::logic_error("Supported only single input background matting models!"); } - m_input_name = m_inputs.begin()->first; + m_input_name = m_inputs[0].get_any_name(); if (m_outputs.size() != 1) { throw std::logic_error("Supported only single output background matting models!"); } - m_output_name = m_outputs.begin()->first; + m_output_name = m_outputs[0].get_any_name(); } cv::GMat custom::BGMattingReplacer::replace(cv::GFrame in, cv::GMat bgr, const cv::Size& in_size, cv::GMat background) { diff --git a/demos/tests/cases.py b/demos/tests/cases.py index cf21f9ce574..fe1e7e62273 100644 --- a/demos/tests/cases.py +++ b/demos/tests/cases.py @@ -174,12 +174,9 @@ def single_option_cases(key, *args): TestCase(options={'--no_show': None, '-at': 'maskrcnn', **MONITORS, }), - # TODO: enable this case once E#111291 is fixed - # single_option_cases('-m', - # ModelArg('instance-segmentation-person-0007'), - # ModelArg('instance-segmentation-security-0091')), single_option_cases('-m', - ModelArg('instance-segmentation-person-0007')), + ModelArg('instance-segmentation-person-0007'), + ModelArg('instance-segmentation-security-0091')), single_option_cases( '-i', DataPatternArg('coco128-every-480x640x3'),