facerec_video.cpp 6.7 KB

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