From 8d4a76925c47703ebc13d593787dacc4b69568cc Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Tue, 20 May 2014 18:03:20 +0400 Subject: [PATCH] fixed binary compatibility --- modules/contrib/doc/facerec/facerec_api.rst | 15 +- .../contrib/include/opencv2/contrib/contrib.hpp | 6 +- modules/contrib/src/facerec.cpp | 169 ++++++++------------- samples/cpp/facerec_demo.cpp | 1 + 4 files changed, 76 insertions(+), 115 deletions(-) diff --git a/modules/contrib/doc/facerec/facerec_api.rst b/modules/contrib/doc/facerec/facerec_api.rst index 4c91bb7..edb6edb 100644 --- a/modules/contrib/doc/facerec/facerec_api.rst +++ b/modules/contrib/doc/facerec/facerec_api.rst @@ -48,13 +48,13 @@ a unified access to all face recongition algorithms in OpenCV. :: virtual void load(const FileStorage& fs) = 0; // Sets additional information as pairs label - info. - virtual void setLabelsInfo(const std::map& labelsInfo) = 0; + void setLabelsInfo(const std::map& labelsInfo); // Gets string information by label - virtual string getLabelInfo(int label) const = 0; + string getLabelInfo(const int &label); // Gets labels by string - virtual vector getLabelsByString(const string& str) = 0; + vector getLabelsByString(const string& str); }; @@ -308,7 +308,7 @@ FaceRecognizer::setLabelsInfo ----------------------------- Sets string information about labels into the model. -.. ocv:function:: void FaceRecognizer::setLabelsInfo(const std::map& labelsInfo) = 0 +.. ocv:function:: void FaceRecognizer::setLabelsInfo(const std::map& labelsInfo) Information about the label loads as a pair "label id - string info". @@ -316,7 +316,7 @@ FaceRecognizer::getLabelInfo ---------------------------- Gets string information by label. -.. ocv:function:: string FaceRecognizer::getLabelInfo(int label) const = 0 +.. ocv:function:: string FaceRecognizer::getLabelInfo(const int &label) If an unknown label id is provided or there is no label information assosiated with the specified label id the method returns an empty string. @@ -324,7 +324,7 @@ FaceRecognizer::getLabelsByString --------------------------------- Gets vector of labels by string. -.. ocv:function:: vector FaceRecognizer::getLabelsByString(const string& str) = 0 +.. ocv:function:: vector FaceRecognizer::getLabelsByString(const string& str) The function searches for the labels containing the specified substring in the associated string info. @@ -354,7 +354,6 @@ Model internal data: * ``mean`` The sample mean calculated from the training data. * ``projections`` The projections of the training data. * ``labels`` The threshold applied in the prediction. If the distance to the nearest neighbor is larger than the threshold, this method returns -1. -* ``labelsInfo`` The string information about the labels. createFisherFaceRecognizer -------------------------- @@ -382,7 +381,6 @@ Model internal data: * ``mean`` The sample mean calculated from the training data. * ``projections`` The projections of the training data. * ``labels`` The labels corresponding to the projections. -* ``labelsInfo`` The string information about the labels. createLBPHFaceRecognizer @@ -412,4 +410,3 @@ Model internal data: * ``threshold`` see :ocv:func:`createLBPHFaceRecognizer`. * ``histograms`` Local Binary Patterns Histograms calculated from the given training data (empty if none was given). * ``labels`` Labels corresponding to the calculated Local Binary Patterns Histograms. -* ``labelsInfo`` The string information about the labels. diff --git a/modules/contrib/include/opencv2/contrib/contrib.hpp b/modules/contrib/include/opencv2/contrib/contrib.hpp index e6e11d8..5684ee2 100644 --- a/modules/contrib/include/opencv2/contrib/contrib.hpp +++ b/modules/contrib/include/opencv2/contrib/contrib.hpp @@ -949,13 +949,13 @@ namespace cv virtual void load(const FileStorage& fs) = 0; // Sets additional information as pairs label - info. - virtual void setLabelsInfo(const std::map& labelsInfo) = 0; + void setLabelsInfo(const std::map& labelsInfo); // Gets string information by label - virtual string getLabelInfo(int label) const = 0; + string getLabelInfo(const int &label); // Gets labels by string - virtual vector getLabelsByString(const string& str) = 0; + vector getLabelsByString(const string& str); }; CV_EXPORTS_W Ptr createEigenFaceRecognizer(int num_components = 0, double threshold = DBL_MAX); diff --git a/modules/contrib/src/facerec.cpp b/modules/contrib/src/facerec.cpp index ef32b52..170f636 100644 --- a/modules/contrib/src/facerec.cpp +++ b/modules/contrib/src/facerec.cpp @@ -133,9 +133,51 @@ inline vector<_Tp> remove_dups(const vector<_Tp>& src) { return elems; } +// This class was introduced to avoid an addition of new virtual functions in FaceRecognizer class. +// It is safe for a binary compatibility. +class FaceRecognizerBase : public FaceRecognizer +{ +protected: + // Stored pairs "label id - string info" + std::map _labelsInfo; + +public: + // Sets additional information as pairs label - info. + virtual void setLabelsInfo(const std::map& labelsInfo); + + // Gets string information by label + virtual string getLabelInfo(int label) const; + + // Gets labels by string + virtual vector getLabelsByString(const string& str); +}; + +void FaceRecognizerBase::setLabelsInfo(const std::map& labelsInfo) +{ + _labelsInfo = labelsInfo; +} + +string FaceRecognizerBase::getLabelInfo(int label) const +{ + std::map::const_iterator iter(_labelsInfo.find(label)); + return iter != _labelsInfo.end() ? iter->second : ""; +} + +vector FaceRecognizerBase::getLabelsByString(const string& str) +{ + vector labels; + for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) + { + size_t found = (it->second).find(str); + if(found != string::npos) + labels.push_back(it->first); + } + return labels; +} + // Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of // Cognitive Neuroscience 3 (1991), 71–86. -class Eigenfaces : public FaceRecognizer +class Eigenfaces : public FaceRecognizerBase { private: int _num_components; @@ -145,7 +187,6 @@ private: Mat _eigenvectors; Mat _eigenvalues; Mat _mean; - std::map _labelsInfo; public: using FaceRecognizer::save; @@ -182,15 +223,6 @@ public: // See FaceRecognizer::save. void save(FileStorage& fs) const; - // Sets additional information as pairs label - info. - void setLabelsInfo(const std::map& labelsInfo); - - // Gets string information by label - string getLabelInfo(int label) const; - - // Gets labels by string - std::vector getLabelsByString(const string& str); - AlgorithmInfo* info() const; }; @@ -198,7 +230,7 @@ public: // faces: Recognition using class specific linear projection.". IEEE // Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997), // 711–720. -class Fisherfaces: public FaceRecognizer +class Fisherfaces: public FaceRecognizerBase { private: int _num_components; @@ -208,7 +240,6 @@ private: Mat _mean; vector _projections; Mat _labels; - std::map _labelsInfo; public: using FaceRecognizer::save; @@ -247,15 +278,6 @@ public: // See FaceRecognizer::save. void save(FileStorage& fs) const; - // Sets additional information as pairs label - info. - void setLabelsInfo(const std::map& labelsInfo); - - // Gets string information by label - string getLabelInfo(int label) const; - - // Gets labels by string - std::vector getLabelsByString(const string& str); - AlgorithmInfo* info() const; }; @@ -265,7 +287,7 @@ public: // patterns: Application to face recognition." IEEE Transactions on Pattern // Analysis and Machine Intelligence, 28(12):2037-2041. // -class LBPH : public FaceRecognizer +class LBPH : public FaceRecognizerBase { private: int _grid_x; @@ -276,14 +298,12 @@ private: vector _histograms; Mat _labels; - std::map _labelsInfo; // Computes a LBPH model with images in src and // corresponding labels in labels, possibly preserving // old model data. void train(InputArrayOfArrays src, InputArray labels, bool preserveData); - public: using FaceRecognizer::save; using FaceRecognizer::load; @@ -342,15 +362,6 @@ public: // See FaceRecognizer::save. void save(FileStorage& fs) const; - // Sets additional information as pairs label - info. - void setLabelsInfo(const std::map& labelsInfo); - - // Gets string information by label - string getLabelInfo(int label) const; - - // Gets labels by string - std::vector getLabelsByString(const string& str); - // Getter functions. int neighbors() const { return _neighbors; } int radius() const { return _radius; } @@ -391,6 +402,27 @@ void FaceRecognizer::load(const string& filename) { fs.release(); } +void FaceRecognizer::setLabelsInfo(const std::map& labelsInfo) +{ + FaceRecognizerBase* base = dynamic_cast(this); + CV_Assert(base != 0); + base->setLabelsInfo(labelsInfo); +} + +string FaceRecognizer::getLabelInfo(const int &label) +{ + FaceRecognizerBase* base = dynamic_cast(this); + CV_Assert(base != 0); + return base->getLabelInfo(label); +} + +vector FaceRecognizer::getLabelsByString(const string& str) +{ + FaceRecognizerBase* base = dynamic_cast(this); + CV_Assert(base != 0); + return base->getLabelsByString(str); +} + //------------------------------------------------------------------------------ // Eigenfaces //------------------------------------------------------------------------------ @@ -515,29 +547,6 @@ void Eigenfaces::save(FileStorage& fs) const { fs << "]"; } -void Eigenfaces::setLabelsInfo(const std::map& labelsInfo) -{ - _labelsInfo = labelsInfo; -} - -string Eigenfaces::getLabelInfo(int label) const -{ - std::map::const_iterator iter(_labelsInfo.find(label)); - return iter != _labelsInfo.end() ? iter->second : ""; -} - -vector Eigenfaces::getLabelsByString(const string& str) -{ - vector labels; - for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) - { - size_t found = (it->second).find(str); - if(found != string::npos) - labels.push_back(it->first); - } - return labels; -} - //------------------------------------------------------------------------------ // Fisherfaces //------------------------------------------------------------------------------ @@ -675,29 +684,6 @@ void Fisherfaces::save(FileStorage& fs) const { fs << "]"; } -void Fisherfaces::setLabelsInfo(const std::map& labelsInfo) -{ - _labelsInfo = labelsInfo; -} - -string Fisherfaces::getLabelInfo(int label) const -{ - std::map::const_iterator iter(_labelsInfo.find(label)); - return iter != _labelsInfo.end() ? iter->second : ""; -} - -vector Fisherfaces::getLabelsByString(const string& str) -{ - vector labels; - for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) - { - size_t found = (it->second).find(str); - if(found != string::npos) - labels.push_back(it->first); - } - return labels; -} - //------------------------------------------------------------------------------ // LBPH //------------------------------------------------------------------------------ @@ -911,29 +897,6 @@ void LBPH::save(FileStorage& fs) const { fs << "]"; } -void LBPH::setLabelsInfo(const std::map& labelsInfo) -{ - _labelsInfo = labelsInfo; -} - -string LBPH::getLabelInfo(int label) const -{ - std::map::const_iterator iter(_labelsInfo.find(label)); - return iter != _labelsInfo.end() ? iter->second : ""; -} - -vector LBPH::getLabelsByString(const string& str) -{ - vector labels; - for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) - { - size_t found = (it->second).find(str); - if(found != string::npos) - labels.push_back(it->first); - } - return labels; -} - void LBPH::train(InputArrayOfArrays _in_src, InputArray _in_labels) { this->train(_in_src, _in_labels, false); } diff --git a/samples/cpp/facerec_demo.cpp b/samples/cpp/facerec_demo.cpp index ef480c6..b92308e 100644 --- a/samples/cpp/facerec_demo.cpp +++ b/samples/cpp/facerec_demo.cpp @@ -118,6 +118,7 @@ int main(int argc, const char *argv[]) { Ptr model = createEigenFaceRecognizer(); model->setLabelsInfo(labelsInfo); model->train(images, labels); + // The following line predicts the label of a given // test image: int predictedLabel = model->predict(testSample); -- 2.7.4