Newer
Older
#include <iostream>
#include <string>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
using namespace cv;
Mat picture, picture_grayscale;
char key;
int picture_height;
int picture_width;
int num_subdirectories;
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;
picture_width = frame.rows;
picture_height = frame.cols;
Noah Eigenfeld
committed
//Get stored images
vector<Mat> images;
vector<int> labels;
Mat load_image, load_image_grayscale;
string filename = "";
string directory = "./myfaces/";
string subdirectory = "";
int j = 0;
while (true) {
Noah Eigenfeld
committed
subdirectory = to_string(j) + "/";
int i = 0;
while (true) {
filename = to_string(i);
string path = directory + subdirectory + filename + ".pgm";
Noah Eigenfeld
committed
cout << "path: " << path << endl;
load_image = imread(path, 1);
if (!load_image.data) {
cout << " No image data" << endl;
Noah Eigenfeld
committed
break;
}
load_image_grayscale = load_image.clone();
cvtColor(load_image, load_image_grayscale, CV_BGR2GRAY);
images.push_back(load_image_grayscale);
labels.push_back(0);
i++;
}
if (i == 0) { //no images in subdirectory/subdirectory does not exist
num_subdirectories = j;
break;
} else {
j++;
}
Noah Eigenfeld
committed
}
//Convert to grayscale
picture_grayscale = picture.clone();
cvtColor(picture, picture_grayscale, CV_BGR2GRAY);
// generate eigenface
labels.push_back(0);
images.push_back(picture_grayscale);
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
cout << "Face Recognizer created" << endl;
int predictedLabel = model->predict(picture_grayscale);
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) {
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;
Noah Eigenfeld
committed
string directory = "./myfaces/";
cout << "Please select a name for the image." << endl;
getline(cin, name);
directory.append(name);
directory.append(".png");
Point org;
org.x = 15;
org.y = 100;
putText(picture_with_text, 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;