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
bf42a59c
Commit
bf42a59c
authored
8 years ago
by
johni
Browse files
Options
Downloads
Patches
Plain Diff
test branch
parent
2307d668
No related branches found
Branches containing commit
No related tags found
1 merge request
!5
Face detection
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
FacialRec.cpp
+266
-80
266 additions, 80 deletions
FacialRec.cpp
with
266 additions
and
80 deletions
FacialRec.cpp
+
266
−
80
View file @
bf42a59c
//This is our project
// //This is our project
//
// #include <iostream>
// #include <string>
//
// #include "opencv2/imgproc/imgproc.hpp"
// #include "opencv2/highgui/highgui.hpp"
//
// using namespace std;
// using namespace cv;
//
// Mat picture;
// char key;
//
// class FacialRec {
//
// };
//
// int main() {
// //Starting the video
// VideoCapture videoCapture(0);
//
// if (!videoCapture.isOpened()) {
// cout << "Unable to open video file." << endl;
// return 1;
// }
//
// namedWindow("Webcam", CV_WINDOW_AUTOSIZE);
//
//
// while(true) {
// Mat frame;
// videoCapture.retrieve(frame);
// bool success = videoCapture.read(frame);
// if (!success) {
// cout << "Could not read from video file" << endl;
// return 1;
// }
//
// //show some test text
// Mat frame_with_text = frame.clone();
// putText(frame_with_text, "Press Spacebar to take a picture", Point2f(110,100), FONT_HERSHEY_SIMPLEX, 2.0, Scalar(255,0,0,0), 3);
// putText(frame_with_text, "Press ESC to close the application", Point2f(90,600), FONT_HERSHEY_SIMPLEX, 2.0, Scalar(0,0,255,255), 3);
//
// imshow("Webcam", frame_with_text);
//
// key = waitKey(30);
//
// if (key == 27) { //escape key pressed: stop program
// cout << "ESC pressed. Program closing..." << endl;
// break;
// }
// else if (key == ' ') { //spacebar pressed: take a picture
//
// picture = frame;
// key = -1;
//
// while (true) {
// 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 ESC/Spacebar to return", Point2f(160,600), FONT_HERSHEY_SIMPLEX, 2.0, Scalar(0,0,255), 3);
//
// imshow("Webcam", picture_with_text);
//
// key = waitKey(30);
//
// if (key == 27 || key == 32) { //spacebar or ESC pressed
// cout << "ESC or SPACE pressed. Returning to video..." << endl;
// break;
// } else if (key == 115) {
// string name;
// string directory = "./images/";
// cout << "Please select a name for the image." << endl;
// getline(cin, name);
// directory.append(name);
// directory.append(".jpg");
// Point org;
// org.x = 15;
// org.y = 100;
// putText(picture, name, org, FONT_HERSHEY_SIMPLEX, 2, Scalar(0, 0, 255), 3);
// bool maybe = imwrite(directory, picture);
// cout << "s was pressed. saving image, success: " << maybe << endl;
// }
// }
// }
//
// }
//
// return 0;
// }
/*
* 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
<iostream>
#include
"opencv2/core/core.hpp"
#include
<string>
#include
"opencv2/imgproc/imgproc.hpp"
#include
"opencv2/highgui/highgui.hpp"
#include
"opencv2/highgui/highgui.hpp"
#include
"opencv2/contrib/contrib.hpp"
using
namespace
std
;
#include
<iostream>
using
namespace
cv
;
#include
<fstream>
#include
<sstream>
Mat
picture
;
char
key
;
class
FacialRec
{
};
int
main
()
{
//Starting the video
VideoCapture
videoCapture
(
0
);
if
(
!
videoCapture
.
isOpened
())
{
cout
<<
"Unable to open video file."
<<
endl
;
return
1
;
}
namedWindow
(
"Webcam"
,
CV_WINDOW_AUTOSIZE
);
while
(
true
)
{
Mat
frame
;
videoCapture
.
retrieve
(
frame
);
bool
success
=
videoCapture
.
read
(
frame
);
if
(
!
success
)
{
cout
<<
"Could not read from video file"
<<
endl
;
return
1
;
}
//show some test text
Mat
frame_with_text
=
frame
.
clone
();
putText
(
frame_with_text
,
"Press Spacebar to take a picture"
,
Point2f
(
110
,
100
),
FONT_HERSHEY_SIMPLEX
,
2.0
,
Scalar
(
255
,
0
,
0
,
0
),
3
);
putText
(
frame_with_text
,
"Press ESC to close the application"
,
Point2f
(
90
,
600
),
FONT_HERSHEY_SIMPLEX
,
2.0
,
Scalar
(
0
,
0
,
255
,
255
),
3
);
imshow
(
"Webcam"
,
frame_with_text
);
key
=
waitKey
(
30
);
if
(
key
==
27
)
{
//escape key pressed: stop program
cout
<<
"ESC pressed. Program closing..."
<<
endl
;
break
;
}
else
if
(
key
==
' '
)
{
//spacebar pressed: take a picture
picture
=
frame
;
key
=
-
1
;
while
(
true
)
{
using
namespace
cv
;
Mat
picture_with_text
=
picture
.
clone
();
using
namespace
std
;
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 ESC/Spacebar to return"
,
Point2f
(
160
,
600
),
FONT_HERSHEY_SIMPLEX
,
2.0
,
Scalar
(
0
,
0
,
255
),
3
);
imshow
(
"Webcam"
,
picture_with_text
);
static
void
read_csv
(
const
string
&
filename
,
vector
<
Mat
>&
images
,
vector
<
int
>&
labels
,
std
::
map
<
int
,
string
>&
labelsInfo
,
char
separator
=
';'
)
{
ifstream
csv
(
filename
.
c_str
());
if
(
!
csv
)
CV_Error
(
CV_StsBadArg
,
"No valid input file was given, please check the given filename."
);
string
line
,
path
,
classlabel
,
info
;
while
(
getline
(
csv
,
line
))
{
stringstream
liness
(
line
);
path
.
clear
();
classlabel
.
clear
();
info
.
clear
();
getline
(
liness
,
path
,
separator
);
getline
(
liness
,
classlabel
,
separator
);
getline
(
liness
,
info
,
separator
);
if
(
!
path
.
empty
()
&&
!
classlabel
.
empty
())
{
cout
<<
"Processing "
<<
path
<<
endl
;
int
label
=
atoi
(
classlabel
.
c_str
());
if
(
!
info
.
empty
())
labelsInfo
.
insert
(
std
::
make_pair
(
label
,
info
));
// 'path' can be file, dir or wildcard path
String
root
(
path
.
c_str
());
vector
<
String
>
files
;
glob
(
root
,
files
,
true
);
for
(
vector
<
String
>::
const_iterator
f
=
files
.
begin
();
f
!=
files
.
end
();
++
f
)
{
cout
<<
"
\t
"
<<
*
f
<<
endl
;
Mat
img
=
imread
(
*
f
,
CV_LOAD_IMAGE_GRAYSCALE
);
static
int
w
=-
1
,
h
=-
1
;
static
bool
showSmallSizeWarning
=
true
;
if
(
w
>
0
&&
h
>
0
&&
(
w
!=
img
.
cols
||
h
!=
img
.
rows
))
cout
<<
"
\t
* Warning: images should be of the same size!"
<<
endl
;
if
(
showSmallSizeWarning
&&
(
img
.
cols
<
50
||
img
.
rows
<
50
))
{
cout
<<
"* Warning: for better results images should be not smaller than 50x50!"
<<
endl
;
showSmallSizeWarning
=
false
;
}
images
.
push_back
(
img
);
labels
.
push_back
(
label
);
}
}
}
}
key
=
waitKey
(
30
);
int
main
(
int
argc
,
const
char
*
argv
[])
{
// Check for valid command line arguments, print usage
// if no arguments were given.
if
(
argc
!=
2
&&
argc
!=
3
)
{
cout
<<
"Usage: "
<<
argv
[
0
]
<<
" <csv> [arg2]
\n
"
<<
"
\t
<csv> - path to config file in CSV format
\n
"
<<
"
\t
arg2 - if the 2nd argument is provided (with any value) "
<<
"the advanced stuff is run and shown to console.
\n
"
<<
"The CSV config file consists of the following lines:
\n
"
<<
"<path>;<label>[;<comment>]
\n
"
<<
"
\t
<path> - file, dir or wildcard path
\n
"
<<
"
\t
<label> - non-negative integer person label
\n
"
<<
"
\t
<comment> - optional comment string (e.g. person name)"
<<
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
;
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
,
labelsInfo
);
}
catch
(
cv
::
Exception
&
e
)
{
cerr
<<
"Error opening file
\"
"
<<
fn_csv
<<
"
\"
. Reason: "
<<
e
.
msg
<<
endl
;
// nothing more we can do
exit
(
1
);
}
if
(
key
==
27
||
key
==
32
)
{
//spacebar or ESC pressed
// Quit if there are not enough images for this demo.
cout
<<
"ESC or SPACE pressed. Returning to video..."
<<
endl
;
if
(
images
.
size
()
<=
1
)
{
break
;
string
error_message
=
"This demo needs at least 2 images to work. Please add more images to your data set!"
;
}
else
if
(
key
==
115
)
{
CV_Error
(
CV_StsError
,
error_message
);
string
name
;
}
string
directory
=
"./images/"
;
// The following lines simply get the last images from
cout
<<
"Please select a name for the image."
<<
endl
;
// your dataset and remove it from the vector. This is
getline
(
cin
,
name
);
// done, so that the training data (which we learn the
directory
.
append
(
name
);
// cv::FaceRecognizer on) and the test data we test
directory
.
append
(
".jpg"
);
// the model with, do not overlap.
Point
org
;
Mat
testSample
=
images
[
images
.
size
()
-
1
];
org
.
x
=
15
;
int
testLabel
=
labels
[
labels
.
size
()
-
1
];
org
.
y
=
100
;
images
.
pop_back
();
putText
(
picture
,
name
,
org
,
FONT_HERSHEY_SIMPLEX
,
2
,
Scalar
(
0
,
0
,
255
),
3
);
labels
.
pop_back
();
bool
maybe
=
imwrite
(
directory
,
picture
);
// The following lines create an Eigenfaces model for
cout
<<
"s was pressed. saving image, success: "
<<
maybe
<<
endl
;
// face recognition and train it with the images and
}
// labels read from the given CSV file.
}
// This here is a full PCA, if you just want to keep
}
// 10 principal components (read Eigenfaces), then call
// the factory method like this:
//
// cv::createEigenFaceRecognizer(10);
//
// If you want to create a FaceRecognizer with a
// confidennce threshold, call it with:
//
// cv::createEigenFaceRecognizer(10, 123.0);
//
Ptr
<
FaceRecognizer
>
model
=
createEigenFaceRecognizer
();
model
->
setLabelsInfo
(
labelsInfo
);
model
->
train
(
images
,
labels
);
string
saveModelPath
=
"face-rec-model.txt"
;
cout
<<
"Saving the trained model to "
<<
saveModelPath
<<
endl
;
model
->
save
(
saveModelPath
);
}
// 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
;
if
(
(
predictedLabel
==
testLabel
)
&&
!
model
->
getLabelInfo
(
predictedLabel
).
empty
()
)
cout
<<
format
(
"%d-th label's info: %s"
,
predictedLabel
,
model
->
getLabelInfo
(
predictedLabel
).
c_str
())
<<
endl
;
return
0
;
// advanced stuff
if
(
argc
>
2
)
{
// 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
;
// Here is how to get the eigenvalues of this Eigenfaces model:
Mat
eigenvalues
=
model
->
getMat
(
"eigenvalues"
);
// And we can do the same to display the Eigenvectors (read Eigenfaces):
Mat
W
=
model
->
getMat
(
"eigenvectors"
);
// From this we will display the (at most) first 10 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
;
normalize
(
ev
.
reshape
(
1
),
grayscale
,
0
,
255
,
NORM_MINMAX
,
CV_8UC1
);
// Show the image & apply a Jet colormap for better sensing.
Mat
cgrayscale
;
applyColorMap
(
grayscale
,
cgrayscale
,
COLORMAP_JET
);
imshow
(
format
(
"%d"
,
i
),
cgrayscale
);
}
waitKey
(
0
);
}
return
0
;
}
}
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