Friday, 18 March 2016

OpenCV C++ Code for Real Time Face Detection Using Haar Cascade

In the Previous Tutorial we learnt about Haar Training and How to detect faces using Haar Cascade Classifier from images.
Refer:
Opencv C++ Tutorial of Face(object) Detection Using Haar Cascade
But in many of the real life application,we need to detect faces (objects) live either from video or from webcam.
Thus this Opencv C++ Tutorial is about doing real time face detection using Haar Cascade.
Note:-
The same code can be used for doing real time object detection by using its corresponding Haar Cacade XML file
Here is the Opencv C++ Example of face detection from a Webcam
//Opencv C++ Example on Real time Face Detection Using Haar Cascade Classifier
 
/*We can similarly train our own Haar Classifier and Detect any object which we want
Only Thing is we need to load our Classifier in place of cascade_frontalface_alt2.xml */
 
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
  
#include <iostream>
#include <stdio.h>
  
using namespace std;
using namespace cv;
  
int main( )
{
 VideoCapture capture(0);  
    if (!capture.isOpened())  
    throw "Error when reading file";  
    namedWindow("window", 1);  
    for (;;)
     { 
       Mat image; 
       capture >> image;  
       if (image.empty())  
       break; 

       // Load Face cascade (.xml file)
       CascadeClassifier face_cascade;
       face_cascade.load( "D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml" );
       if(!face_cascade.load("D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"))
       {
         cerr<<"Error Loading XML file"<<endl;
         return 0;
       }
 
      // Detect faces
      std::vector<Rect> faces;
      face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  
      // Draw circles on the detected faces
      for( int i = 0; i < faces.size(); i++ )
      {
        Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        ellipse( image, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
      }
      
  imshow( "Detected Face", image );
  waitKey(1);  
   }  
               
     return 0;
}

Note:- In the Previous code ,we are loading the Haar Cascade XML file again and again for every frame in the Video
We can ,initialize and load it before the start of Videocapture() function, thus improving the efficieny of our program considerably.
Refer the OpenCV C++ Code below:-
//Opencv C++ Example on Real Time Face Detection from a Video/Webcam Using Haar Cascade
 
/*We can similarly train our own Haar Classifier and Detect any object which we want
Only Thing is we need to load our Classifier in palce of cascade_frontalface_alt2.xml */
 
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
  
#include <iostream>
#include <stdio.h>
  
using namespace std;
using namespace cv;
  
int main( )
{
   // Load Face cascade (.xml file)
       CascadeClassifier face_cascade;
       face_cascade.load( "D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml" );
       if(!face_cascade.load("D:\\opencv2410\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml"))
       {
         cerr<<"Error Loading XML file"<<endl;
         return 0;
       }

 VideoCapture capture(0);  
    if (!capture.isOpened())  
    throw "Error when reading file";  
    namedWindow("window", 1);  
    for (;;)
     { 
       Mat image; 
       capture >> image;  
       if (image.empty())  
       break; 

      // Detect faces
      std::vector<Rect> faces;
      face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  
      // Draw circles on the detected faces
      for( int i = 0; i < faces.size(); i++ )
      {
        Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
        ellipse( image, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );
      }
      
  imshow( "Detected Face", image );
  waitKey(1);  
   }  
               
     return 0;
}
Note:
Haar is not Rotation Invariant.Thus if we rotate our Face a little,it wont be able to detect our faces.
Compare Below:
Face Detection Using Haar Cascade without Rotation
Face Detection Using Haar Cascade with Rotation

No comments:

Post a Comment