c22e41621165e42102b5f3cf0f19baa958b69c32
[platform/upstream/opencv.git] / modules / contrib / doc / facerec / src / facerec_video.cpp
1 /*
2  * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
3  * Released to public domain under terms of the BSD Simplified license.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *   * Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *   * Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *   * Neither the name of the organization nor the names of its contributors
13  *     may be used to endorse or promote products derived from this software
14  *     without specific prior written permission.
15  *
16  *   See <http://www.opensource.org/licenses/bsd-license>
17  */
18
19 #include "opencv2/core.hpp"
20 #include "opencv2/contrib.hpp"
21 #include "opencv2/highgui.hpp"
22 #include "opencv2/imgproc.hpp"
23 #include "opencv2/objdetect.hpp"
24
25 #include <iostream>
26 #include <fstream>
27 #include <sstream>
28
29 using namespace cv;
30 using namespace std;
31
32 static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
33     std::ifstream file(filename.c_str(), ifstream::in);
34     if (!file) {
35         string error_message = "No valid input file was given, please check the given filename.";
36         CV_Error(CV_StsBadArg, error_message);
37     }
38     string line, path, classlabel;
39     while (getline(file, line)) {
40         stringstream liness(line);
41         getline(liness, path, separator);
42         getline(liness, classlabel);
43         if(!path.empty() && !classlabel.empty()) {
44             images.push_back(imread(path, 0));
45             labels.push_back(atoi(classlabel.c_str()));
46         }
47     }
48 }
49
50 int main(int argc, const char *argv[]) {
51     // Check for valid command line arguments, print usage
52     // if no arguments were given.
53     if (argc != 4) {
54         cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
55         cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
56         cout << "\t </path/to/csv.ext> -- Path to the CSV file with the face database." << endl;
57         cout << "\t <device id> -- The webcam device id to grab frames from." << endl;
58         exit(1);
59     }
60     // Get the path to your CSV:
61     string fn_haar = string(argv[1]);
62     string fn_csv = string(argv[2]);
63     int deviceId = atoi(argv[3]);
64     // These vectors hold the images and corresponding labels:
65     vector<Mat> images;
66     vector<int> labels;
67     // Read in the data (fails if no valid input filename is given, but you'll get an error message):
68     try {
69         read_csv(fn_csv, images, labels);
70     } catch (cv::Exception& e) {
71         cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
72         // nothing more we can do
73         exit(1);
74     }
75     // Get the height from the first image. We'll need this
76     // later in code to reshape the images to their original
77     // size AND we need to reshape incoming faces to this size:
78     int im_width = images[0].cols;
79     int im_height = images[0].rows;
80     // Create a FaceRecognizer and train it on the given images:
81     Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
82     model->train(images, labels);
83     // That's it for learning the Face Recognition model. You now
84     // need to create the classifier for the task of Face Detection.
85     // We are going to use the haar cascade you have specified in the
86     // command line arguments:
87     //
88     CascadeClassifier haar_cascade;
89     haar_cascade.load(fn_haar);
90     // Get a handle to the Video device:
91     VideoCapture cap(deviceId);
92     // Check if we can use this device at all:
93     if(!cap.isOpened()) {
94         cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
95         return -1;
96     }
97     // Holds the current frame from the Video device:
98     Mat frame;
99     for(;;) {
100         cap >> frame;
101         // Clone the current frame:
102         Mat original = frame.clone();
103         // Convert the current frame to grayscale:
104         Mat gray;
105         cvtColor(original, gray, CV_BGR2GRAY);
106         // Find the faces in the frame:
107         vector< Rect_<int> > faces;
108         haar_cascade.detectMultiScale(gray, faces);
109         // At this point you have the position of the faces in
110         // faces. Now we'll get the faces, make a prediction and
111         // annotate it in the video. Cool or what?
112         for(int i = 0; i < faces.size(); i++) {
113             // Process face by face:
114             Rect face_i = faces[i];
115             // Crop the face from the image. So simple with OpenCV C++:
116             Mat face = gray(face_i);
117             // Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
118             // verify this, by reading through the face recognition tutorial coming with OpenCV.
119             // Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
120             // input data really depends on the algorithm used.
121             //
122             // I strongly encourage you to play around with the algorithms. See which work best
123             // in your scenario, LBPH should always be a contender for robust face recognition.
124             //
125             // Since I am showing the Fisherfaces algorithm here, I also show how to resize the
126             // face you have just found:
127             Mat face_resized;
128             cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
129             // Now perform the prediction, see how easy that is:
130             int prediction = model->predict(face_resized);
131             // And finally write all we've found out to the original image!
132             // First of all draw a green rectangle around the detected face:
133             rectangle(original, face_i, CV_RGB(0, 255,0), 1);
134             // Create the text we will annotate the box with:
135             string box_text = format("Prediction = %d", prediction);
136             // Calculate the position for annotated text (make sure we don't
137             // put illegal values in there):
138             int pos_x = std::max(face_i.tl().x - 10, 0);
139             int pos_y = std::max(face_i.tl().y - 10, 0);
140             // And now put it into the image:
141             putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
142         }
143         // Show the result:
144         imshow("face_recognizer", original);
145         // And display it:
146         char key = (char) waitKey(20);
147         // Exit this loop on escape:
148         if(key == 27)
149             break;
150     }
151     return 0;
152 }