Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
36afd4ef
Commit
36afd4ef
authored
May 06, 2014
by
Konstantin Matskevich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added additionalInfo in faceRecognition
parent
b1a28a52
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
204 additions
and
6 deletions
+204
-6
contrib.hpp
modules/contrib/include/opencv2/contrib/contrib.hpp
+8
-0
facerec.cpp
modules/contrib/src/facerec.cpp
+184
-2
facerec_demo.cpp
samples/cpp/facerec_demo.cpp
+12
-4
No files found.
modules/contrib/include/opencv2/contrib/contrib.hpp
View file @
36afd4ef
...
...
@@ -948,6 +948,14 @@ namespace cv
// Deserializes this object from a given cv::FileStorage.
virtual
void
load
(
const
FileStorage
&
fs
)
=
0
;
// Sets additions information as pairs label - info.
virtual
void
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
additionalInfo
)
=
0
;
// Gets string information by label
virtual
string
getLabelInfo
(
const
int
label
)
=
0
;
// Gets labels by string
virtual
vector
<
int
>
getLabelsByString
(
const
string
str
)
=
0
;
};
CV_EXPORTS_W
Ptr
<
FaceRecognizer
>
createEigenFaceRecognizer
(
int
num_components
=
0
,
double
threshold
=
DBL_MAX
);
...
...
modules/contrib/src/facerec.cpp
View file @
36afd4ef
...
...
@@ -18,6 +18,43 @@
#include "precomp.hpp"
#include <set>
struct
LabelInfo
{
LabelInfo
()
:
label
(
-
1
),
value
(
""
)
{}
LabelInfo
(
int
_label
,
std
::
string
_value
)
:
label
(
_label
),
value
(
_value
)
{}
std
::
string
value
;
int
label
;
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"
];
}
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
LabelInfo
&
info
);
};
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
);
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
out
,
const
LabelInfo
&
info
)
{
out
<<
"{ label = "
<<
info
.
label
<<
", "
<<
"value = "
<<
info
.
value
<<
"}"
;
return
out
;
}
namespace
cv
{
...
...
@@ -98,7 +135,6 @@ inline vector<_Tp> remove_dups(const vector<_Tp>& src) {
return
elems
;
}
// Turk, M., and Pentland, A. "Eigenfaces for recognition.". Journal of
// Cognitive Neuroscience 3 (1991), 71–86.
class
Eigenfaces
:
public
FaceRecognizer
...
...
@@ -111,6 +147,7 @@ private:
Mat
_eigenvectors
;
Mat
_eigenvalues
;
Mat
_mean
;
std
::
map
<
int
,
string
>
_labelsInfo
;
public
:
using
FaceRecognizer
::
save
;
...
...
@@ -147,6 +184,15 @@ public:
// See FaceRecognizer::save.
void
save
(
FileStorage
&
fs
)
const
;
// Sets additions information as pairs label - info.
void
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
);
// Gets additional information by label
string
getLabelInfo
(
const
int
label
);
// Gets labels by string
std
::
vector
<
int
>
getLabelsByString
(
const
string
str
);
AlgorithmInfo
*
info
()
const
;
};
...
...
@@ -164,6 +210,7 @@ private:
Mat
_mean
;
vector
<
Mat
>
_projections
;
Mat
_labels
;
std
::
map
<
int
,
string
>
_labelsInfo
;
public
:
using
FaceRecognizer
::
save
;
...
...
@@ -202,6 +249,15 @@ public:
// See FaceRecognizer::save.
void
save
(
FileStorage
&
fs
)
const
;
// Sets additions information as pairs label - info.
void
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
);
// Gets additional information by label
string
getLabelInfo
(
const
int
label
);
// Gets labels by string
std
::
vector
<
int
>
getLabelsByString
(
const
string
str
);
AlgorithmInfo
*
info
()
const
;
};
...
...
@@ -222,6 +278,7 @@ 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
...
...
@@ -287,6 +344,15 @@ public:
// See FaceRecognizer::save.
void
save
(
FileStorage
&
fs
)
const
;
// Sets additions information as pairs label - info.
void
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
);
// Gets additional information by label
string
getLabelInfo
(
const
int
label
);
// Gets labels by string
std
::
vector
<
int
>
getLabelsByString
(
const
string
str
);
// Getter functions.
int
neighbors
()
const
{
return
_neighbors
;
}
int
radius
()
const
{
return
_radius
;
}
...
...
@@ -423,6 +489,17 @@ void Eigenfaces::load(const FileStorage& fs) {
// read sequences
readFileNodeList
(
fs
[
"projections"
],
_projections
);
fs
[
"labels"
]
>>
_labels
;
const
FileNode
&
fn
=
fs
[
"info"
];
if
(
fn
.
type
()
==
FileNode
::
SEQ
)
{
_labelsInfo
.
clear
();
for
(
FileNodeIterator
it
=
fn
.
begin
();
it
!=
fn
.
end
();)
{
LabelInfo
item
;
it
>>
item
;
_labelsInfo
.
insert
(
std
::
make_pair
(
item
.
label
,
item
.
value
));
}
}
}
void
Eigenfaces
::
save
(
FileStorage
&
fs
)
const
{
...
...
@@ -434,6 +511,34 @@ void Eigenfaces::save(FileStorage& fs) const {
// write sequences
writeFileNodeList
(
fs
,
"projections"
,
_projections
);
fs
<<
"labels"
<<
_labels
;
fs
<<
"info"
<<
"["
;
for
(
std
::
map
<
int
,
string
>::
const_iterator
it
=
_labelsInfo
.
begin
();
it
!=
_labelsInfo
.
end
();
it
++
)
fs
<<
LabelInfo
(
it
->
first
,
it
->
second
);
fs
<<
"]"
;
}
void
Eigenfaces
::
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
)
{
_labelsInfo
=
labelsInfo
;
}
string
Eigenfaces
::
getLabelInfo
(
const
int
label
)
{
if
(
_labelsInfo
.
count
(
label
)
>
0
)
return
_labelsInfo
[
label
];
return
""
;
}
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
;
}
//------------------------------------------------------------------------------
...
...
@@ -544,6 +649,17 @@ void Fisherfaces::load(const FileStorage& fs) {
// read sequences
readFileNodeList
(
fs
[
"projections"
],
_projections
);
fs
[
"labels"
]
>>
_labels
;
const
FileNode
&
fn
=
fs
[
"info"
];
if
(
fn
.
type
()
==
FileNode
::
SEQ
)
{
_labelsInfo
.
clear
();
for
(
FileNodeIterator
it
=
fn
.
begin
();
it
!=
fn
.
end
();)
{
LabelInfo
item
;
it
>>
item
;
_labelsInfo
.
insert
(
std
::
make_pair
(
item
.
label
,
item
.
value
));
}
}
}
// See FaceRecognizer::save.
...
...
@@ -556,6 +672,34 @@ void Fisherfaces::save(FileStorage& fs) const {
// write sequences
writeFileNodeList
(
fs
,
"projections"
,
_projections
);
fs
<<
"labels"
<<
_labels
;
fs
<<
"info"
<<
"["
;
for
(
std
::
map
<
int
,
string
>::
const_iterator
it
=
_labelsInfo
.
begin
();
it
!=
_labelsInfo
.
end
();
it
++
)
fs
<<
LabelInfo
(
it
->
first
,
it
->
second
);
fs
<<
"]"
;
}
void
Fisherfaces
::
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
)
{
_labelsInfo
=
labelsInfo
;
}
string
Fisherfaces
::
getLabelInfo
(
const
int
label
)
{
if
(
_labelsInfo
.
count
(
label
)
>
0
)
return
_labelsInfo
[
label
];
return
""
;
}
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
;
}
//------------------------------------------------------------------------------
...
...
@@ -743,6 +887,17 @@ void LBPH::load(const FileStorage& fs) {
//read matrices
readFileNodeList
(
fs
[
"histograms"
],
_histograms
);
fs
[
"labels"
]
>>
_labels
;
const
FileNode
&
fn
=
fs
[
"info"
];
if
(
fn
.
type
()
==
FileNode
::
SEQ
)
{
_labelsInfo
.
clear
();
for
(
FileNodeIterator
it
=
fn
.
begin
();
it
!=
fn
.
end
();)
{
LabelInfo
item
;
it
>>
item
;
_labelsInfo
.
insert
(
std
::
make_pair
(
item
.
label
,
item
.
value
));
}
}
}
// See FaceRecognizer::save.
...
...
@@ -754,6 +909,34 @@ void LBPH::save(FileStorage& fs) const {
// write matrices
writeFileNodeList
(
fs
,
"histograms"
,
_histograms
);
fs
<<
"labels"
<<
_labels
;
fs
<<
"info"
<<
"["
;
for
(
std
::
map
<
int
,
string
>::
const_iterator
it
=
_labelsInfo
.
begin
();
it
!=
_labelsInfo
.
end
();
it
++
)
fs
<<
LabelInfo
(
it
->
first
,
it
->
second
);
fs
<<
"]"
;
}
void
LBPH
::
setLabelsInfo
(
const
std
::
map
<
int
,
string
>&
labelsInfo
)
{
_labelsInfo
=
labelsInfo
;
}
string
LBPH
::
getLabelInfo
(
const
int
label
)
{
if
(
_labelsInfo
.
count
(
label
)
>
0
)
return
_labelsInfo
[
label
];
return
""
;
}
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
)
{
...
...
@@ -849,7 +1032,6 @@ int LBPH::predict(InputArray _src) const {
return
label
;
}
Ptr
<
FaceRecognizer
>
createEigenFaceRecognizer
(
int
num_components
,
double
threshold
)
{
return
new
Eigenfaces
(
num_components
,
threshold
);
...
...
samples/cpp/facerec_demo.cpp
View file @
36afd4ef
...
...
@@ -39,20 +39,23 @@ static Mat toGrayscale(InputArray _src) {
return
dst
;
}
static
void
read_csv
(
const
string
&
filename
,
vector
<
Mat
>&
images
,
vector
<
int
>&
labels
,
char
separator
=
';'
)
{
static
void
read_csv
(
const
string
&
filename
,
vector
<
Mat
>&
images
,
vector
<
int
>&
labels
,
std
::
map
<
int
,
string
>&
labelsInfo
,
char
separator
=
';'
)
{
std
::
ifstream
file
(
filename
.
c_str
(),
ifstream
::
in
);
if
(
!
file
)
{
string
error_message
=
"No valid input file was given, please check the given filename."
;
CV_Error
(
CV_StsBadArg
,
error_message
);
}
string
line
,
path
,
classlabel
;
string
line
,
path
,
classlabel
,
info
;
while
(
getline
(
file
,
line
))
{
stringstream
liness
(
line
);
getline
(
liness
,
path
,
separator
);
getline
(
liness
,
classlabel
);
getline
(
liness
,
classlabel
,
separator
);
getline
(
liness
,
info
,
separator
);
if
(
!
path
.
empty
()
&&
!
classlabel
.
empty
())
{
images
.
push_back
(
imread
(
path
,
0
));
labels
.
push_back
(
atoi
(
classlabel
.
c_str
()));
if
(
!
info
.
empty
())
labelsInfo
.
insert
(
std
::
make_pair
(
labels
.
back
(),
info
));
}
}
}
...
...
@@ -69,15 +72,17 @@ int main(int argc, const char *argv[]) {
// These vectors hold the images and corresponding labels.
vector
<
Mat
>
images
;
vector
<
int
>
labels
;
std
::
map
<
int
,
string
>
labelsInfo
;
// Read in the data. This can fail if no valid
// input filename is given.
try
{
read_csv
(
fn_csv
,
images
,
labels
);
read_csv
(
fn_csv
,
images
,
labels
,
labelsInfo
);
}
catch
(
cv
::
Exception
&
e
)
{
cerr
<<
"Error opening file
\"
"
<<
fn_csv
<<
"
\"
. Reason: "
<<
e
.
msg
<<
endl
;
// nothing more we can do
exit
(
1
);
}
// Quit if there are not enough images for this demo.
if
(
images
.
size
()
<=
1
)
{
string
error_message
=
"This demo needs at least 2 images to work. Please add more images to your data set!"
;
...
...
@@ -111,6 +116,7 @@ int main(int argc, const char *argv[]) {
// cv::createEigenFaceRecognizer(10, 123.0);
//
Ptr
<
FaceRecognizer
>
model
=
createEigenFaceRecognizer
();
model
->
setLabelsInfo
(
labelsInfo
);
model
->
train
(
images
,
labels
);
// The following line predicts the label of a given
// test image:
...
...
@@ -124,6 +130,8 @@ 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
)
!=
""
)
)
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.
// Since each cv::FaceRecognizer is derived from a
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment