Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
face_recognition
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
se_329_cylicon_valley
face_recognition
Commits
b43eaf45
Commit
b43eaf45
authored
8 years ago
by
Noah Eigenfeld
Browse files
Options
Downloads
Patches
Plain Diff
Got basic face recognition working
parent
e47e7269
No related branches found
Branches containing commit
No related tags found
1 merge request
!5
Face detection
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
FacialRec.cpp
+13
-36
13 additions, 36 deletions
FacialRec.cpp
lbph.cpp
+155
-0
155 additions, 0 deletions
lbph.cpp
with
168 additions
and
36 deletions
FacialRec.cpp
+
13
−
36
View file @
b43eaf45
...
@@ -85,7 +85,7 @@ int main() {
...
@@ -85,7 +85,7 @@ int main() {
load_image_grayscale
=
load_image
.
clone
();
load_image_grayscale
=
load_image
.
clone
();
cvtColor
(
load_image
,
load_image_grayscale
,
CV_BGR2GRAY
);
cvtColor
(
load_image
,
load_image_grayscale
,
CV_BGR2GRAY
);
images
.
push_back
(
load_image_grayscale
);
images
.
push_back
(
load_image_grayscale
);
labels
.
push_back
(
0
);
labels
.
push_back
(
j
);
i
++
;
i
++
;
}
}
...
@@ -100,51 +100,28 @@ int main() {
...
@@ -100,51 +100,28 @@ int main() {
//Check that images were read correctly
//Check that images were read correctly
cout
<<
"Images read: "
<<
images
.
size
()
<<
endl
;
cout
<<
"Images read: "
<<
images
.
size
()
<<
endl
;
int
counter
=
0
;
//Display all images read
for
(
Mat
test_image
:
images
)
{
// int counter = 0;
string
window_name
=
format
(
"test_image_%d"
,
counter
);
// for (Mat test_image : images) {
namedWindow
(
window_name
,
CV_WINDOW_AUTOSIZE
);
// string window_name = format("test_image_%d", counter);
imshow
(
window_name
,
test_image
);
// namedWindow(window_name, CV_WINDOW_AUTOSIZE);
cout
<<
"showing image "
<<
counter
<<
endl
;
// imshow(window_name, test_image);
counter
++
;
// cout << "showing image " << counter << endl;
}
// counter++;
// }
//Convert to grayscale
//Convert to grayscale
picture_grayscale
=
picture
.
clone
();
picture_grayscale
=
picture
.
clone
();
cvtColor
(
picture
,
picture_grayscale
,
CV_BGR2GRAY
);
cvtColor
(
picture
,
picture_grayscale
,
CV_BGR2GRAY
);
// generate eigenface
// generate eigenfaces
labels
.
push_back
(
0
);
Ptr
<
FaceRecognizer
>
model
=
createLBPHFaceRecognizer
();
images
.
push_back
(
picture_grayscale
);
Ptr
<
FaceRecognizer
>
model
=
createEigenFaceRecognizer
();
model
->
train
(
images
,
labels
);
model
->
train
(
images
,
labels
);
cout
<<
"Face Recognizer created"
<<
endl
;
cout
<<
"Face Recognizer created"
<<
endl
;
model
->
save
(
"FaceRecognizer"
);
int
predictedLabel
=
model
->
predict
(
picture_grayscale
);
int
predictedLabel
=
model
->
predict
(
picture_grayscale
);
cout
<<
"Predicted label: "
<<
predictedLabel
<<
endl
;
cout
<<
"Predicted label: "
<<
predictedLabel
<<
endl
;
// //Display mean
// Mat eigenvalues = model->getMat("eigenvalues");
// // And we can do the same to display the Eigenvectors (read Eigenfaces):
// Mat W = model->getMat("eigenvectors");
// // Get the sample mean from the training data
// Mat mean = model->getMat("mean");
// imshow("mean", mean);
// // Display or save the Eigenfaces:
// for (int i = 0; i < min(10, W.cols); i++) {
// string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));
// cout << msg << endl;
// // get eigenvector #i
// Mat ev = W.col(i).clone();
// // Reshape to original size & normalize to [0...255] for imshow.
// Mat grayscale = ev;
// // Show the image & apply a Jet colormap for better sensing.
// Mat cgrayscale;
// applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
// // Display or save:
// imshow(format("eigenface_%d", i), cgrayscale);
// }
while
(
true
)
{
while
(
true
)
{
Mat
picture_with_text
=
picture
.
clone
();
Mat
picture_with_text
=
picture
.
clone
();
putText
(
picture_with_text
,
"Press 's' to save"
,
Point2f
(
375
,
100
),
FONT_HERSHEY_SIMPLEX
,
2.0
,
Scalar
(
255
,
0
,
0
,
0
),
3
);
putText
(
picture_with_text
,
"Press 's' to save"
,
Point2f
(
375
,
100
),
FONT_HERSHEY_SIMPLEX
,
2.0
,
Scalar
(
255
,
0
,
0
,
0
),
3
);
...
...
This diff is collapsed.
Click to expand it.
lbph.cpp
0 → 100644
+
155
−
0
View file @
b43eaf45
/*
* Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
* Released to public domain under terms of the BSD Simplified license.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the organization nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* See <http://www.opensource.org/licenses/bsd-license>
*/
#include
"opencv2/core/core.hpp"
#include
"opencv2/contrib/contrib.hpp"
#include
"opencv2/highgui/highgui.hpp"
#include
<iostream>
#include
<fstream>
#include
<sstream>
using
namespace
cv
;
using
namespace
std
;
static
void
read_csv
(
const
string
&
filename
,
vector
<
Mat
>&
images
,
vector
<
int
>&
labels
,
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
;
while
(
getline
(
file
,
line
))
{
stringstream
liness
(
line
);
getline
(
liness
,
path
,
separator
);
getline
(
liness
,
classlabel
);
if
(
!
path
.
empty
()
&&
!
classlabel
.
empty
())
{
images
.
push_back
(
imread
(
path
,
0
));
labels
.
push_back
(
atoi
(
classlabel
.
c_str
()));
}
}
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
// Check for valid command line arguments, print usage
// if no arguments were given.
if
(
argc
!=
2
)
{
cout
<<
"usage: "
<<
argv
[
0
]
<<
" <csv.ext>"
<<
endl
;
exit
(
1
);
}
// Get the path to your CSV.
string
fn_csv
=
string
(
argv
[
1
]);
// These vectors hold the images and corresponding labels.
vector
<
Mat
>
images
;
vector
<
int
>
labels
;
// Read in the data. This can fail if no valid
// input filename is given.
try
{
read_csv
(
fn_csv
,
images
,
labels
);
}
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!"
;
CV_Error
(
CV_StsError
,
error_message
);
}
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size:
int
height
=
images
[
0
].
rows
;
// The following lines simply get the last images from
// your dataset and remove it from the vector. This is
// done, so that the training data (which we learn the
// cv::FaceRecognizer on) and the test data we test
// the model with, do not overlap.
Mat
testSample
=
images
[
images
.
size
()
-
1
];
int
testLabel
=
labels
[
labels
.
size
()
-
1
];
images
.
pop_back
();
labels
.
pop_back
();
// The following lines create an LBPH model for
// face recognition and train it with the images and
// labels read from the given CSV file.
//
// The LBPHFaceRecognizer uses Extended Local Binary Patterns
// (it's probably configurable with other operators at a later
// point), and has the following default values
//
// radius = 1
// neighbors = 8
// grid_x = 8
// grid_y = 8
//
// So if you want a LBPH FaceRecognizer using a radius of
// 2 and 16 neighbors, call the factory method with:
//
// cv::createLBPHFaceRecognizer(2, 16);
//
// And if you want a threshold (e.g. 123.0) call it with its default values:
//
// cv::createLBPHFaceRecognizer(1,8,8,8,123.0)
//
Ptr
<
FaceRecognizer
>
model
=
createLBPHFaceRecognizer
();
model
->
train
(
images
,
labels
);
// The following line predicts the label of a given
// test image:
int
predictedLabel
=
model
->
predict
(
testSample
);
//
// To get the confidence of a prediction call the model with:
//
// int predictedLabel = -1;
// double confidence = 0.0;
// model->predict(testSample, predictedLabel, confidence);
//
string
result_message
=
format
(
"Predicted class = %d / Actual class = %d."
,
predictedLabel
,
testLabel
);
cout
<<
result_message
<<
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
// cv::Algorithm, you can query the data.
//
// First we'll use it to set the threshold of the FaceRecognizer
// to 0.0 without retraining the model. This can be useful if
// you are evaluating the model:
//
model
->
set
(
"threshold"
,
0.0
);
// Now the threshold of this model is set to 0.0. A prediction
// now returns -1, as it's impossible to have a distance below
// it
predictedLabel
=
model
->
predict
(
testSample
);
cout
<<
"Predicted class = "
<<
predictedLabel
<<
endl
;
// Show some informations about the model, as there's no cool
// Model data to display as in Eigenfaces/Fisherfaces.
// Due to efficiency reasons the LBP images are not stored
// within the model:
cout
<<
"Model Information:"
<<
endl
;
string
model_info
=
format
(
"
\t
LBPH(radius=%i, neighbors=%i, grid_x=%i, grid_y=%i, threshold=%.2f)"
,
model
->
getInt
(
"radius"
),
model
->
getInt
(
"neighbors"
),
model
->
getInt
(
"grid_x"
),
model
->
getInt
(
"grid_y"
),
model
->
getDouble
(
"threshold"
));
cout
<<
model_info
<<
endl
;
// We could get the histograms for example:
vector
<
Mat
>
histograms
=
model
->
getMatVector
(
"histograms"
);
// But should I really visualize it? Probably the length is interesting:
cout
<<
"Size of the histograms: "
<<
histograms
[
0
].
total
()
<<
endl
;
return
0
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment