predict_collector.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. By downloading, copying, installing or using the software you agree to this license.
  3. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
  9. Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  10. Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
  11. Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  12. Copyright (C) 2015, OpenCV Foundation, all rights reserved.
  13. Copyright (C) 2015, Itseez Inc., all rights reserved.
  14. Third party copyrights are property of their respective owners.
  15. Redistribution and use in source and binary forms, with or without modification,
  16. are permitted provided that the following conditions are met:
  17. * Redistributions of source code must retain the above copyright notice,
  18. this list of conditions and the following disclaimer.
  19. * Redistributions in binary form must reproduce the above copyright notice,
  20. this list of conditions and the following disclaimer in the documentation
  21. and/or other materials provided with the distribution.
  22. * Neither the names of the copyright holders nor the names of the contributors
  23. may be used to endorse or promote products derived from this software
  24. without specific prior written permission.
  25. This software is provided by the copyright holders and contributors "as is" and
  26. any express or implied warranties, including, but not limited to, the implied
  27. warranties of merchantability and fitness for a particular purpose are disclaimed.
  28. In no event shall copyright holders or contributors be liable for any direct,
  29. indirect, incidental, special, exemplary, or consequential damages
  30. (including, but not limited to, procurement of substitute goods or services;
  31. loss of use, data, or profits; or business interruption) however caused
  32. and on any theory of liability, whether in contract, strict liability,
  33. or tort (including negligence or otherwise) arising in any way out of
  34. the use of this software, even if advised of the possibility of such damage.
  35. */
  36. #ifndef __OPENCV_PREDICT_COLLECTOR_HPP__
  37. #define __OPENCV_PREDICT_COLLECTOR_HPP__
  38. #include <vector>
  39. #include <map>
  40. #include <utility>
  41. #include <cfloat>
  42. #include "opencv2/core/base.hpp"
  43. namespace cv {
  44. namespace face {
  45. //! @addtogroup face
  46. //! @{
  47. /** @brief Abstract base class for all strategies of prediction result handling
  48. */
  49. class CV_EXPORTS_W PredictCollector
  50. {
  51. public:
  52. virtual ~PredictCollector() {}
  53. /** @brief Interface method called by face recognizer before results processing
  54. @param size total size of prediction evaluation that recognizer could perform
  55. */
  56. virtual void init(size_t size) { CV_UNUSED(size); }
  57. /** @brief Interface method called by face recognizer for each result
  58. @param label current prediction label
  59. @param dist current prediction distance (confidence)
  60. */
  61. virtual bool collect(int label, double dist) = 0;
  62. };
  63. /** @brief Default predict collector
  64. Trace minimal distance with treshhold checking (that is default behavior for most predict logic)
  65. */
  66. class CV_EXPORTS_W StandardCollector : public PredictCollector
  67. {
  68. public:
  69. struct PredictResult
  70. {
  71. int label;
  72. double distance;
  73. PredictResult(int label_ = -1, double distance_ = DBL_MAX) : label(label_), distance(distance_) {}
  74. };
  75. protected:
  76. double threshold;
  77. PredictResult minRes;
  78. std::vector<PredictResult> data;
  79. public:
  80. /** @brief Constructor
  81. @param threshold_ set threshold
  82. */
  83. StandardCollector(double threshold_ = DBL_MAX);
  84. /** @brief overloaded interface method */
  85. void init(size_t size) CV_OVERRIDE;
  86. /** @brief overloaded interface method */
  87. bool collect(int label, double dist) CV_OVERRIDE;
  88. /** @brief Returns label with minimal distance */
  89. CV_WRAP int getMinLabel() const;
  90. /** @brief Returns minimal distance value */
  91. CV_WRAP double getMinDist() const;
  92. /** @brief Return results as vector
  93. @param sorted If set, results will be sorted by distance
  94. Each values is a pair of label and distance.
  95. */
  96. CV_WRAP std::vector< std::pair<int, double> > getResults(bool sorted = false) const;
  97. /** @brief Return results as map
  98. Labels are keys, values are minimal distances
  99. */
  100. std::map<int, double> getResultsMap() const;
  101. /** @brief Static constructor
  102. @param threshold set threshold
  103. */
  104. CV_WRAP static Ptr<StandardCollector> create(double threshold = DBL_MAX);
  105. };
  106. //! @}
  107. }
  108. }
  109. #endif