Commit 8d4a7692 authored by Konstantin Matskevich's avatar Konstantin Matskevich

fixed binary compatibility

parent a46f119f
......@@ -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<int, string>& labelsInfo) = 0;
void setLabelsInfo(const std::map<int, string>& labelsInfo);
// Gets string information by label
virtual string getLabelInfo(int label) const = 0;
string getLabelInfo(const int &label);
// Gets labels by string
virtual vector<int> getLabelsByString(const string& str) = 0;
vector<int> 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<int, string>& labelsInfo) = 0
.. ocv:function:: void FaceRecognizer::setLabelsInfo(const std::map<int, string>& 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<int> FaceRecognizer::getLabelsByString(const string& str) = 0
.. ocv:function:: vector<int> 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.
......@@ -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<int, string>& labelsInfo) = 0;
void setLabelsInfo(const std::map<int, string>& labelsInfo);
// Gets string information by label
virtual string getLabelInfo(int label) const = 0;
string getLabelInfo(const int &label);
// Gets labels by string
virtual vector<int> getLabelsByString(const string& str) = 0;
vector<int> getLabelsByString(const string& str);
};
CV_EXPORTS_W Ptr<FaceRecognizer> createEigenFaceRecognizer(int num_components = 0, double threshold = DBL_MAX);
......
......@@ -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<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;
}
string FaceRecognizerBase::getLabelInfo(int label) const
{
std::map<int, string>::const_iterator iter(_labelsInfo.find(label));
return iter != _labelsInfo.end() ? iter->second : "";
}
vector<int> FaceRecognizerBase::getLabelsByString(const string& str)
{
vector<int> labels;
for(std::map<int,string>::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<int, string> _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<int,string>& labelsInfo);
// Gets string information by label
string getLabelInfo(int label) const;
// Gets labels by string
std::vector<int> 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<Mat> _projections;
Mat _labels;
std::map<int, string> _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<int,string>& labelsInfo);
// Gets string information by label
string getLabelInfo(int label) const;
// Gets labels by string
std::vector<int> 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<Mat> _histograms;
Mat _labels;
std::map<int, string> _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<int,string>& labelsInfo);
// Gets string information by label
string getLabelInfo(int label) const;
// Gets labels by string
std::vector<int> 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<int, string>& labelsInfo)
{
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(this);
CV_Assert(base != 0);
base->setLabelsInfo(labelsInfo);
}
string FaceRecognizer::getLabelInfo(const int &label)
{
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(this);
CV_Assert(base != 0);
return base->getLabelInfo(label);
}
vector<int> FaceRecognizer::getLabelsByString(const string& str)
{
FaceRecognizerBase* base = dynamic_cast<FaceRecognizerBase*>(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<int,string>& labelsInfo)
{
_labelsInfo = labelsInfo;
}
string Eigenfaces::getLabelInfo(int label) const
{
std::map<int, string>::const_iterator iter(_labelsInfo.find(label));
return iter != _labelsInfo.end() ? iter->second : "";
}
vector<int> Eigenfaces::getLabelsByString(const string& str)
{
vector<int> labels;
for(std::map<int,string>::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<int,string>& labelsInfo)
{
_labelsInfo = labelsInfo;
}
string Fisherfaces::getLabelInfo(int label) const
{
std::map<int, string>::const_iterator iter(_labelsInfo.find(label));
return iter != _labelsInfo.end() ? iter->second : "";
}
vector<int> Fisherfaces::getLabelsByString(const string& str)
{
vector<int> labels;
for(std::map<int,string>::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<int,string>& labelsInfo)
{
_labelsInfo = labelsInfo;
}
string LBPH::getLabelInfo(int label) const
{
std::map<int, string>::const_iterator iter(_labelsInfo.find(label));
return iter != _labelsInfo.end() ? iter->second : "";
}
vector<int> LBPH::getLabelsByString(const string& str)
{
vector<int> labels;
for(std::map<int,string>::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);
}
......
......@@ -118,6 +118,7 @@ int main(int argc, const char *argv[]) {
Ptr<FaceRecognizer> 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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment