parsers.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef OPENCV_GAPI_PARSERS_HPP
  7. #define OPENCV_GAPI_PARSERS_HPP
  8. #include <utility> // std::tuple
  9. #include <opencv2/gapi/gmat.hpp>
  10. #include <opencv2/gapi/gkernel.hpp>
  11. namespace cv { namespace gapi {
  12. namespace nn {
  13. namespace parsers {
  14. using GRects = GArray<Rect>;
  15. using GDetections = std::tuple<GArray<Rect>, GArray<int>>;
  16. G_TYPED_KERNEL(GParseSSDBL, <GDetections(GMat, GOpaque<Size>, float, int)>,
  17. "org.opencv.nn.parsers.parseSSD_BL") {
  18. static std::tuple<GArrayDesc,GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&, float, int) {
  19. return std::make_tuple(empty_array_desc(), empty_array_desc());
  20. }
  21. };
  22. G_TYPED_KERNEL(GParseSSD, <GRects(GMat, GOpaque<Size>, float, bool, bool)>,
  23. "org.opencv.nn.parsers.parseSSD") {
  24. static GArrayDesc outMeta(const GMatDesc&, const GOpaqueDesc&, float, bool, bool) {
  25. return empty_array_desc();
  26. }
  27. };
  28. G_TYPED_KERNEL(GParseYolo, <GDetections(GMat, GOpaque<Size>, float, float, std::vector<float>)>,
  29. "org.opencv.nn.parsers.parseYolo") {
  30. static std::tuple<GArrayDesc, GArrayDesc> outMeta(const GMatDesc&, const GOpaqueDesc&,
  31. float, float, const std::vector<float>&) {
  32. return std::make_tuple(empty_array_desc(), empty_array_desc());
  33. }
  34. static const std::vector<float>& defaultAnchors() {
  35. static std::vector<float> anchors {
  36. 0.57273f, 0.677385f, 1.87446f, 2.06253f, 3.33843f, 5.47434f, 7.88282f, 3.52778f, 9.77052f, 9.16828f
  37. };
  38. return anchors;
  39. }
  40. };
  41. } // namespace parsers
  42. } // namespace nn
  43. /** @brief Parses output of SSD network.
  44. Extracts detection information (box, confidence, label) from SSD output and
  45. filters it by given confidence and label.
  46. @note Function textual ID is "org.opencv.nn.parsers.parseSSD_BL"
  47. @param in Input CV_32F tensor with {1,1,N,7} dimensions.
  48. @param inSz Size to project detected boxes to (size of the input image).
  49. @param confidenceThreshold If confidence of the
  50. detection is smaller than confidence threshold, detection is rejected.
  51. @param filterLabel If provided (!= -1), only detections with
  52. given label will get to the output.
  53. @return a tuple with a vector of detected boxes and a vector of appropriate labels.
  54. */
  55. GAPI_EXPORTS_W std::tuple<GArray<Rect>, GArray<int>> parseSSD(const GMat& in,
  56. const GOpaque<Size>& inSz,
  57. const float confidenceThreshold = 0.5f,
  58. const int filterLabel = -1);
  59. /** @brief Parses output of SSD network.
  60. Extracts detection information (box, confidence) from SSD output and
  61. filters it by given confidence and by going out of bounds.
  62. @note Function textual ID is "org.opencv.nn.parsers.parseSSD"
  63. @param in Input CV_32F tensor with {1,1,N,7} dimensions.
  64. @param inSz Size to project detected boxes to (size of the input image).
  65. @param confidenceThreshold If confidence of the
  66. detection is smaller than confidence threshold, detection is rejected.
  67. @param alignmentToSquare If provided true, bounding boxes are extended to squares.
  68. The center of the rectangle remains unchanged, the side of the square is
  69. the larger side of the rectangle.
  70. @param filterOutOfBounds If provided true, out-of-frame boxes are filtered.
  71. @return a vector of detected bounding boxes.
  72. */
  73. GAPI_EXPORTS_W GArray<Rect> parseSSD(const GMat& in,
  74. const GOpaque<Size>& inSz,
  75. const float confidenceThreshold,
  76. const bool alignmentToSquare,
  77. const bool filterOutOfBounds);
  78. /** @brief Parses output of Yolo network.
  79. Extracts detection information (box, confidence, label) from Yolo output,
  80. filters it by given confidence and performs non-maximum suppression for overlapping boxes.
  81. @note Function textual ID is "org.opencv.nn.parsers.parseYolo"
  82. @param in Input CV_32F tensor with {1,13,13,N} dimensions, N should satisfy:
  83. \f[\texttt{N} = (\texttt{num_classes} + \texttt{5}) * \texttt{5},\f]
  84. where num_classes - a number of classes Yolo network was trained with.
  85. @param inSz Size to project detected boxes to (size of the input image).
  86. @param confidenceThreshold If confidence of the
  87. detection is smaller than confidence threshold, detection is rejected.
  88. @param nmsThreshold Non-maximum suppression threshold which controls minimum
  89. relative box intersection area required for rejecting the box with a smaller confidence.
  90. If 1.f, nms is not performed and no boxes are rejected.
  91. @param anchors Anchors Yolo network was trained with.
  92. @note The default anchor values are specified for YOLO v2 Tiny as described in Intel Open Model Zoo
  93. <a href="https://github.com/openvinotoolkit/open_model_zoo/blob/master/models/public/yolo-v2-tiny-tf/yolo-v2-tiny-tf.md">documentation</a>.
  94. @return a tuple with a vector of detected boxes and a vector of appropriate labels.
  95. */
  96. GAPI_EXPORTS_W std::tuple<GArray<Rect>, GArray<int>> parseYolo(const GMat& in,
  97. const GOpaque<Size>& inSz,
  98. const float confidenceThreshold = 0.5f,
  99. const float nmsThreshold = 0.5f,
  100. const std::vector<float>& anchors
  101. = nn::parsers::GParseYolo::defaultAnchors());
  102. } // namespace gapi
  103. } // namespace cv
  104. // Reimport parseSSD & parseYolo under their initial namespace
  105. namespace cv {
  106. namespace gapi {
  107. namespace streaming {
  108. using cv::gapi::parseSSD;
  109. using cv::gapi::parseYolo;
  110. } // namespace streaming
  111. } // namespace gapi
  112. } // namespace cv
  113. #endif // OPENCV_GAPI_PARSERS_HPP