Skip to content

Commit

Permalink
Port G-API demos to API2.0 - background_subtraction_demo/cpp_gapi - r…
Browse files Browse the repository at this point in the history
…ebase and port demo to OV20 (custom_kernels)
  • Loading branch information
DariaMityagina committed Apr 29, 2024
1 parent fcca470 commit 23c8a75
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cpp_gapi-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions demos/background_subtraction_demo/cpp_gapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
#include <string>
#include <vector>

#include <cpp/ie_cnn_network.h>
#include <ie_allocator.hpp>
#include <ie_common.h>
#include <ie_input_info.hpp>
#include <openvino/openvino.hpp>

#include <opencv2/core.hpp>
#include <opencv2/gapi/gkernel.hpp>
#include <opencv2/gapi/gmat.hpp>

namespace IE = InferenceEngine;

namespace custom {
// clang-format off
G_API_OP(GTensorToImg, <cv::GMat(cv::GMat)>, "custom.tensorToImg") {
Expand Down Expand Up @@ -49,10 +45,10 @@ class NNBGReplacer {
}

protected:
IE::CNNNetwork m_cnn_network;
std::shared_ptr<const ov::Model> model;
std::string m_tag;
IE::InputsDataMap m_inputs;
IE::OutputsDataMap m_outputs;
std::vector<ov::Output<const ov::Node>> m_inputs;
std::vector<ov::Output<const ov::Node>> m_outputs;
};

class MaskRCNNBGReplacer : public NNBGReplacer {
Expand Down
2 changes: 2 additions & 0 deletions demos/background_subtraction_demo/cpp_gapi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 11 additions & 14 deletions demos/background_subtraction_demo/cpp_gapi/src/custom_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include <stdexcept>
#include <utility>

#include <ie_core.hpp>
#include <ie_data.h>
#include <ie_layouts.h>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/cpu/gcpukernel.hpp>
#include <opencv2/gapi/gscalar.hpp>
Expand Down Expand Up @@ -137,26 +134,26 @@ 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;
}

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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
7 changes: 2 additions & 5 deletions demos/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 23c8a75

Please sign in to comment.