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


17 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
  9. High quality in cleaning It is sure that in the vicinity of cleaning company in Taif we are specialized companies in all types of cleaning operations from the exterior and exposed to dust and weather factors by cars and wind, whether for villas or buildings, hotels and storefronts to the premises and clean and clear the floors شركة تنظيف بالطائف .

    Steam cleaning is very useful in cases of fabrics that are difficult to rub as they are exposed to rapid damage and in the past days before the development occurred in the cleaning operations was placed detergent on the fabrics and leave a period and then remove the dirt and that leaves a kind of problems on the furnishings شركة تنظيف بالبخار بالطائف .

    The best insulation materials are used according to the quality of the place in which the house is located. If you are in a high temperature zone, two types of insulation will be carried out: thermal insulation at the top and bottom شركة عزل اسطح بالطائف

    شركة نقل عفش بالطائف Each house is not without a variety of brushes in the interior and each period wishing to those inside the house in the renovation and transfer of the garbage شركة نقل عفش بمكة .

    When we are exposed to insects in the place should be disposed of immediately and will be easily with an شركة مكافحة حشرات بالطائف we are working to provide serious types of organic pesticides that protect all types of insects .

    ReplyDelete
  10. اصلاح سريع وشامل لاعطال التكييفات الان مع مراكز صيانة تكييفات يونيون اير التى تضمن لكم الحصول علي مستوي احترافي من خدمات صيانة يونيون اير بواسطة فريق متميز من فنيين ومهندسيين الصيانة المتوافرين في فروع شركة يونيون اير لاصلاح وصيانة كافه الاجهزة الكهربائية المعطلة
    http://unionairemaintenance.com

    ReplyDelete
  11. Looking for help with your thesis? Someone to Write My Thesis USA as well as acess Best Thesis Writing Help From the leading Affordable Writing Services Online And get your assignment done on time.

    ReplyDelete
  12. AR Pest Control Mississauga has developed the best solution and methodology for a wide range of occasional invaders. We realize that every client has individual needs and issues. Our team promises to complete our work in a timely manner that is the least inconvenience to you.
    Pest Control Brampton

    ReplyDelete
  13. Airport Limo Service is a transportation service that offers luxurious and comfortable limousine rides to and from the airport for both individuals and groups. the company prides itself on providing premium transportation services to travelers who value comfort, safety, and reliability.
    Pickering Airport Limo

    ReplyDelete