Skip to content

Commit 1a61e8d

Browse files
committed
Merge pull request #1268 from berak:face_read_write
2 parents 489f8df + b73d565 commit 1a61e8d

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

modules/face/src/face_basic.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ cv::Mat BasicFaceRecognizer::getMean() const
5353
void BasicFaceRecognizer::read(const FileNode& fs)
5454
{
5555
//read matrices
56-
fs["threshold"] >> _threshold;
56+
double _t = 0;
57+
fs["threshold"] >> _t; // older versions might not have "threshold"
58+
if (_t !=0)
59+
_threshold = _t; // be careful, not to overwrite DBL_MAX with 0 !
5760
fs["num_components"] >> _num_components;
5861
fs["mean"] >> _mean;
5962
fs["eigenvalues"] >> _eigenvalues;

modules/face/src/facerec.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void FaceRecognizer::read(const String &filename)
5959
FileStorage fs(filename, FileStorage::READ);
6060
if (!fs.isOpened())
6161
CV_Error(Error::StsError, "File can't be opened for reading!");
62-
this->read(fs.root());
62+
this->read(fs.getFirstTopLevelNode());
6363
fs.release();
6464
}
6565

@@ -68,7 +68,9 @@ void FaceRecognizer::write(const String &filename) const
6868
FileStorage fs(filename, FileStorage::WRITE);
6969
if (!fs.isOpened())
7070
CV_Error(Error::StsError, "File can't be opened for writing!");
71+
fs << getDefaultName() << "{";
7172
this->write(fs);
73+
fs << "}";
7274
fs.release();
7375
}
7476

modules/face/src/lbph_faces.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ class LBPH : public LBPHFaceRecognizer
119119

120120

121121
void LBPH::read(const FileNode& fs) {
122-
fs["threshold"] >> _threshold;
122+
double _t = 0;
123+
fs["threshold"] >> _t; // older versions might not have "threshold"
124+
if (_t !=0)
125+
_threshold = _t; // be careful, not to overwrite DBL_MAX with 0 !
123126
fs["radius"] >> _radius;
124127
fs["neighbors"] >> _neighbors;
125128
fs["grid_x"] >> _grid_x;

modules/face/test/test_loadsave.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
By downloading, copying, installing or using the software you agree to this
3+
license. If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
6+
License Agreement
7+
For Open Source Computer Vision Library
8+
(3-clause BSD License)
9+
10+
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
11+
Third party copyrights are property of their respective owners.
12+
13+
Redistribution and use in source and binary forms, with or without modification,
14+
are permitted provided that the following conditions are met:
15+
16+
* Redistributions of source code must retain the above copyright notice,
17+
this list of conditions and the following disclaimer.
18+
19+
* Redistributions in binary form must reproduce the above copyright notice,
20+
this list of conditions and the following disclaimer in the documentation
21+
and/or other materials provided with the distribution.
22+
23+
* Neither the names of the copyright holders nor the names of the contributors
24+
may be used to endorse or promote products derived from this software
25+
without specific prior written permission.
26+
27+
This software is provided by the copyright holders and contributors "as is" and
28+
any express or implied warranties, including, but not limited to, the implied
29+
warranties of merchantability and fitness for a particular purpose are
30+
disclaimed. In no event shall copyright holders or contributors be liable for
31+
any direct, indirect, incidental, special, exemplary, or consequential damages
32+
(including, but not limited to, procurement of substitute goods or services;
33+
loss of use, data, or profits; or business interruption) however caused
34+
and on any theory of liability, whether in contract, strict liability,
35+
or tort (including negligence or otherwise) arising in any way out of
36+
the use of this software, even if advised of the possibility of such damage.
37+
*/
38+
39+
#include "test_precomp.hpp"
40+
41+
// regression for #1267
42+
// let's make sure, that both Algorithm::save(String) and
43+
// FaceRecognizer::write(String) lead to the same result
44+
45+
void make_test_data(std::vector<cv::Mat> &images, std::vector<int> &labels) {
46+
for (int i=0; i<5; i++) {
47+
cv::Mat m(100,100,CV_8U);
48+
cv::randu(m,0,255);
49+
images.push_back(m);
50+
labels.push_back(i);
51+
}
52+
}
53+
54+
TEST(CV_Face_SAVELOAD, use_save) {
55+
std::vector<cv::Mat> images;
56+
std::vector<int> labels;
57+
make_test_data(images, labels);
58+
cv::Ptr<cv::face::FaceRecognizer> model1 = cv::face::LBPHFaceRecognizer::create();
59+
model1->train(images,labels);
60+
model1->save("fr.xml");
61+
int p1 = model1->predict(images[2]);
62+
cv::Ptr<cv::face::FaceRecognizer> model2 = cv::face::LBPHFaceRecognizer::create();
63+
model2->read("fr.xml");
64+
EXPECT_EQ(model2->empty(), false);
65+
EXPECT_EQ(p1, model2->predict(images[2]));
66+
}
67+
68+
TEST(CV_Face_SAVELOAD, use_write) {
69+
std::vector<cv::Mat> images;
70+
std::vector<int> labels;
71+
make_test_data(images, labels);
72+
cv::Ptr<cv::face::FaceRecognizer> model1 = cv::face::LBPHFaceRecognizer::create();
73+
model1->train(images,labels);
74+
model1->write("fr.xml");
75+
int p1 = model1->predict(images[2]);
76+
cv::Ptr<cv::face::FaceRecognizer> model2 = cv::face::LBPHFaceRecognizer::create();
77+
model2->read("fr.xml");
78+
EXPECT_EQ(model2->empty(), false);
79+
EXPECT_EQ(p1, model2->predict(images[2]));
80+
}

0 commit comments

Comments
 (0)