This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Face Detection using OpenCV

By: karanjthakkar on Oct 31st, 2012  |  syntax: C++  |  size: 3.53 KB  |  views: 3,707  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * @file facedetect.cpp
  3.  * @author Karan Thakkar
  4.  * @brief Uses the Haar Cascade Classifier to detect face and eyes in a video feed
  5.  */
  6.  
  7. #include "stdafx.h"
  8. #include "opencv2/objdetect/objdetect.hpp"
  9. #include "opencv2/highgui/highgui.hpp"
  10. #include "opencv2/imgproc/imgproc.hpp"
  11.  
  12. #include <iostream>
  13.  
  14. using namespace std;
  15. using namespace cv;
  16.  
  17. /** Function Headers */
  18. void detectAndDisplay(IplImage* frame );
  19.  
  20. /** Global variables */
  21. CvHaarClassifierCascade* face_cascade = 0;
  22. CvHaarClassifierCascade* eye_cascade = 0;
  23. CvMemStorage* pStorageface = 0;
  24. CvMemStorage* pStorageeye = 0;        // memory for detector to use
  25.    
  26. RNG rng(12345);
  27.  
  28. /**
  29.  * @function main
  30.  */
  31. int main( int argc, const char** argv )
  32. {
  33.   CvCapture* capture;
  34.   IplImage* frame = 0;
  35.  
  36.   //-- 1. Load the cascade
  37.   face_cascade = (CvHaarClassifierCascade *)cvLoad("C:\\OpenCV-2.4.2\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml", 0 , 0, 0);
  38.   eye_cascade = (CvHaarClassifierCascade *)cvLoad("C:\\OpenCV-2.4.2\\opencv\\data\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml", 0 , 0, 0);
  39.  
  40.   while( true )
  41.     {
  42.      
  43.                 //-- 2. Read the video stream
  44.                 capture = cvCaptureFromCAM(1);
  45.                 frame = cvQueryFrame( capture );
  46.  
  47.       //-- 3. Apply the classifier to the frame
  48.           detectAndDisplay(frame);
  49.  
  50.       int c = waitKey(10);
  51.       if( (char)c == 27 ) { exit(0); }
  52.  
  53.     }
  54.  
  55.   // clean up and release resources
  56.     cvReleaseImage(&frame);
  57.         if(face_cascade) cvReleaseHaarClassifierCascade(&face_cascade);
  58.         if(pStorageface) cvReleaseMemStorage(&pStorageface);
  59.         if(pStorageeye) cvReleaseMemStorage(&pStorageeye);
  60.  
  61.         return 0;
  62.  
  63. }
  64.  
  65. /**
  66.  * @function detectAndDisplay
  67.  */
  68. void detectAndDisplay(IplImage* frame )
  69. {
  70.    
  71.    CvSeq * pFaceRectSeq;               // memory-access interface
  72.    CvSeq * pEyeRectSeq;
  73.    pStorageface = cvCreateMemStorage(0);
  74.    
  75.     // detect faces in image
  76.         pFaceRectSeq = cvHaarDetectObjects
  77.                 (frame, face_cascade, pStorageface,
  78.                 1.1,                       // increase search scale by 10% each pass
  79.                 3,                         // merge groups of three detections
  80.                 CV_HAAR_DO_CANNY_PRUNING,  // skip regions unlikely to contain a face
  81.                 cvSize(40,40));            // smallest size face to detect = 40x40
  82.  
  83.         pStorageeye = cvCreateMemStorage(0);
  84.    
  85.     // detect faces in image
  86.         pEyeRectSeq = cvHaarDetectObjects
  87.                 (frame, eye_cascade, pStorageeye,
  88.                 1.1,                       // increase search scale by 10% each pass
  89.                 3,                         // merge groups of three detections
  90.                 CV_HAAR_DO_CANNY_PRUNING,  // skip regions unlikely to contain a face
  91.                 cvSize(40,40));            // smallest size face to detect = 40x40
  92.  
  93.  
  94.    const char * DISPLAY_WINDOW = "Haar Window";
  95.         int i;
  96.        
  97.         // create a window to display detected faces
  98.         cvNamedWindow(DISPLAY_WINDOW, CV_WINDOW_AUTOSIZE);
  99.  
  100.         // draw a rectangular outline around each detection
  101.         for(i=0;i<(pFaceRectSeq? pFaceRectSeq->total:0); i++ )
  102.         {
  103.                 CvRect* r = (CvRect*)cvGetSeqElem(pFaceRectSeq, i);
  104.                 CvPoint pt1 = { r->x, r->y };
  105.                 CvPoint pt2 = { r->x + r->width, r->y + r->height };
  106.                 cvRectangle(frame, pt1, pt2, CV_RGB(255,255,255), 3, 4, 0);
  107.         }
  108.  
  109.         // draw a rectangular outline around each detection
  110.         for(i=0;i<(pEyeRectSeq? pEyeRectSeq->total:0); i++ )
  111.         {
  112.                 CvRect* r = (CvRect*)cvGetSeqElem(pEyeRectSeq, i);
  113.                 CvPoint pt1 = { r->x, r->y };
  114.                 CvPoint pt2 = { r->x + r->width, r->y + r->height };
  115.                 cvRectangle(frame, pt1, pt2, CV_RGB(255,255,255), 3, 4, 0);
  116.         }
  117.  
  118.         // display face detections
  119.         cvShowImage(DISPLAY_WINDOW, frame);
  120. }
clone this paste RAW Paste Data