This commit is contained in:
Konstantin Matskevich 2014-05-28 15:28:54 +04:00
parent 8d4a76925c
commit 59c8edfd98
3 changed files with 30 additions and 29 deletions

View File

@ -79,7 +79,7 @@ Moreover every :ocv:class:`FaceRecognizer` supports the:
* **Loading/Saving** the model state from/to a given XML or YAML. * **Loading/Saving** the model state from/to a given XML or YAML.
* **Setting/Getting labels info**, that is storaged as a string. * **Setting/Getting labels info**, that is storaged as a string. String labels info is useful for keeping names of the recognized people.
.. note:: When using the FaceRecognizer interface in combination with Python, please stick to Python 2. Some underlying scripts like create_csv will not work in other versions, like Python 3. .. note:: When using the FaceRecognizer interface in combination with Python, please stick to Python 2. Some underlying scripts like create_csv will not work in other versions, like Python 3.

View File

@ -958,6 +958,26 @@ namespace cv
vector<int> getLabelsByString(const string& str); vector<int> 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<int, string> _labelsInfo;
public:
// Sets additional information as pairs label - info.
virtual void setLabelsInfo(const std::map<int, string>& labelsInfo);
// Gets string information by label
virtual string getLabelInfo(int label) const;
// Gets labels by string
virtual vector<int> getLabelsByString(const string& str);
};
CV_EXPORTS_W Ptr<FaceRecognizer> createEigenFaceRecognizer(int num_components = 0, double threshold = DBL_MAX); CV_EXPORTS_W Ptr<FaceRecognizer> createEigenFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
CV_EXPORTS_W Ptr<FaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX); CV_EXPORTS_W Ptr<FaceRecognizer> createFisherFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
CV_EXPORTS_W Ptr<FaceRecognizer> createLBPHFaceRecognizer(int radius=1, int neighbors=8, CV_EXPORTS_W Ptr<FaceRecognizer> createLBPHFaceRecognizer(int radius=1, int neighbors=8,

View File

@ -133,37 +133,18 @@ inline vector<_Tp> remove_dups(const vector<_Tp>& src) {
return elems; return elems;
} }
// This class was introduced to avoid an addition of new virtual functions in FaceRecognizer class. void FaceRecognizer2::setLabelsInfo(const std::map<int,string>& labelsInfo)
// It is safe for a binary compatibility.
class FaceRecognizerBase : public FaceRecognizer
{
protected:
// Stored pairs "label id - string info"
std::map<int, string> _labelsInfo;
public:
// Sets additional information as pairs label - info.
virtual void setLabelsInfo(const std::map<int, string>& labelsInfo);
// Gets string information by label
virtual string getLabelInfo(int label) const;
// Gets labels by string
virtual vector<int> getLabelsByString(const string& str);
};
void FaceRecognizerBase::setLabelsInfo(const std::map<int,string>& labelsInfo)
{ {
_labelsInfo = labelsInfo; _labelsInfo = labelsInfo;
} }
string FaceRecognizerBase::getLabelInfo(int label) const string FaceRecognizer2::getLabelInfo(int label) const
{ {
std::map<int, string>::const_iterator iter(_labelsInfo.find(label)); std::map<int, string>::const_iterator iter(_labelsInfo.find(label));
return iter != _labelsInfo.end() ? iter->second : ""; return iter != _labelsInfo.end() ? iter->second : "";
} }
vector<int> FaceRecognizerBase::getLabelsByString(const string& str) vector<int> FaceRecognizer2::getLabelsByString(const string& str)
{ {
vector<int> labels; vector<int> labels;
for(std::map<int,string>::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++) for(std::map<int,string>::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++)
@ -177,7 +158,7 @@ vector<int> FaceRecognizerBase::getLabelsByString(const string& str)
// Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of // Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of
// Cognitive Neuroscience 3 (1991), 7186. // Cognitive Neuroscience 3 (1991), 7186.
class Eigenfaces : public FaceRecognizerBase class Eigenfaces : public FaceRecognizer2
{ {
private: private:
int _num_components; int _num_components;
@ -230,7 +211,7 @@ public:
// faces: Recognition using class specific linear projection.". IEEE // faces: Recognition using class specific linear projection.". IEEE
// Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997), // Transactions on Pattern Analysis and Machine Intelligence 19, 7 (1997),
// 711720. // 711720.
class Fisherfaces: public FaceRecognizerBase class Fisherfaces: public FaceRecognizer2
{ {
private: private:
int _num_components; int _num_components;
@ -287,7 +268,7 @@ public:
// patterns: Application to face recognition." IEEE Transactions on Pattern // patterns: Application to face recognition." IEEE Transactions on Pattern
// Analysis and Machine Intelligence, 28(12):2037-2041. // Analysis and Machine Intelligence, 28(12):2037-2041.
// //
class LBPH : public FaceRecognizerBase class LBPH : public FaceRecognizer2
{ {
private: private:
int _grid_x; int _grid_x;
@ -404,21 +385,21 @@ void FaceRecognizer::load(const string& filename) {
void FaceRecognizer::setLabelsInfo(const std::map<int, string>& labelsInfo) void FaceRecognizer::setLabelsInfo(const std::map<int, string>& labelsInfo)
{ {
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(this); FaceRecognizer2* base = dynamic_cast<FaceRecognizer2*>(this);
CV_Assert(base != 0); CV_Assert(base != 0);
base->setLabelsInfo(labelsInfo); base->setLabelsInfo(labelsInfo);
} }
string FaceRecognizer::getLabelInfo(const int &label) string FaceRecognizer::getLabelInfo(const int &label)
{ {
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(this); FaceRecognizer2* base = dynamic_cast<FaceRecognizer2*>(this);
CV_Assert(base != 0); CV_Assert(base != 0);
return base->getLabelInfo(label); return base->getLabelInfo(label);
} }
vector<int> FaceRecognizer::getLabelsByString(const string& str) vector<int> FaceRecognizer::getLabelsByString(const string& str)
{ {
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(this); FaceRecognizer2* base = dynamic_cast<FaceRecognizer2*>(this);
CV_Assert(base != 0); CV_Assert(base != 0);
return base->getLabelsByString(str); return base->getLabelsByString(str);
} }