From 02b32d86d342083dbc4327300a94f0bf7cb5b48f Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Mon, 9 Jun 2014 13:26:45 +0400 Subject: [PATCH] moving FaceRecognizer2 from public header to .cpp --- .../contrib/include/opencv2/contrib/contrib.hpp | 20 ---- modules/contrib/src/facerec.cpp | 117 ++++++++++++--------- 2 files changed, 67 insertions(+), 70 deletions(-) diff --git a/modules/contrib/include/opencv2/contrib/contrib.hpp b/modules/contrib/include/opencv2/contrib/contrib.hpp index f74d9ca..5684ee2 100644 --- a/modules/contrib/include/opencv2/contrib/contrib.hpp +++ b/modules/contrib/include/opencv2/contrib/contrib.hpp @@ -958,26 +958,6 @@ namespace cv vector getLabelsByString(const string& str); }; - // The FaceRecognizerBase class is introduced to keep the FaceRecognizer binary backward compatibility in 2.4 - // In master setLabelInfo/getLabelInfo/getLabelsByString should be virtual and _labelsInfo should be moved to FaceRecognizer - // that allows to avoid FaceRecognizer2 in master - class FaceRecognizer2 : 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); - }; - CV_EXPORTS_W Ptr createEigenFaceRecognizer(int num_components = 0, double threshold = DBL_MAX); CV_EXPORTS_W Ptr createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX); CV_EXPORTS_W Ptr createLBPHFaceRecognizer(int radius=1, int neighbors=8, diff --git a/modules/contrib/src/facerec.cpp b/modules/contrib/src/facerec.cpp index f24e3ff..3f8aafb 100644 --- a/modules/contrib/src/facerec.cpp +++ b/modules/contrib/src/facerec.cpp @@ -18,41 +18,6 @@ #include "precomp.hpp" #include -struct LabelInfo -{ - LabelInfo():label(-1), value("") {} - LabelInfo(int _label, const std::string &_value): label(_label), value(_value) {} - int label; - std::string value; - void write(cv::FileStorage& fs) const - { - fs << "{" << "label" << label << "value" << value << "}"; - } - void read(const cv::FileNode& node) - { - label = (int)node["label"]; - value = (std::string)node["value"]; - } - std::ostream& operator<<(std::ostream& out) - { - out << "{ label = " << label << ", " << "value = " << value << "}"; - return out; - } -}; - -static void write(cv::FileStorage& fs, const std::string&, const LabelInfo& x) -{ - x.write(fs); -} - -static void read(const cv::FileNode& node, LabelInfo& x, const LabelInfo& default_value = LabelInfo()) -{ - if(node.empty()) - x = default_value; - else - x.read(node); -} - namespace cv { @@ -133,29 +98,81 @@ inline vector<_Tp> remove_dups(const vector<_Tp>& src) { return elems; } -void FaceRecognizer2::setLabelsInfo(const std::map& labelsInfo) +// The FaceRecognizer2 class is introduced to keep the FaceRecognizer binary backward compatibility in 2.4 +// In master setLabelInfo/getLabelInfo/getLabelsByString should be virtual and _labelsInfo should be moved +// to FaceRecognizer, that allows to avoid FaceRecognizer2 in master +class FaceRecognizer2 : public FaceRecognizer { - _labelsInfo = labelsInfo; -} +protected: + // Stored pairs "label id - string info" + std::map _labelsInfo; -string FaceRecognizer2::getLabelInfo(int label) const -{ - std::map::const_iterator iter(_labelsInfo.find(label)); - return iter != _labelsInfo.end() ? iter->second : ""; -} +public: + // Sets additional information as pairs label - info. + virtual void setLabelsInfo(const std::map& labelsInfo) + { + _labelsInfo = labelsInfo; + } -vector FaceRecognizer2::getLabelsByString(const string& str) + // Gets string information by label + virtual string getLabelInfo(int label) const + { + std::map::const_iterator iter(_labelsInfo.find(label)); + return iter != _labelsInfo.end() ? iter->second : ""; + } + + // Gets labels by string + virtual vector 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; + } + +}; + +// Utility structure to load/save face label info (a pair of int and string) via FileStorage +struct LabelInfo { - vector labels; - for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) + LabelInfo():label(-1), value("") {} + LabelInfo(int _label, const std::string &_value): label(_label), value(_value) {} + int label; + std::string value; + void write(cv::FileStorage& fs) const { - size_t found = (it->second).find(str); - if(found != string::npos) - labels.push_back(it->first); + fs << "{" << "label" << label << "value" << value << "}"; } - return labels; + void read(const cv::FileNode& node) + { + label = (int)node["label"]; + value = (std::string)node["value"]; + } + std::ostream& operator<<(std::ostream& out) + { + out << "{ label = " << label << ", " << "value = " << value << "}"; + return out; + } +}; + +static void write(cv::FileStorage& fs, const std::string&, const LabelInfo& x) +{ + x.write(fs); +} + +static void read(const cv::FileNode& node, LabelInfo& x, const LabelInfo& default_value = LabelInfo()) +{ + if(node.empty()) + x = default_value; + else + x.read(node); } + // Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of // Cognitive Neuroscience 3 (1991), 71–86. class Eigenfaces : public FaceRecognizer2 -- 2.7.4