Skip to content

Commit

Permalink
Port G-API demos to API2.0 - background_subtraction_demo/cpp_gapi
Browse files Browse the repository at this point in the history
  • Loading branch information
DariaMityagina committed Apr 29, 2024
1 parent 9c6d95a commit 0049139
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 53 deletions.
24 changes: 13 additions & 11 deletions .github/workflows/cpp_gapi-demos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ jobs:
path: ${{ github.workspace }}/cache
key: unused
restore-keys: ubuntu20_2023.2.0.13089.cfd42bd2cb0-
- if: ${{ !steps.cache.outputs.cache-hit }}
uses: actions/checkout@v3
with:
repository: opencv/opencv
path: cache/opencv
- if: ${{ !steps.cache.outputs.cache-hit }}
name: Compile OpenCV
- name: Clone and checkout OpenCV
run: |
cd cache
mkdir opencv-with-fix
cd opencv-with-fix
git clone https://github.com/DariaMityagina/opencv.git
cd opencv
git checkout icv/dm/govbackend-partial-shape-support
- name: Compile OpenCV
run: |
rm -rf cache/opencv/.git/ # Minimize cache
mkdir cache/opencv/build
cd cache/opencv/build
cd cache
mkdir opencv-with-fix/opencv/build
cd opencv-with-fix/opencv/build
cmake -DCMAKE_BUILD_TYPE=Release -DWITH_INF_ENGINE=y -DOpenVINO_DIR=$GITHUB_WORKSPACE/ov/runtime/cmake/ -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_LINKER_LAUNCHER=ccache -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_C_LINKER_LAUNCHER=ccache -DBUILD_TESTS=y -DVIDEOIO_ENABLE_PLUGINS=y -DBUILD_PERF_TESTS=n -DBUILD_EXAMPLES=n -DBUILD_opencv_apps=y -DWITH_OPENCL=n -DWITH_OPENCLAMDBLAS=n -DWITH_GSTREAMER=n -DWITH_V4L=ON -DWITH_LIBV4L=ON -DWITH_OPENCLAMDFFT=n -DWITH_VA=n -DWITH_VA_INTEL=n -DWITH_PROTOBUF=n -DBUILD_PROTOBUF=n -DBUILD_JAVA=n -DBUILD_opencv_java_bindings_generator=n -DBUILD_opencv_python2=n -DBUILD_opencv_python3=n -DWITH_IMGCODEC_HDR=y -DWITH_IMGCODEC_SUNRASTER=y -DWITH_IMGCODEC_PXM=y -DWITH_IMGCODEC_PFM=y -DWITH_PNG=y -DWITH_TIFF=n -DWITH_WEBP=n -DWITH_OPENJPEG=n -DWITH_JASPER=n -DWITH_OPENEXR=n -DBUILD_opencv_dnn=n -DBUILD_opencv_features2d=n -DBUILD_opencv_flann=n -DWITH_TBB=n -DBUILD_INFO_SKIP_EXTRA_MODULES=n -DBUILD_JASPER=n -DBUILD_PNG=n -DBUILD_OPENEXR=n -DBUILD_WEBP=n -DBUILD_ZLIB=n -DWITH_CUDA=n -DWITH_EIGEN=n -DWITH_GPHOTO2=n -DOPENCV_GAPI_GSTREAMER=n -DWITH_LAPACK=n -DWITH_MATLAB=n -DWITH_MFX=n -DWITH_QUIRC=n -DWITH_VTK=n -DINSTALL_PDB=n -DINSTALL_TESTS=n -DINSTALL_C_EXAMPLES=n -DINSTALL_PYTHON_EXAMPLES=n -DOPENCV_GENERATE_SETUPVARS=n -DWITH_1394=n -DWITH_FFMPEG=y -DWITH_GTK_2_X=y -DBUILD_JPEG=y -DWITH_IPP=y -DENABLE_CONFIG_VERIFICATION=y -DBUILD_LIST=core,gapi,highgui,imgcodecs,imgproc,videoio,video ..
cmake --build . -j $((`nproc`*2+2))
- name: build_demos.sh
run: |
source ov/setupvars.sh
OpenCV_DIR=$GITHUB_WORKSPACE/cache/opencv/build CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CXX_LINKER_LAUNCHER=ccache ./demos/build_demos.sh --build_dir=build # TODO: add CMAKE_CXX_FLAGS=-Werror after background_subtraction_demo/cpp_gapi is updated to ov2.0
OpenCV_DIR=$GITHUB_WORKSPACE/cache/opencv-with-fix/opencv/build CMAKE_CXX_COMPILER_LAUNCHER=ccache CMAKE_CXX_LINKER_LAUNCHER=ccache ./demos/build_demos.sh --build_dir=build # TODO: add CMAKE_CXX_FLAGS=-Werror after background_subtraction_demo/cpp_gapi is updated to ov2.0
- uses: actions/setup-python@v4
with:
python-version: 3.11
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
8 changes: 5 additions & 3 deletions demos/background_subtraction_demo/cpp_gapi/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <opencv2/gapi/gstreaming.hpp>
#include <opencv2/gapi/imgproc.hpp>
#include <opencv2/gapi/infer.hpp>
#include <opencv2/gapi/infer/ie.hpp>
#include <opencv2/gapi/infer/ov.hpp>
#include <opencv2/gapi/own/assert.hpp>
#include <opencv2/gapi/streaming/source.hpp>
#include <opencv2/gapi/util/optional.hpp>
Expand Down Expand Up @@ -147,13 +147,13 @@ int main(int argc, char* argv[]) {
auto config = ConfigFactory::getUserConfig(FLAGS_d, FLAGS_nireq, FLAGS_nstreams, FLAGS_nthreads);
// clang-format off
const auto net =
cv::gapi::ie::Params<cv::gapi::Generic>{
cv::gapi::ov::Params<cv::gapi::Generic>{
model->getName(),
FLAGS_m, // path to topology IR
fileNameNoExt(FLAGS_m) + ".bin", // path to weights
FLAGS_d // device specifier
}.cfgNumRequests(config.maxAsyncRequests)
.pluginConfig(config.getLegacyConfig());
.cfgPluginConfig(config.getLegacyConfig());
// clang-format on

slog::info << "The background matting model " << FLAGS_m << " is loaded to " << FLAGS_d << " device."
Expand Down 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
25 changes: 14 additions & 11 deletions demos/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,18 @@ def single_option_cases(key, *args):


DEMOS = [
# CppDemo(name='background_subtraction_demo', device_keys=['-d'], implementation='cpp_gapi', test_cases=combine_cases(
# TestCase(options={'--no_show': None, '-at': 'maskrcnn',
# **MONITORS,
# '-i': DataPatternArg('coco128-subset-480x640x3'),
# }),
# single_option_cases('-m',
# ModelArg('instance-segmentation-person-0007'),
# ModelArg('instance-segmentation-security-0091')),
# )),
CppDemo(name='background_subtraction_demo', device_keys=['-d'], implementation='cpp_gapi', test_cases=combine_cases(
TestCase(options={'--no_show': None, '-at': 'maskrcnn',
**MONITORS,
}),
single_option_cases('-m',
ModelArg('instance-segmentation-person-0007'),
ModelArg('instance-segmentation-security-0091')),
single_option_cases(
'-i',
DataPatternArg('coco128-every-480x640x3'),
str('video.mp4')),
)),

CppDemo('classification_benchmark_demo', 'cpp_gapi', test_cases=combine_cases(
single_option_cases(
Expand Down Expand Up @@ -251,8 +254,8 @@ def single_option_cases(key, *args):
ModelArg('face-detection-retail-0004')),
single_option_cases(
'-i',
str('video.mp4'),
DataPatternArg('coco128-every-480x640x3')),
DataPatternArg('coco128-every-480x640x3'),
str('video.mp4')),
)),

# TODO: https://github.com/DariaMityagina is to fix the demo
Expand Down

0 comments on commit 0049139

Please sign in to comment.