From 48903ef96d37ae2b9716268e0975943fe83c194f Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Wed, 20 Apr 2016 18:47:38 +0300 Subject: [PATCH] Added python aliases for face predict overloaded functions --- modules/face/include/opencv2/face.hpp | 5 +- .../opencv2/face/predict_collector.hpp | 280 ++++-------------- modules/face/src/eigen_faces.cpp | 8 +- modules/face/src/facerec.cpp | 8 +- modules/face/src/fisher_faces.cpp | 8 +- modules/face/src/lbph_faces.cpp | 8 +- modules/face/src/map_predict_collector.cpp | 86 ------ .../face/src/mindist_predict_collector.cpp | 76 ----- modules/face/src/predict_collector.cpp | 140 +++------ modules/face/src/stat_predict_collector.cpp | 87 ------ modules/face/src/std_predict_collector.cpp | 65 ---- modules/face/src/topn_predict_collector.cpp | 109 ------- modules/face/src/vector_predict_collector.cpp | 71 ----- 13 files changed, 117 insertions(+), 834 deletions(-) delete mode 100644 modules/face/src/map_predict_collector.cpp delete mode 100644 modules/face/src/mindist_predict_collector.cpp delete mode 100644 modules/face/src/stat_predict_collector.cpp delete mode 100644 modules/face/src/std_predict_collector.cpp delete mode 100644 modules/face/src/topn_predict_collector.cpp delete mode 100644 modules/face/src/vector_predict_collector.cpp diff --git a/modules/face/include/opencv2/face.hpp b/modules/face/include/opencv2/face.hpp index d7237bc94..49990555c 100644 --- a/modules/face/include/opencv2/face.hpp +++ b/modules/face/include/opencv2/face.hpp @@ -256,7 +256,7 @@ public: CV_WRAP virtual void update(InputArrayOfArrays src, InputArray labels); /** @overload */ - CV_WRAP int predict(InputArray src) const; + CV_WRAP_AS(predict_label) int predict(InputArray src) const; /** @brief Predicts a label and associated confidence (e.g. distance) for a given input image. @@ -300,12 +300,11 @@ public: /** @brief - if implemented - send all result of prediction to collector that can be used for somehow custom result handling @param src Sample image to get a prediction from. @param collector User-defined collector object that accepts all results - @param state - optional user-defined state token that should be passed back from FaceRecognizer implementation To implement this method u just have to do same internal cycle as in predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) but not try to get "best@ result, just resend it to caller side with given collector */ - CV_WRAP virtual void predict(InputArray src, Ptr collector, const int state = 0) const = 0; + CV_WRAP_AS(predict_collect) virtual void predict(InputArray src, Ptr collector) const = 0; /** @brief Saves a FaceRecognizer and its model state. diff --git a/modules/face/include/opencv2/face/predict_collector.hpp b/modules/face/include/opencv2/face/predict_collector.hpp index 92de6c1b8..a9f907d5c 100644 --- a/modules/face/include/opencv2/face/predict_collector.hpp +++ b/modules/face/include/opencv2/face/predict_collector.hpp @@ -44,262 +44,84 @@ the use of this software, even if advised of the possibility of such damage. #ifndef __OPENCV_PREDICT_COLLECTOR_HPP__ #define __OPENCV_PREDICT_COLLECTOR_HPP__ -#include -#include + #include #include -#include "opencv2/core/cvdef.h" +#include +#include + #include "opencv2/core/cvstd.hpp" -#undef emit //fix for qt + namespace cv { namespace face { //! @addtogroup face //! @{ /** @brief Abstract base class for all strategies of prediction result handling */ -class CV_EXPORTS_W PredictCollector { -protected: - double _threshold; - int _size; - int _state; - int _excludeLabel; - double _distanceKoef; - double _minthreshold; +class CV_EXPORTS_W PredictCollector +{ public: - /** @brief creates new predict collector with given threshold */ - PredictCollector(double threshold = DBL_MAX) { - _threshold = threshold; - _excludeLabel = 0; - _distanceKoef = 1; - _minthreshold = -1; - } - CV_WRAP virtual ~PredictCollector() {} + virtual ~PredictCollector() {} - /** @brief called once at start of recognition + /** @brief Interface method called by face recognizer before results processing @param size total size of prediction evaluation that recognizer could perform - @param state user defined send-to-back optional value to allow multi-thread, multi-session or aggregation scenarios - */ - CV_WRAP virtual void init(const int size, const int state = 0); - - /** @brief called by recognizer prior to emit to decide if prediction require emiting - @param label current predicted label - @param dist current predicted distance - @param state back send state parameter of prediction session - @return true if prediction is valid and required for emiting - @note can override given label and distance to another values */ - CV_WRAP virtual bool defaultFilter(int* label, double* dist, const int state); - - /** @brief extension point for filter - called if base filter executed */ - CV_WRAP virtual bool filter(int* label, double* dist, const int state); + virtual void init(size_t size) { (void)size; } - /** @brief called with every recognition result + /** @brief Interface method called by face recognizer for each result @param label current prediction label @param dist current prediction distance (confidence) - @param state user defined send-to-back optional value to allow multi-thread, multi-session or aggregation scenarios - @return true if recognizer should proceed prediction , false - if recognizer should terminate prediction - */ - CV_WRAP virtual bool emit(const int label, const double dist, const int state = 0); //not abstract while Python generation require non-abstract class - - /** @brief outer interface method to be called from recognizer - @param label current prediction label - @param dist current prediction distance (confidence) - @param state user defined send-to-back optional value to allow multi-thread, multi-session or aggregation scenarios - @note wraps filter and emit calls, not tended to be overriden - */ - CV_WRAP virtual bool collect(int label, double dist, const int state = 0); - - /** - @brief get size of prediction - ### Description - Is set by recognizer and is amount of all available predicts - So we can use it to perform statistic collectors before prediction of whole set - */ - CV_WRAP virtual int getSize(); - - /** @brief set size of prediction */ - CV_WRAP virtual void setSize(int size); - - /** - @brief get state of prediction - ### Description - State is a custom value assigned for prediction session, 0 if it's no-state session - */ - CV_WRAP virtual int getState(); - - /** @brief set state of prediction */ - CV_WRAP virtual void setState(int state); - - /** - @brief returns currently excluded label, 0 if no set - ### Description - We require to exclude label if we want to test card in train set against others */ - CV_WRAP virtual int getExcludeLabel(); - - /** @brief set exclude label of prediction */ - CV_WRAP virtual void setExcludeLabel(int excludeLabel); - - /** - @brief returns current distance koeficient (applyed to distance in filter stage) - ### Description - It's required if we want to predict with distinct algorithms in one session - so LBPH, Eigen and Fisher distance are different, but we can provide koef for them to translate to - each other (while their distribuition for same train set is close and started from 0) - Default 1 koef means that distance is not corrected - */ - CV_WRAP virtual double getDistanceKoef(); - - /** @brief set exclude label of prediction */ - CV_WRAP virtual void setDistanceKoef(double distanceKoef); - /** - @brief returns current minimal threshold - ### Description - It's required when we must exclude most closed predictions (for example we - search for close but not same faces - usable for mixed set where doubles exists - in train collection) - */ - CV_WRAP virtual double getMinThreshold(); - - /** @brief set minimal threshold for prediction */ - CV_WRAP virtual void setMinThreshold(double minthreshold); - + virtual bool collect(int label, double dist) = 0; }; -/** @brief default predict collector that trace minimal distance with treshhold checking (that is default behavior for most predict logic) -*/ -class CV_EXPORTS_W MinDistancePredictCollector : public PredictCollector { -private: - int _label; - double _dist; -public: - /** @brief creates new MinDistancePredictCollector with given threshold */ - CV_WRAP MinDistancePredictCollector(double threshold = DBL_MAX) : PredictCollector(threshold) { - _label = -1; - _dist = DBL_MAX; - }; - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - CV_WRAP bool filter(int* label, double* dist, const int state); - /** @brief result label, -1 if not found */ - CV_WRAP int getLabel() const; - /** @brief result distance (confidence) DBL_MAX if not found */ - CV_WRAP double getDist() const; - /** @brief factory method to create cv-pointers to MinDistancePredictCollector */ - CV_WRAP static Ptr create(double threshold = DBL_MAX); -}; +/** @brief Default predict collector -/** -@brief Collects top N most close predictions -@note Prevent doubling of same label - if one label is occured twice - most closed distance value will be set +Trace minimal distance with treshhold checking (that is default behavior for most predict logic) */ -class CV_EXPORTS_W TopNPredictCollector : public PredictCollector { -private: - size_t _size; - Ptr > > _idx; +class CV_EXPORTS_W StandardCollector : public PredictCollector +{ public: - CV_WRAP TopNPredictCollector(size_t size = 5, double threshold = DBL_MAX) : PredictCollector(threshold) { - _size = size; - _idx = Ptr > >(new std::list >); + struct PredictResult + { + int label; + double distance; + PredictResult(int label_ = -1, double distance_ = DBL_MAX) : label(label_), distance(distance_) {} }; - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - CV_WRAP bool filter(int* label, double* dist, const int state); - Ptr > > getResult(); - CV_WRAP std::vector > getResultVector(); // pythonable version - CV_WRAP static Ptr create(size_t size = 5, double threshold = DBL_MAX); -}; - - -/** -@brief Collects all predict results to single vector -@note this collector not analyze double labels in emit, it's raw copy of source prediction result, -remember that filter is still applyed so you can use min/max threshold , distanceKoef and excludeLabel -*/ -class CV_EXPORTS_W VectorPredictCollector : public PredictCollector { -private: - Ptr > > _idx; -public: - CV_WRAP static const int DEFAULT_SIZE = 5; // top 5 by default - CV_WRAP VectorPredictCollector(double threshold = DBL_MAX) : PredictCollector(threshold) { - _idx = Ptr > >(new std::vector >); - } - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - Ptr > > getResult(); - CV_WRAP std::vector > getResultVector(); // pythonable version - CV_WRAP static Ptr create(double threshold = DBL_MAX); -}; - - -/** -@brief Collects all predict results to single vector -@note this collector not analyze double labels in emit, it's raw copy of source prediction result, -remember that filter is still applyed so you can use min/max threshold , distanceKoef and excludeLabel -*/ -class CV_EXPORTS_W MapPredictCollector : public PredictCollector { -private: - Ptr > _idx; -public: - CV_WRAP static const int DEFAULT_SIZE = 5; // top 5 by default - CV_WRAP MapPredictCollector(double threshold = DBL_MAX) : PredictCollector(threshold) { - _idx = Ptr >(new std::map); - } - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - Ptr > getResult(); - CV_WRAP std::vector > getResultVector(); // pythonable version - CV_WRAP static Ptr create(double threshold = DBL_MAX); -}; - -/** -@brief Collects basic statistic information about prediction -@note stat predict collector is usefull for determining valid thresholds -on given trained set, additionally it's required to -evaluate unified koefs between algorithms -*/ -class CV_EXPORTS_W StatPredictCollector : public PredictCollector { -private: - double _min; - double _max; - int _count; - double _sum; -public: - CV_WRAP StatPredictCollector(double threshold = DBL_MAX) : PredictCollector(threshold) { - _min = DBL_MAX; - _max = DBL_MIN; - _count = 0; - _sum = 0; - } - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - CV_WRAP double getMin(); - CV_WRAP double getMax(); - CV_WRAP double getSum(); - CV_WRAP int getCount(); - CV_WRAP static Ptr create(double threshold = DBL_MAX); -}; - -/** -@brief evaluates standard deviation of given prediction session over trained set -@note in combine with StatPredictCollector can provide statistically based metrices -for thresholds -*/ -class CV_EXPORTS_W StdPredictCollector : public PredictCollector { -private: - double _avg; - double _n; - double _s; +protected: + double threshold; + PredictResult minRes; + std::vector data; public: - CV_WRAP StdPredictCollector(double threshold = DBL_MAX, double avg = 0) : PredictCollector(threshold) { - _avg = avg; - _n = 0; - _s = 0; - } - CV_WRAP bool emit(const int label, const double dist, const int state = 0); - CV_WRAP double getResult(); - CV_WRAP static Ptr create(double threshold = DBL_MAX, double avg = 0); + /** @brief Constructor + @param threshold_ set threshold + */ + StandardCollector(double threshold_ = DBL_MAX); + /** @brief overloaded interface method */ + void init(size_t size); + /** @brief overloaded interface method */ + bool collect(int label, double dist); + /** @brief Returns label with minimal distance */ + CV_WRAP int getMinLabel() const; + /** @brief Returns minimal distance value */ + CV_WRAP double getMinDist() const; + /** @brief Return results as vector + @param sorted If set, results will be sorted by distance + Each values is a pair of label and distance. + */ + CV_WRAP std::vector< std::pair > getResults(bool sorted = false) const; + /** @brief Return results as map + Labels are keys, values are minimal distances + */ + std::map getResultsMap() const; + /** @brief Static constructor + @param threshold set threshold + */ + CV_WRAP static Ptr create(double threshold = DBL_MAX); }; - - //! @} } } -#endif \ No newline at end of file +#endif diff --git a/modules/face/src/eigen_faces.cpp b/modules/face/src/eigen_faces.cpp index efb2c060d..9ea5d8240 100644 --- a/modules/face/src/eigen_faces.cpp +++ b/modules/face/src/eigen_faces.cpp @@ -42,7 +42,7 @@ public: void train(InputArrayOfArrays src, InputArray labels); // Send all predict results to caller side for custom result handling - void predict(InputArray src, Ptr collector, const int state) const; + void predict(InputArray src, Ptr collector) const; }; //------------------------------------------------------------------------------ @@ -99,7 +99,7 @@ void Eigenfaces::train(InputArrayOfArrays _src, InputArray _local_labels) { } } -void Eigenfaces::predict(InputArray _src, Ptr collector, const int state) const { +void Eigenfaces::predict(InputArray _src, Ptr collector) const { // get data Mat src = _src.getMat(); // make sure the user is passing correct data @@ -114,11 +114,11 @@ void Eigenfaces::predict(InputArray _src, Ptr collector, const } // project into PCA subspace Mat q = LDA::subspaceProject(_eigenvectors, _mean, src.reshape(1, 1)); - collector->init((int)_projections.size(), state); + collector->init(_projections.size()); for (size_t sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) { double dist = norm(_projections[sampleIdx], q, NORM_L2); int label = _labels.at((int)sampleIdx); - if (!collector->collect(label, dist, state))return; + if (!collector->collect(label, dist))return; } } diff --git a/modules/face/src/facerec.cpp b/modules/face/src/facerec.cpp index 0ea2a044d..b9e062e34 100644 --- a/modules/face/src/facerec.cpp +++ b/modules/face/src/facerec.cpp @@ -80,10 +80,10 @@ int FaceRecognizer::predict(InputArray src) const { } void FaceRecognizer::predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) const { - Ptr collector = MinDistancePredictCollector::create(getThreshold()); - predict(src, collector, 0); - label = collector->getLabel(); - confidence = collector->getDist(); + Ptr collector = StandardCollector::create(getThreshold()); + predict(src, collector); + label = collector->getMinLabel(); + confidence = collector->getMinDist(); } } diff --git a/modules/face/src/fisher_faces.cpp b/modules/face/src/fisher_faces.cpp index bc7d89b27..a43fa435c 100644 --- a/modules/face/src/fisher_faces.cpp +++ b/modules/face/src/fisher_faces.cpp @@ -37,7 +37,7 @@ public: void train(InputArrayOfArrays src, InputArray labels); // Send all predict results to caller side for custom result handling - void predict(InputArray src, Ptr collector, const int state) const; + void predict(InputArray src, Ptr collector) const; }; // Removes duplicate elements in a given vector. @@ -120,7 +120,7 @@ void Fisherfaces::train(InputArrayOfArrays src, InputArray _lbls) { } } -void Fisherfaces::predict(InputArray _src, Ptr collector, const int state) const { +void Fisherfaces::predict(InputArray _src, Ptr collector) const { Mat src = _src.getMat(); // check data alignment just for clearer exception messages if(_projections.empty()) { @@ -134,11 +134,11 @@ void Fisherfaces::predict(InputArray _src, Ptr collector, cons // project into LDA subspace Mat q = LDA::subspaceProject(_eigenvectors, _mean, src.reshape(1,1)); // find 1-nearest neighbor - collector->init((int)_projections.size(), state); + collector->init((int)_projections.size()); for (size_t sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) { double dist = norm(_projections[sampleIdx], q, NORM_L2); int label = _labels.at((int)sampleIdx); - if (!collector->collect(label, dist, state))return; + if (!collector->collect(label, dist))return; } } diff --git a/modules/face/src/lbph_faces.cpp b/modules/face/src/lbph_faces.cpp index 8491fd940..f73f06029 100644 --- a/modules/face/src/lbph_faces.cpp +++ b/modules/face/src/lbph_faces.cpp @@ -92,7 +92,7 @@ public: void update(InputArrayOfArrays src, InputArray labels); // Send all predict results to caller side for custom result handling - void predict(InputArray src, Ptr collector, const int state = 0) const; + void predict(InputArray src, Ptr collector) const; // See FaceRecognizer::load. void load(const FileStorage& fs); @@ -383,7 +383,7 @@ void LBPH::train(InputArrayOfArrays _in_src, InputArray _in_labels, bool preserv } } -void LBPH::predict(InputArray _src, Ptr collector, const int state) const { +void LBPH::predict(InputArray _src, Ptr collector) const { if(_histograms.empty()) { // throw error if no data (or simply return -1?) String error_message = "This LBPH model is not computed yet. Did you call the train method?"; @@ -399,11 +399,11 @@ void LBPH::predict(InputArray _src, Ptr collector, const int s _grid_y, /* grid size y */ true /* normed histograms */); // find 1-nearest neighbor - collector->init((int)_histograms.size(), state); + collector->init((int)_histograms.size()); for (size_t sampleIdx = 0; sampleIdx < _histograms.size(); sampleIdx++) { double dist = compareHist(_histograms[sampleIdx], query, HISTCMP_CHISQR_ALT); int label = _labels.at((int)sampleIdx); - if (!collector->collect(label, dist, state))return; + if (!collector->collect(label, dist))return; } } diff --git a/modules/face/src/map_predict_collector.cpp b/modules/face/src/map_predict_collector.cpp deleted file mode 100644 index 1a26bde43..000000000 --- a/modules/face/src/map_predict_collector.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -#include -namespace cv { -namespace face { - -CV_WRAP bool MapPredictCollector::emit(const int label, const double dist, const int state) -{ - ((void)state); - //if already in index check which is closer - if (_idx->find(label) != _idx->end()) { - double current = (*_idx)[label]; - if (dist < current) { - (*_idx)[label] = dist; - } - } - else { - (*_idx)[label] = dist; - } - return true; -} - -Ptr > MapPredictCollector::getResult() -{ - return _idx; -} - -CV_WRAP std::vector > MapPredictCollector::getResultVector() -{ - std::vector > result; - std::copy(_idx->begin(), _idx->end(), std::back_inserter(result)); - return result; -} - -CV_WRAP Ptr MapPredictCollector::create(double threshold) -{ - return Ptr(new MapPredictCollector(threshold)); -} - - - -} -} \ No newline at end of file diff --git a/modules/face/src/mindist_predict_collector.cpp b/modules/face/src/mindist_predict_collector.cpp deleted file mode 100644 index 5410f0fce..000000000 --- a/modules/face/src/mindist_predict_collector.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -namespace cv { -namespace face { - -bool MinDistancePredictCollector::emit(const int label, const double dist, const int state) { - ((void)state); - _label = label; - _dist = dist; - return true; -} - -CV_WRAP bool MinDistancePredictCollector::filter(int* label, double* dist, const int state) -{ - ((void)label); - ((void)state); - return *dist < _dist; -} - -int MinDistancePredictCollector::getLabel() const { - return _label; -} - -double MinDistancePredictCollector::getDist() const { - return _dist; -} - -Ptr MinDistancePredictCollector::create(double threshold) { - return Ptr(new MinDistancePredictCollector(threshold)); -} - -} -} \ No newline at end of file diff --git a/modules/face/src/predict_collector.cpp b/modules/face/src/predict_collector.cpp index decee61ca..67fdb6ed3 100644 --- a/modules/face/src/predict_collector.cpp +++ b/modules/face/src/predict_collector.cpp @@ -42,117 +42,73 @@ or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. */ #include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -namespace cv { -namespace face { +namespace cv {namespace face { -void PredictCollector::init(const int size, const int state) { - //reserve for some-how usage in descendants - _size = size; - _state = state; +static std::pair toPair(const StandardCollector::PredictResult & val) { + return std::make_pair(val.label, val.distance); } -CV_WRAP bool PredictCollector::defaultFilter(int * label, double * dist, const int state) -{ - // if state provided we should compare it with current state - if (_state != 0 && _state != state) { - return false; - } - - // if exclude label provided we can test it first - if (_excludeLabel != 0 && _excludeLabel == *label) { - return false; - } - - // initially we must recalculate distance by koef iv given - if (_distanceKoef != 1) { - *dist = *dist * _distanceKoef; - } - // check upper threshold - if (*dist > _threshold) { - return false; - } - //check inner threshold - if (*dist < _minthreshold) { - return false; - } - - return true; +static bool pairLess(const std::pair & lhs, const std::pair & rhs) { + return lhs.second < rhs.second; } -CV_WRAP bool PredictCollector::filter(int* label, double* dist, const int state) -{ - ((void)label); - ((void)dist); - ((void)state); - return true; //no custom logic at base level +//=================================== + +StandardCollector::StandardCollector(double threshold_) : threshold(threshold_) { + init(0); } -bool PredictCollector::emit(const int label, const double dist, const int state) { - ((void)label); - ((void)dist); - ((void)state); - return false; // terminate prediction - no any behavior in base PredictCollector +void StandardCollector::init(size_t size) { + minRes = PredictResult(); + data.clear(); + data.reserve(size); } -CV_WRAP bool PredictCollector::collect(int label, double dist, const int state) -{ - if (defaultFilter(&label, &dist, state) && filter(&label,&dist,state)) { - return emit(label, dist, state); +bool StandardCollector::collect(int label, double dist) { + if (dist < threshold) + { + PredictResult res(label, dist); + if (res.distance < minRes.distance) + minRes = res; + data.push_back(res); } return true; } -CV_WRAP int PredictCollector::getSize() -{ - return _size; +int StandardCollector::getMinLabel() const { + return minRes.label; } -CV_WRAP void PredictCollector::setSize(int size) -{ - _size = size; +double StandardCollector::getMinDist() const { + return minRes.distance; } -CV_WRAP int PredictCollector::getState() -{ - return _state; -} - -CV_WRAP void PredictCollector::setState(int state) -{ - _state = state; -} - -CV_WRAP int PredictCollector::getExcludeLabel() -{ - return _excludeLabel; -} - -CV_WRAP void PredictCollector::setExcludeLabel(int excludeLabel) -{ - _excludeLabel = excludeLabel; -} - -CV_WRAP double PredictCollector::getDistanceKoef() -{ - return _distanceKoef; -} - -CV_WRAP void PredictCollector::setDistanceKoef(double distanceKoef) -{ - _distanceKoef = distanceKoef; -} - -CV_WRAP double PredictCollector::getMinThreshold() -{ - return _minthreshold; +std::vector< std::pair > StandardCollector::getResults(bool sorted) const { + std::vector< std::pair > res(data.size()); + std::transform(data.begin(), data.end(), res.begin(), &toPair); + if (sorted) + { + std::sort(res.begin(), res.end(), &pairLess); + } + return res; +} + +std::map StandardCollector::getResultsMap() const { + std::map res; + for (std::vector::const_iterator i = data.begin(); i != data.end(); ++i) { + std::map::iterator j = res.find(i->label); + if (j == res.end()) { + res.insert(toPair(*i)); + } else if (i->distance < j->second) { + j->second = i->distance; + } + } + return res; } -CV_WRAP void PredictCollector::setMinThreshold(double minthreshold) -{ - _minthreshold = minthreshold; +Ptr StandardCollector::create(double threshold) { + return makePtr(threshold); } -} -} \ No newline at end of file +}} // cv::face:: diff --git a/modules/face/src/stat_predict_collector.cpp b/modules/face/src/stat_predict_collector.cpp deleted file mode 100644 index bc4612a15..000000000 --- a/modules/face/src/stat_predict_collector.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -namespace cv { -namespace face { - -CV_WRAP bool StatPredictCollector::emit(const int label, const double dist, const int state) { - ((void)state); - ((void)label); - _count++; - _sum += dist; - if (_min > dist) { - _min = dist; - } - if (_max < dist) { - _max = dist; - } - return true; -} -CV_WRAP double StatPredictCollector::getMin() -{ - return _min; -} - -CV_WRAP double StatPredictCollector::getMax() -{ - return _max; -} - -CV_WRAP double StatPredictCollector::getSum() -{ - return _sum; -} - -CV_WRAP int StatPredictCollector::getCount() -{ - return _count; -} - -CV_WRAP Ptr StatPredictCollector::create(double threshold) { - return Ptr(new StatPredictCollector(threshold)); -} - -} -} \ No newline at end of file diff --git a/modules/face/src/std_predict_collector.cpp b/modules/face/src/std_predict_collector.cpp deleted file mode 100644 index 0350c4ec6..000000000 --- a/modules/face/src/std_predict_collector.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -namespace cv { -namespace face { - -CV_WRAP bool StdPredictCollector::emit(const int label, const double dist, const int state) { - ((void)state); - ((void)label); - _s += pow(dist - _avg, 2); - _n++; - return true; -} -CV_WRAP double StdPredictCollector::getResult() { - return sqrt(_s / (_n - 1)); -} -CV_WRAP Ptr StdPredictCollector::create(double threshold, double avg) { - return Ptr(new StdPredictCollector(threshold, avg)); -} - - -} -} \ No newline at end of file diff --git a/modules/face/src/topn_predict_collector.cpp b/modules/face/src/topn_predict_collector.cpp deleted file mode 100644 index 196c889db..000000000 --- a/modules/face/src/topn_predict_collector.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -#include // std::back_inserter -namespace cv { -namespace face { - -CV_WRAP bool TopNPredictCollector::emit(const int label, const double dist, const int state) -{ - ((void)state); - std::pair p = std::make_pair(label, dist); - if (_idx->size() == 0 || p.second <= _idx->front().second) { - _idx->push_front(p); - } else if (p.second >= _idx->back().second) { - _idx->push_back(p); - } - else { - typedef std::list >::iterator it_type; - for (it_type i = _idx->begin(); i != _idx->end(); i++) { - if (p.second <= i->second) { - _idx->insert(i, p); - break; - } - } - } - return true; -} - -CV_WRAP bool TopNPredictCollector::filter(int * label, double * dist, const int state) -{ - ((void)state); - if (_idx->size() < _size)return true; //not full - can insert; - if (*dist >= _idx->back().second)return false; //too far distance - for (std::list >::iterator it = _idx->begin(); it != _idx->end(); ++it) { - if (it->first == *label) { - if (it->second <= *dist) { - return false; //has more close - } - else { - _idx->erase(it); - return true; //no more require pop_back - } - } - } - _idx->pop_back(); - return true; -} - -CV_WRAP Ptr > > TopNPredictCollector::getResult() -{ - return _idx; -} - -CV_WRAP std::vector > TopNPredictCollector::getResultVector() -{ - std::vector > result; - std::copy(_idx->begin(), _idx->end(), std::back_inserter(result)); - return result; -} - -CV_WRAP Ptr TopNPredictCollector::create(size_t size, double threshold) -{ - return Ptr(new TopNPredictCollector(size, threshold)); -} - -} -} \ No newline at end of file diff --git a/modules/face/src/vector_predict_collector.cpp b/modules/face/src/vector_predict_collector.cpp deleted file mode 100644 index e83e134dd..000000000 --- a/modules/face/src/vector_predict_collector.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* -By downloading, copying, installing or using the software you agree to this license. -If you do not agree to this license, do not download, install, -copy or use the software. - - -License Agreement -For Open Source Computer Vision Library -(3-clause BSD License) - -Copyright (C) 2000-2015, Intel Corporation, all rights reserved. -Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. -Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved. -Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. -Copyright (C) 2015, OpenCV Foundation, all rights reserved. -Copyright (C) 2015, Itseez Inc., all rights reserved. -Third party copyrights are property of their respective owners. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -* Neither the names of the copyright holders nor the names of the contributors -may be used to endorse or promote products derived from this software -without specific prior written permission. - -This software is provided by the copyright holders and contributors "as is" and -any express or implied warranties, including, but not limited to, the implied -warranties of merchantability and fitness for a particular purpose are disclaimed. -In no event shall copyright holders or contributors be liable for any direct, -indirect, incidental, special, exemplary, or consequential damages -(including, but not limited to, procurement of substitute goods or services; -loss of use, data, or profits; or business interruption) however caused -and on any theory of liability, whether in contract, strict liability, -or tort (including negligence or otherwise) arising in any way out of -the use of this software, even if advised of the possibility of such damage. -*/ -#include "opencv2/face/predict_collector.hpp" -#include "opencv2/core/cvstd.hpp" -namespace cv { -namespace face { -CV_WRAP bool VectorPredictCollector::emit(const int label, const double dist, const int state) -{ - ((void)state); - _idx->push_back(std::pair(label, dist)); - return true; -} -Ptr > > VectorPredictCollector::getResult() -{ - return _idx; -} - -CV_WRAP std::vector > VectorPredictCollector::getResultVector() -{ - return (*_idx); -} - -CV_WRAP Ptr VectorPredictCollector::create(double threshold) -{ - return Ptr(new VectorPredictCollector(threshold)); -} - - -} -} \ No newline at end of file