|
| 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 |
0 commit comments