1 Cascade Classifier {#tutorial_cascade_classifier} 2 ================== 3 4 Goal 5 ---- 6 7 In this tutorial you will learn how to: 8 9 - Use the @ref cv::CascadeClassifier class to detect objects in a video stream. Particularly, we 10 will use the functions: 11 - @ref cv::CascadeClassifier::load to load a .xml classifier file. It can be either a Haar or a LBP classifer 12 - @ref cv::CascadeClassifier::detectMultiScale to perform the detection. 13 14 Theory 15 ------ 16 17 Code 18 ---- 19 20 This tutorial code's is shown lines below. You can also download it from 21 [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/objectDetection/objectDetection.cpp) 22 . The second version (using LBP for face detection) can be [found 23 here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/objectDetection/objectDetection2.cpp) 24 @code{.cpp} 25 #include "opencv2/objdetect.hpp" 26 #include "opencv2/highgui.hpp" 27 #include "opencv2/imgproc.hpp" 28 29 #include <iostream> 30 #include <stdio.h> 31 32 using namespace std; 33 using namespace cv; 34 35 /* Function Headers */ 36 void detectAndDisplay( Mat frame ); 37 38 /* Global variables */ 39 String face_cascade_name = "haarcascade_frontalface_alt.xml"; 40 String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml"; 41 CascadeClassifier face_cascade; 42 CascadeClassifier eyes_cascade; 43 String window_name = "Capture - Face detection"; 44 45 /* @function main */ 46 int main( void ) 47 { 48 VideoCapture capture; 49 Mat frame; 50 51 //-- 1. Load the cascades 52 if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; }; 53 if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; }; 54 55 //-- 2. Read the video stream 56 capture.open( -1 ); 57 if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; } 58 59 while ( capture.read(frame) ) 60 { 61 if( frame.empty() ) 62 { 63 printf(" --(!) No captured frame -- Break!"); 64 break; 65 } 66 67 //-- 3. Apply the classifier to the frame 68 detectAndDisplay( frame ); 69 70 int c = waitKey(10); 71 if( (char)c == 27 ) { break; } // escape 72 } 73 return 0; 74 } 75 76 /* @function detectAndDisplay */ 77 void detectAndDisplay( Mat frame ) 78 { 79 std::vector<Rect> faces; 80 Mat frame_gray; 81 82 cvtColor( frame, frame_gray, COLOR_BGR2GRAY ); 83 equalizeHist( frame_gray, frame_gray ); 84 85 //-- Detect faces 86 face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) ); 87 88 for( size_t i = 0; i < faces.size(); i++ ) 89 { 90 Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 ); 91 ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 ); 92 93 Mat faceROI = frame_gray( faces[i] ); 94 std::vector<Rect> eyes; 95 96 //-- In each face, detect eyes 97 eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(30, 30) ); 98 99 for( size_t j = 0; j < eyes.size(); j++ ) 100 { 101 Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 ); 102 int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 ); 103 circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 ); 104 } 105 } 106 //-- Show what you got 107 imshow( window_name, frame ); 108 } 109 @endcode 110 Explanation 111 ----------- 112 113 Result 114 ------ 115 116 -# Here is the result of running the code above and using as input the video stream of a build-in 117 webcam: 118 119 ![](images/Cascade_Classifier_Tutorial_Result_Haar.jpg) 120 121 Remember to copy the files *haarcascade_frontalface_alt.xml* and 122 *haarcascade_eye_tree_eyeglasses.xml* in your current directory. They are located in 123 *opencv/data/haarcascades* 124 125 -# This is the result of using the file *lbpcascade_frontalface.xml* (LBP trained) for the face 126 detection. For the eyes we keep using the file used in the tutorial. 127 128 ![](images/Cascade_Classifier_Tutorial_Result_LBP.jpg) 129