Skip to content

Commit a63cee2

Browse files
author
Maxim Pashchenkov
authored
Merge pull request opencv#18287 from mpashchenkov:mp/ocv-gapi-blue-branch
[G-API]: Add four kernels to parse NN outputs & provide information in Streaming scenarios * Kernels from GL "blue" branch, acc and perf tests * Code cleanup * Output fix * Comment fix * Added new file for parsers, stylistic corrections * Added end line * Namespace fix * Code cleanup * nnparsers.hpp moved to gapi/infer/, nnparsers -> parsers * Removed cv:: from parsers.hpp
1 parent 830d8d6 commit a63cee2

16 files changed

+1423
-1
lines changed

modules/gapi/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ set(gapi_srcs
7171
src/api/kernels_core.cpp
7272
src/api/kernels_imgproc.cpp
7373
src/api/kernels_video.cpp
74+
src/api/kernels_nnparsers.cpp
7475
src/api/render.cpp
7576
src/api/render_ocv.cpp
7677
src/api/ginfer.cpp
@@ -105,6 +106,7 @@ set(gapi_srcs
105106
src/backends/cpu/gcpuimgproc.cpp
106107
src/backends/cpu/gcpuvideo.cpp
107108
src/backends/cpu/gcpucore.cpp
109+
src/backends/cpu/gnnparsers.cpp
108110

109111
# Fluid Backend (also built-in, FIXME:move away)
110112
src/backends/fluid/gfluidbuffer.cpp

modules/gapi/include/opencv2/gapi/core.hpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace core {
3131
using GMat2 = std::tuple<GMat,GMat>;
3232
using GMat3 = std::tuple<GMat,GMat,GMat>; // FIXME: how to avoid this?
3333
using GMat4 = std::tuple<GMat,GMat,GMat,GMat>;
34-
using GMatScalar = std::tuple<GMat, GScalar>;
34+
using GMatScalar = std::tuple<GMat, GScalar>;
3535

3636
G_TYPED_KERNEL(GAdd, <GMat(GMat, GMat, int)>, "org.opencv.core.math.add") {
3737
static GMatDesc outMeta(GMatDesc a, GMatDesc b, int ddepth) {
@@ -501,6 +501,18 @@ namespace core {
501501
return in.withType(in.depth, in.chan).withSize(dsize);
502502
}
503503
};
504+
505+
G_TYPED_KERNEL(GSize, <GOpaque<Size>(GMat)>, "org.opencv.core.size") {
506+
static GOpaqueDesc outMeta(const GMatDesc&) {
507+
return empty_gopaque_desc();
508+
}
509+
};
510+
511+
G_TYPED_KERNEL(GSizeR, <GOpaque<Size>(GOpaque<Rect>)>, "org.opencv.core.sizeR") {
512+
static GOpaqueDesc outMeta(const GOpaqueDesc&) {
513+
return empty_gopaque_desc();
514+
}
515+
};
504516
}
505517

506518
//! @addtogroup gapi_math
@@ -1720,6 +1732,24 @@ GAPI_EXPORTS GMat warpAffine(const GMat& src, const Mat& M, const Size& dsize, i
17201732
int borderMode = cv::BORDER_CONSTANT, const Scalar& borderValue = Scalar());
17211733
//! @} gapi_transform
17221734

1735+
/** @brief Gets dimensions from Mat.
1736+
1737+
@note Function textual ID is "org.opencv.core.size"
1738+
1739+
@param src Input tensor
1740+
@return Size (tensor dimensions).
1741+
*/
1742+
GAPI_EXPORTS GOpaque<Size> size(const GMat& src);
1743+
1744+
/** @overload
1745+
Gets dimensions from rectangle.
1746+
1747+
@note Function textual ID is "org.opencv.core.sizeR"
1748+
1749+
@param r Input rectangle.
1750+
@return Size (rectangle dimensions).
1751+
*/
1752+
GAPI_EXPORTS GOpaque<Size> size(const GOpaque<Rect>& r);
17231753
} //namespace gapi
17241754
} //namespace cv
17251755

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
//
5+
// Copyright (C) 2020 Intel Corporation
6+
7+
8+
#ifndef OPENCV_GAPI_PARSERS_HPP
9+
#define OPENCV_GAPI_PARSERS_HPP
10+
11+
#include <utility> // std::tuple
12+
13+
#include <opencv2/gapi/gmat.hpp>
14+
#include <opencv2/gapi/gkernel.hpp>
15+
16+
namespace cv { namespace gapi {
17+
namespace nn {
18+
namespace parsers {
19+
using GRects = GArray<Rect>;
20+
using GDetections = std::tuple<GArray<Rect>, GArray<int>>;
21+
22+
G_TYPED_KERNEL(GParseSSDBL, <GDetections(GMat, GOpaque<Size>, float, int)>,
23+
"org.opencv.nn.parsers.parseSSD_BL") {
24+
static std::tuple<GArrayDesc,GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&, float, int) {
25+
return std::make_tuple(empty_array_desc(), empty_array_desc());
26+
}
27+
};
28+
29+
G_TYPED_KERNEL(GParseSSD, <GRects(GMat, GOpaque<Size>, float, bool, bool)>,
30+
"org.opencv.nn.parsers.parseSSD") {
31+
static GArrayDesc outMeta(const GMatDesc&, const GOpaqueDesc&, float, bool, bool) {
32+
return empty_array_desc();
33+
}
34+
};
35+
36+
G_TYPED_KERNEL(GParseYolo, <GDetections(GMat, GOpaque<Size>, float, float, std::vector<float>)>,
37+
"org.opencv.nn.parsers.parseYolo") {
38+
static std::tuple<GArrayDesc, GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&,
39+
float, float, const std::vector<float>&) {
40+
return std::make_tuple(empty_array_desc(), empty_array_desc());
41+
}
42+
static const std::vector<float>& defaultAnchors() {
43+
static std::vector<float> anchors {
44+
0.57273f, 0.677385f, 1.87446f, 2.06253f, 3.33843f, 5.47434f, 7.88282f, 3.52778f, 9.77052f, 9.16828f
45+
};
46+
return anchors;
47+
}
48+
};
49+
} // namespace parsers
50+
} // namespace nn
51+
52+
/** @brief Parses output of SSD network.
53+
54+
Extracts detection information (box, confidence, label) from SSD output and
55+
filters it by given confidence and label.
56+
57+
@note Function textual ID is "org.opencv.nn.parsers.parseSSD_BL"
58+
59+
@param in Input CV_32F tensor with {1,1,N,7} dimensions.
60+
@param inSz Size to project detected boxes to (size of the input image).
61+
@param confidenceThreshold If confidence of the
62+
detection is smaller than confidence threshold, detection is rejected.
63+
@param filterLabel If provided (!= -1), only detections with
64+
given label will get to the output.
65+
@return a tuple with a vector of detected boxes and a vector of appropriate labels.
66+
*/
67+
GAPI_EXPORTS std::tuple<GArray<Rect>, GArray<int>> parseSSD(const GMat& in,
68+
const GOpaque<Size>& inSz,
69+
const float confidenceThreshold = 0.5f,
70+
const int filterLabel = -1);
71+
72+
/** @overload
73+
Extracts detection information (box, confidence) from SSD output and
74+
filters it by given confidence and by going out of bounds.
75+
76+
@note Function textual ID is "org.opencv.nn.parsers.parseSSD"
77+
78+
@param in Input CV_32F tensor with {1,1,N,7} dimensions.
79+
@param inSz Size to project detected boxes to (size of the input image).
80+
@param confidenceThreshold If confidence of the
81+
detection is smaller than confidence threshold, detection is rejected.
82+
@param alignmentToSquare If provided true, bounding boxes are extended to squares.
83+
The center of the rectangle remains unchanged, the side of the square is
84+
the larger side of the rectangle.
85+
@param filterOutOfBounds If provided true, out-of-frame boxes are filtered.
86+
@return a vector of detected bounding boxes.
87+
*/
88+
GAPI_EXPORTS GArray<Rect> parseSSD(const GMat& in,
89+
const GOpaque<Size>& inSz,
90+
const float confidenceThreshold = 0.5f,
91+
const bool alignmentToSquare = false,
92+
const bool filterOutOfBounds = false);
93+
94+
/** @brief Parses output of Yolo network.
95+
96+
Extracts detection information (box, confidence, label) from Yolo output,
97+
filters it by given confidence and performs non-maximum supression for overlapping boxes.
98+
99+
@note Function textual ID is "org.opencv.nn.parsers.parseYolo"
100+
101+
@param in Input CV_32F tensor with {1,13,13,N} dimensions, N should satisfy:
102+
\f[\texttt{N} = (\texttt{num_classes} + \texttt{5}) * \texttt{5},\f]
103+
where num_classes - a number of classes Yolo network was trained with.
104+
@param inSz Size to project detected boxes to (size of the input image).
105+
@param confidenceThreshold If confidence of the
106+
detection is smaller than confidence threshold, detection is rejected.
107+
@param nmsThreshold Non-maximum supression threshold which controls minimum
108+
relative box intersection area required for rejecting the box with a smaller confidence.
109+
If 1.f, nms is not performed and no boxes are rejected.
110+
@param anchors Anchors Yolo network was trained with.
111+
@note The default anchor values are taken from openvinotoolkit docs:
112+
https://docs.openvinotoolkit.org/latest/omz_models_intel_yolo_v2_tiny_vehicle_detection_0001_description_yolo_v2_tiny_vehicle_detection_0001.html#output.
113+
@return a tuple with a vector of detected boxes and a vector of appropriate labels.
114+
*/
115+
GAPI_EXPORTS std::tuple<GArray<Rect>, GArray<int>> parseYolo(const GMat& in,
116+
const GOpaque<Size>& inSz,
117+
const float confidenceThreshold = 0.5f,
118+
const float nmsThreshold = 0.5f,
119+
const std::vector<float>& anchors
120+
= nn::parsers::GParseYolo::defaultAnchors());
121+
122+
} // namespace gapi
123+
} // namespace cv
124+
125+
#endif // OPENCV_GAPI_PARSERS_HPP

modules/gapi/perf/common/gapi_core_perf_tests.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
#include "../../test/common/gapi_tests_common.hpp"
13+
#include "../../test/common/gapi_parsers_tests_common.hpp"
1314
#include <opencv2/gapi/core.hpp>
1415

1516
namespace opencv_test
@@ -73,5 +74,10 @@ namespace opencv_test
7374
class ConvertToPerfTest : public TestPerfParams<tuple<MatType, int, cv::Size, cv::GCompileArgs>> {};
7475
class ResizePerfTest : public TestPerfParams<tuple<compare_f, MatType, int, cv::Size, cv::Size, cv::GCompileArgs>> {};
7576
class ResizeFxFyPerfTest : public TestPerfParams<tuple<compare_f, MatType, int, cv::Size, double, double, cv::GCompileArgs>> {};
77+
class ParseSSDBLPerfTest : public TestPerfParams<tuple<cv::Size, float, int, cv::GCompileArgs>>, public ParserSSDTest {};
78+
class ParseSSDPerfTest : public TestPerfParams<tuple<cv::Size, float, bool, bool, cv::GCompileArgs>>, public ParserSSDTest {};
79+
class ParseYoloPerfTest : public TestPerfParams<tuple<cv::Size, float, float, int, cv::GCompileArgs>>, public ParserYoloTest {};
80+
class SizePerfTest : public TestPerfParams<tuple<MatType, cv::Size, cv::GCompileArgs>> {};
81+
class SizeRPerfTest : public TestPerfParams<tuple<cv::Size, cv::GCompileArgs>> {};
7682
}
7783
#endif // OPENCV_GAPI_CORE_PERF_TESTS_HPP

0 commit comments

Comments
 (0)