Faster face detection by varying detectMultiScale function parameters

In example 1, a simple face detection code is provided which runs at 1-2 frames per second.

To make it faster, we can modify the parameters to detectMultiScale function to achieve very high frame rate.
Example 2 provides the code for minor optimization of code to make it considerably reducing the size of image for detecting face and remapping the face Region Of Interest (ROI) in the original image.

Here the last two parameters - Size minSize=Size() and Size maxSize=Size() - are adaptively changed to reduce computational complexity. These parameters are responsible for restricting the search window of the function to vary from minimum size(minSize) to maximum size(maxSize) for face detection.

Note : You need  a camera connected, otherwise it will throw an error.

Example 1: (Face detection from Video captured from camera)

--------------
#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;

Mat detectFace(Mat src);

int main( )
{
 VideoCapture cap(0);
 namedWindow( "window1", 1 );   

 while(1)
 {
  Mat frame;
  cap >> frame;         
  frame=detectFace(frame);
  
  imshow( "window1", frame );
  // Press 'c' to escape
  if(waitKey(1) == 'c') break;  
 }

 waitKey(0);                  
 return 0;
}

Mat detectFace(Mat image)
{
 // Load Face cascade (.xml file)
 CascadeClassifier face_cascade( "C:/OpenCV243/data/Haarcascades/haarcascade_frontalface_alt2.xml" );

 // 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 );
 } 
 return image;
}
--------------

Example 2: (Optimizing the parameters of detectMultiScale for faster operation)

--------------
#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;

double min_face_size=20;
double max_face_size=200;

Mat detectFace(Mat src);

int main( )
{
 VideoCapture cap(0);
 namedWindow( "window1", 1 );   

 while(1)
 {
  Mat frame;
  cap >> frame;         
  frame=detectFace(frame);
  
  imshow( "window1", frame );
  // Press 'c' to escape
  if(waitKey(1) == 'c') break;  
 }

 waitKey(0);                  
 return 0;
}

Mat detectFace(Mat image)
{
 // Load Face cascade (.xml file)
 CascadeClassifier face_cascade( "C:/OpenCV243/data/Haarcascades/haarcascade_frontalface_alt2.xml" );

 // Detect faces
 std::vector<Rect> faces;
 
 face_cascade.detectMultiScale( image, faces, 1.2, 2, 0|CV_HAAR_SCALE_IMAGE, Size(min_face_size, min_face_size),Size(max_face_size, max_face_size) );
 
 // Draw circles on the detected faces
 for( int i = 0; i < faces.size(); i++ )
 { 
  min_face_size = faces[0].width*0.8;
  max_face_size = faces[0].width*1.2;
  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 );
 } 
 return image;
}
--------------


11 comments:

  1. Thank you so much. thank you. But i have one question: How to crop the ellipse area?

    ReplyDelete
    Replies
    1. please visit this page for your answer...
      http://opencvexamples.blogspot.com/2014/02/crop-elliptical-region-from-image.html

      Delete
  2. It works perfect when we need it, so thanks for providing such brief examples.
    http://algorithm-research.weebly.com

    ReplyDelete
  3. Hello! I got the following error message when I run the code:

    OpenCV Error: Assertion failed (axes.width >= 0 && axes.height >= 0 && thickness <= 255 &&
    ilds\master_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\drawing.cpp, line 1772

    Would you please help me? Thanks so much in advance!

    ReplyDelete
    Replies
    1. here again the whole error message:
      OpenCV Error: Assertion failed (axes.width >= 0 && axes.height >= 0 && thickness <= 255 && 0 <= shift && shift <= XY_SHIFT) in cv::ellipse, file C:\bu
      ilds\master_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\drawing.cpp, line 1772

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thank you so much. thank you. It works perfect, But it is not drawing circles on the detected faces. In my case videos from my webcam.

    ReplyDelete
  6. Thanks but I don't see where you set the ROI, only that you specified the min and max detection size and increased the detection scale a bit.

    ReplyDelete
  7. How fast it is in second?

    ReplyDelete
  8. How to modify parameters if the feature detection is by HOG?

    ReplyDelete