From aa76ef9a98d64e45f441eb914541f78d66912b9f Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Wed, 7 May 2014 17:19:22 +0400 Subject: [PATCH] fixes --- .../include/opencv2/contrib/contrib.hpp | 4 +- modules/contrib/src/facerec.cpp | 46 +++++++++---------- samples/cpp/facerec_demo.cpp | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/modules/contrib/include/opencv2/contrib/contrib.hpp b/modules/contrib/include/opencv2/contrib/contrib.hpp index 18c6eeb4b6..b3b4f330db 100644 --- a/modules/contrib/include/opencv2/contrib/contrib.hpp +++ b/modules/contrib/include/opencv2/contrib/contrib.hpp @@ -952,10 +952,10 @@ namespace cv virtual void setLabelsInfo(const std::map& additionalInfo) = 0; // Gets string information by label - virtual string getLabelInfo(const int label) = 0; + virtual string getLabelInfo(int label) const = 0; // Gets labels by string - virtual vector getLabelsByString(const string str) = 0; + virtual vector getLabelsByString(const string& str) = 0; }; 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 20bd411075..9c310e0316 100644 --- a/modules/contrib/src/facerec.cpp +++ b/modules/contrib/src/facerec.cpp @@ -21,9 +21,9 @@ struct LabelInfo { LabelInfo():label(-1), value("") {} - LabelInfo(int _label, std::string _value): label(_label), value(_value) {} - std::string 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 << "}"; @@ -33,7 +33,11 @@ struct LabelInfo label = (int)node["label"]; value = (std::string)node["value"]; } - friend std::ostream& operator<<(std::ostream& out, const LabelInfo& info); + 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) @@ -49,12 +53,6 @@ static void read(const cv::FileNode& node, LabelInfo& x, const LabelInfo& defaul x.read(node); } -std::ostream& operator<<(std::ostream& out, const LabelInfo& info) -{ - out << "{ label = " << info.label << ", " << "value = " << info.value << "}"; - return out; -} - namespace cv { @@ -188,10 +186,10 @@ public: void setLabelsInfo(const std::map& labelsInfo); // Gets additional information by label - string getLabelInfo(const int label); + string getLabelInfo(int label) const; // Gets labels by string - std::vector getLabelsByString(const string str); + std::vector getLabelsByString(const string& str); AlgorithmInfo* info() const; }; @@ -253,10 +251,10 @@ public: void setLabelsInfo(const std::map& labelsInfo); // Gets additional information by label - string getLabelInfo(const int label); + string getLabelInfo(int label) const; // Gets labels by string - std::vector getLabelsByString(const string str); + std::vector getLabelsByString(const string& str); AlgorithmInfo* info() const; }; @@ -348,10 +346,10 @@ public: void setLabelsInfo(const std::map& labelsInfo); // Gets additional information by label - string getLabelInfo(const int label); + string getLabelInfo(int label) const; // Gets labels by string - std::vector getLabelsByString(const string str); + std::vector getLabelsByString(const string& str); // Getter functions. int neighbors() const { return _neighbors; } @@ -522,14 +520,14 @@ void Eigenfaces::setLabelsInfo(const std::map& labelsInfo) _labelsInfo = labelsInfo; } -string Eigenfaces::getLabelInfo(const int label) +string Eigenfaces::getLabelInfo(int label) const { if(_labelsInfo.count(label) > 0) - return _labelsInfo[label]; + return _labelsInfo.at(label); return ""; } -vector Eigenfaces::getLabelsByString(const string str) +vector Eigenfaces::getLabelsByString(const string& str) { vector labels; for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) @@ -683,14 +681,14 @@ void Fisherfaces::setLabelsInfo(const std::map& labelsInfo) _labelsInfo = labelsInfo; } -string Fisherfaces::getLabelInfo(const int label) +string Fisherfaces::getLabelInfo(int label) const { if(_labelsInfo.count(label) > 0) - return _labelsInfo[label]; + return _labelsInfo.at(label); return ""; } -vector Fisherfaces::getLabelsByString(const string str) +vector Fisherfaces::getLabelsByString(const string& str) { vector labels; for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) @@ -920,14 +918,14 @@ void LBPH::setLabelsInfo(const std::map& labelsInfo) _labelsInfo = labelsInfo; } -string LBPH::getLabelInfo(const int label) +string LBPH::getLabelInfo(int label) const { if(_labelsInfo.count(label) > 0) - return _labelsInfo[label]; + return _labelsInfo.at(label); return ""; } -vector LBPH::getLabelsByString(const string str) +vector LBPH::getLabelsByString(const string& str) { vector labels; for(std::map::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) diff --git a/samples/cpp/facerec_demo.cpp b/samples/cpp/facerec_demo.cpp index dfc15aa81e..ef480c6df7 100644 --- a/samples/cpp/facerec_demo.cpp +++ b/samples/cpp/facerec_demo.cpp @@ -130,7 +130,7 @@ int main(int argc, const char *argv[]) { // string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel); cout << result_message << endl; - if( (predictedLabel == testLabel) && (model->getLabelInfo(predictedLabel) != "") ) + if( (predictedLabel == testLabel) && !model->getLabelInfo(predictedLabel).empty() ) cout << format("%d-th label's info: %s", predictedLabel, model->getLabelInfo(predictedLabel).c_str()) << endl; // Sometimes you'll need to get/set internal model data, // which isn't exposed by the public cv::FaceRecognizer.