title: Face recognition with OpenCV 3
body: <p>I'm trying to follow this tutorial (<a href="http://docs.opencv.org/2.4/modules/contrib/doc/facerec/tutorial/facerec_video_recognition.html" rel="nofollow noreferrer">http://docs.opencv.org/2.4/modules/contrib/doc/facerec/tutorial/facerec_video_recognition.html</a>) for my Raspberry Pi 3 but it is for OpenCV 2.4 and I already installed OpenCV 3.</p>
  
  <p>I cropped my images, created the CSV file but the problem is when I want to compile the facerec_video.cpp and I have countless of errors. I tried to debug one by one but it seems that it keeps adding errors. I tried to translate the code for OpenCV 3 with this page (<a href="http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html" rel="nofollow noreferrer">http://docs.opencv.org/master/db/dfa/tutorial_transition_guide.html</a>).</p>
  
  <p>Here are my errors :</p>
  
  <pre><code>pi@raspberrypi:~ $ g++ facerec_video.cpp -o facerec_video
  /tmp/ccZNikTK.o: In function `read_csv(std::string const&amp;, std::vector&lt;cv::Mat, std::allocator&lt;cv::Mat&gt; &gt;&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, char)':
  facerec_video.cpp:(.text+0x18c): undefined reference to `cv::error(int, cv::String const&amp;, char const*, char const*, int)'
  facerec_video.cpp:(.text+0x2a4): undefined reference to `cv::imread(cv::String const&amp;, int)'
  /tmp/ccZNikTK.o: In function `main':
  facerec_video.cpp:(.text+0x610): undefined reference to `cv::face::createFisherFaceRecognizer(int, double)'
  facerec_video.cpp:(.text+0x6b0): undefined reference to `cv::CascadeClassifier::CascadeClassifier()'
  facerec_video.cpp:(.text+0x6d8): undefined reference to `cv::CascadeClassifier::load(cv::String const&amp;)'
  facerec_video.cpp:(.text+0x6f4): undefined reference to `cv::VideoCapture::VideoCapture(int)'
  facerec_video.cpp:(.text+0x700): undefined reference to `cv::VideoCapture::isOpened() const'
  facerec_video.cpp:(.text+0x7e0): undefined reference to `cv::cvtColor(cv::_InputArray const&amp;, cv::_OutputArray const&amp;, int, int)'
  facerec_video.cpp:(.text+0x860): undefined reference to `cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&amp;, std::vector&lt;cv::Rect_&lt;int&gt;, std::allocator&lt;cv::Rect_&lt;int&gt; &gt; &gt;&amp;, double, int, int, cv::Size_&lt;int&gt;, cv::Size_&lt;int&gt;)'
  facerec_video.cpp:(.text+0x8b8): undefined reference to `cv::VideoCapture::operator&gt;&gt;(cv::Mat&amp;)'
  facerec_video.cpp:(.text+0x96c): undefined reference to `cv::resize(cv::_InputArray const&amp;, cv::_OutputArray const&amp;, cv::Size_&lt;int&gt;, double, double, int)'
  facerec_video.cpp:(.text+0x9b8): undefined reference to `cv::face::FaceRecognizer::predict(cv::_InputArray const&amp;) const'
  facerec_video.cpp:(.text+0xa4c): undefined reference to `cv::rectangle(cv::Mat&amp;, cv::Rect_&lt;int&gt;, cv::Scalar_&lt;double&gt; const&amp;, int, int, int)'
  facerec_video.cpp:(.text+0xa60): undefined reference to `cv::format(char const*, ...)'
  facerec_video.cpp:(.text+0xbcc): undefined reference to `cv::putText(cv::_InputOutputArray const&amp;, cv::String const&amp;, cv::Point_&lt;int&gt;, int, double, cv::Scalar_&lt;double&gt;, int, int, bool)'
  facerec_video.cpp:(.text+0xc78): undefined reference to `cv::imshow(cv::String const&amp;, cv::_InputArray const&amp;)'
  facerec_video.cpp:(.text+0xc98): undefined reference to `cv::waitKey(int)'
  facerec_video.cpp:(.text+0xd68): undefined reference to `cv::VideoCapture::~VideoCapture()'
  facerec_video.cpp:(.text+0xd74): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
  facerec_video.cpp:(.text+0xfcc): undefined reference to `cv::VideoCapture::~VideoCapture()'
  facerec_video.cpp:(.text+0xfdc): undefined reference to `cv::CascadeClassifier::~CascadeClassifier()'
  /tmp/ccZNikTK.o: In function `cv::String::String(char const*)':
  facerec_video.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x50): undefined reference to `cv::String::allocate(unsigned int)'
  /tmp/ccZNikTK.o: In function `cv::String::~String()':
  facerec_video.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
  /tmp/ccZNikTK.o: In function `cv::Mat::Mat(cv::Mat const&amp;)':
  facerec_video.cpp:(.text._ZN2cv3MatC2ERKS0_[_ZN2cv3MatC5ERKS0_]+0x1b0): undefined reference to `cv::Mat::copySize(cv::Mat const&amp;)'
  /tmp/ccZNikTK.o: In function `cv::Mat::~Mat()':
  facerec_video.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
  /tmp/ccZNikTK.o: In function `cv::Mat::operator=(cv::Mat const&amp;)':
  facerec_video.cpp:(.text._ZN2cv3MataSERKS0_[_ZN2cv3MataSERKS0_]+0x140): undefined reference to `cv::Mat::copySize(cv::Mat const&amp;)'
  /tmp/ccZNikTK.o: In function `cv::Mat::clone() const':
  facerec_video.cpp:(.text._ZNK2cv3Mat5cloneEv[_ZNK2cv3Mat5cloneEv]+0x38): undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&amp;) const'
  /tmp/ccZNikTK.o: In function `cv::Mat::release()':
  facerec_video.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
  /tmp/ccZNikTK.o: In function `cv::Mat::operator()(cv::Rect_&lt;int&gt; const&amp;) const':
  facerec_video.cpp:(.text._ZNK2cv3MatclERKNS_5Rect_IiEE[_ZNK2cv3MatclERKNS_5Rect_IiEE]+0x24): undefined reference to `cv::Mat::Mat(cv::Mat const&amp;, cv::Rect_&lt;int&gt; const&amp;)'
  /tmp/ccZNikTK.o: In function `cv::String::String(std::string const&amp;)':
  facerec_video.cpp:(.text._ZN2cv6StringC2ERKSs[_ZN2cv6StringC5ERKSs]+0x60): undefined reference to `cv::String::allocate(unsigned int)'
  /tmp/ccZNikTK.o:(.ARM.extab+0x1bc): undefined reference to `typeinfo for cv::Exception'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>And here is the code :</p>
  
  <pre><code>/*
   * Copyright (c) 2011. Philipp Wagner &lt;bytefish[at]gmx[dot]de&gt;.
   * 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 &lt;http://www.opensource.org/licenses/bsd-license&gt;
   */
  
  #include "opencv2/core/core.hpp"
  #include "opencv2/contrib/contrib.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/objdetect/objdetect.hpp"
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  using namespace cv;
  using namespace std;
  
  static void read_csv(const string&amp; filename, vector&lt;Mat&gt;&amp; images, vector&lt;int&gt;&amp; 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() &amp;&amp; !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 != 4) {
          cout &lt;&lt; "usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;/path/to/haar_cascade&gt; &lt;/path/to/csv.ext&gt; &lt;/path/to/device id&gt;" &lt;&lt; endl;
          cout &lt;&lt; "\t &lt;/path/to/haar_cascade&gt; -- Path to the Haar Cascade for face detection." &lt;&lt; endl;
          cout &lt;&lt; "\t &lt;/path/to/csv.ext&gt; -- Path to the CSV file with the face database." &lt;&lt; endl;
          cout &lt;&lt; "\t &lt;device id&gt; -- The webcam device id to grab frames from." &lt;&lt; endl;
          exit(1);
      }
      // Get the path to your CSV:
      string fn_haar = string(argv[1]);
      string fn_csv = string(argv[2]);
      int deviceId = atoi(argv[3]);
      // These vectors hold the images and corresponding labels:
      vector&lt;Mat&gt; images;
      vector&lt;int&gt; labels;
      // Read in the data (fails if no valid input filename is given, but you'll get an error message):
      try {
          read_csv(fn_csv, images, labels);
      } catch (cv::Exception&amp; e) {
          cerr &lt;&lt; "Error opening file \"" &lt;&lt; fn_csv &lt;&lt; "\". Reason: " &lt;&lt; e.msg &lt;&lt; endl;
          // nothing more we can do
          exit(1);
      }
      // Get the height from the first image. We'll need this
      // later in code to reshape the images to their original
      // size AND we need to reshape incoming faces to this size:
      int im_width = images[0].cols;
      int im_height = images[0].rows;
      // Create a FaceRecognizer and train it on the given images:
      Ptr&lt;FaceRecognizer&gt; model = createFisherFaceRecognizer();
      model-&gt;train(images, labels);
      // That's it for learning the Face Recognition model. You now
      // need to create the classifier for the task of Face Detection.
      // We are going to use the haar cascade you have specified in the
      // command line arguments:
      //
      CascadeClassifier haar_cascade;
      haar_cascade.load(fn_haar);
      // Get a handle to the Video device:
      VideoCapture cap(deviceId);
      // Check if we can use this device at all:
      if(!cap.isOpened()) {
          cerr &lt;&lt; "Capture Device ID " &lt;&lt; deviceId &lt;&lt; "cannot be opened." &lt;&lt; endl;
          return -1;
      }
      // Holds the current frame from the Video device:
      Mat frame;
      for(;;) {
          cap &gt;&gt; frame;
          // Clone the current frame:
          Mat original = frame.clone();
          // Convert the current frame to grayscale:
          Mat gray;
          cvtColor(original, gray, CV_BGR2GRAY);
          // Find the faces in the frame:
          vector&lt; Rect_&lt;int&gt; &gt; faces;
          haar_cascade.detectMultiScale(gray, faces);
          // At this point you have the position of the faces in
          // faces. Now we'll get the faces, make a prediction and
          // annotate it in the video. Cool or what?
          for(int i = 0; i &lt; faces.size(); i++) {
              // Process face by face:
              Rect face_i = faces[i];
              // Crop the face from the image. So simple with OpenCV C++:
              Mat face = gray(face_i);
              // Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
              // verify this, by reading through the face recognition tutorial coming with OpenCV.
              // Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
              // input data really depends on the algorithm used.
              //
              // I strongly encourage you to play around with the algorithms. See which work best
              // in your scenario, LBPH should always be a contender for robust face recognition.
              //
              // Since I am showing the Fisherfaces algorithm here, I also show how to resize the
              // face you have just found:
              Mat face_resized;
              cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
              // Now perform the prediction, see how easy that is:
              int prediction = model-&gt;predict(face_resized);
              // And finally write all we've found out to the original image!
              // First of all draw a green rectangle around the detected face:
              rectangle(original, face_i, CV_RGB(0, 255,0), 1);
              // Create the text we will annotate the box with:
              string box_text = format("Prediction = %d", prediction);
              // Calculate the position for annotated text (make sure we don't
              // put illegal values in there):
              int pos_x = std::max(face_i.tl().x - 10, 0);
              int pos_y = std::max(face_i.tl().y - 10, 0);
              // And now put it into the image:
              putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
          }
          // Show the result:
          imshow("face_recognizer", original);
          // And display it:
          char key = (char) waitKey(20);
          // Exit this loop on escape:
          if(key == 27)
              break;
      }
      return 0;
  }
  </code></pre>
  
  <p>If someone could tell me what I need to do in order to translate the code for OpenCV 3 that would be amazing !</p>
  
  <p>Thanks !</p>