Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / objdetect / cascade_classifier / cascade_classifier.markdown
1 Cascade Classifier {#tutorial_cascade_classifier}
2 ==================
3
4 @next_tutorial{tutorial_traincascade}
5
6 Goal
7 ----
8
9 In this tutorial,
10
11 -   We will learn how the Haar cascade object detection works.
12 -   We will see the basics of face detection and eye detection using the Haar Feature-based Cascade Classifiers
13 -   We will use the @ref cv::CascadeClassifier class to detect objects in a video stream. Particularly, we
14     will use the functions:
15     -   @ref cv::CascadeClassifier::load to load a .xml classifier file. It can be either a Haar or a LBP classifier
16     -   @ref cv::CascadeClassifier::detectMultiScale to perform the detection.
17
18 Theory
19 ------
20
21 Object Detection using Haar feature-based cascade classifiers is an effective object detection
22 method proposed by Paul Viola and Michael Jones in their paper, "Rapid Object Detection using a
23 Boosted Cascade of Simple Features" in 2001. It is a machine learning based approach where a cascade
24 function is trained from a lot of positive and negative images. It is then used to detect objects in
25 other images.
26
27 Here we will work with face detection. Initially, the algorithm needs a lot of positive images
28 (images of faces) and negative images (images without faces) to train the classifier. Then we need
29 to extract features from it. For this, Haar features shown in the below image are used. They are just
30 like our convolutional kernel. Each feature is a single value obtained by subtracting sum of pixels
31 under the white rectangle from sum of pixels under the black rectangle.
32
33 ![image](images/haar_features.jpg)
34
35 Now, all possible sizes and locations of each kernel are used to calculate lots of features. (Just
36 imagine how much computation it needs? Even a 24x24 window results over 160000 features). For each
37 feature calculation, we need to find the sum of the pixels under white and black rectangles. To solve
38 this, they introduced the integral image. However large your image, it reduces the calculations for a
39 given pixel to an operation involving just four pixels. Nice, isn't it? It makes things super-fast.
40
41 But among all these features we calculated, most of them are irrelevant. For example, consider the
42 image below. The top row shows two good features. The first feature selected seems to focus on the
43 property that the region of the eyes is often darker than the region of the nose and cheeks. The
44 second feature selected relies on the property that the eyes are darker than the bridge of the nose.
45 But the same windows applied to cheeks or any other place is irrelevant. So how do we select the
46 best features out of 160000+ features? It is achieved by **Adaboost**.
47
48 ![image](images/haar.png)
49
50 For this, we apply each and every feature on all the training images. For each feature, it finds the
51 best threshold which will classify the faces to positive and negative. Obviously, there will be
52 errors or misclassifications. We select the features with minimum error rate, which means they are
53 the features that most accurately classify the face and non-face images. (The process is not as simple as
54 this. Each image is given an equal weight in the beginning. After each classification, weights of
55 misclassified images are increased. Then the same process is done. New error rates are calculated.
56 Also new weights. The process is continued until the required accuracy or error rate is achieved or
57 the required number of features are found).
58
59 The final classifier is a weighted sum of these weak classifiers. It is called weak because it alone
60 can't classify the image, but together with others forms a strong classifier. The paper says even
61 200 features provide detection with 95% accuracy. Their final setup had around 6000 features.
62 (Imagine a reduction from 160000+ features to 6000 features. That is a big gain).
63
64 So now you take an image. Take each 24x24 window. Apply 6000 features to it. Check if it is face or
65 not. Wow.. Isn't it a little inefficient and time consuming? Yes, it is. The authors have a good
66 solution for that.
67
68 In an image, most of the image is non-face region. So it is a better idea to have a simple
69 method to check if a window is not a face region. If it is not, discard it in a single shot, and don't
70 process it again. Instead, focus on regions where there can be a face. This way, we spend more time
71 checking possible face regions.
72
73 For this they introduced the concept of **Cascade of Classifiers**. Instead of applying all 6000
74 features on a window, the features are grouped into different stages of classifiers and applied one-by-one.
75 (Normally the first few stages will contain very many fewer features). If a window fails the first
76 stage, discard it. We don't consider the remaining features on it. If it passes, apply the second stage
77 of features and continue the process. The window which passes all stages is a face region. How is
78 that plan!
79
80 The authors' detector had 6000+ features with 38 stages with 1, 10, 25, 25 and 50 features in the first five
81 stages. (The two features in the above image are actually obtained as the best two features from
82 Adaboost). According to the authors, on average 10 features out of 6000+ are evaluated per
83 sub-window.
84
85 So this is a simple intuitive explanation of how Viola-Jones face detection works. Read the paper for
86 more details or check out the references in the Additional Resources section.
87
88 Haar-cascade Detection in OpenCV
89 --------------------------------
90 OpenCV provides a training method (see @ref tutorial_traincascade) or pretrained models, that can be read using the @ref cv::CascadeClassifier::load method.
91 The pretrained models are located in the data folder in the OpenCV installation or can be found [here](https://github.com/opencv/opencv/tree/3.4/data).
92
93 The following code example will use pretrained Haar cascade models to detect faces and eyes in an image.
94 First, a @ref cv::CascadeClassifier is created and the necessary XML file is loaded using the @ref cv::CascadeClassifier::load method.
95 Afterwards, the detection is done using the @ref cv::CascadeClassifier::detectMultiScale method, which returns boundary rectangles for the detected faces or eyes.
96
97 @add_toggle_cpp
98 This tutorial code's is shown lines below. You can also download it from
99 [here](https://github.com/opencv/opencv/tree/3.4/samples/cpp/tutorial_code/objectDetection/objectDetection.cpp)
100 @include samples/cpp/tutorial_code/objectDetection/objectDetection.cpp
101 @end_toggle
102
103 @add_toggle_java
104 This tutorial code's is shown lines below. You can also download it from
105 [here](https://github.com/opencv/opencv/tree/3.4/samples/java/tutorial_code/objectDetection/cascade_classifier/ObjectDetectionDemo.java)
106 @include samples/java/tutorial_code/objectDetection/cascade_classifier/ObjectDetectionDemo.java
107 @end_toggle
108
109 @add_toggle_python
110 This tutorial code's is shown lines below. You can also download it from
111 [here](https://github.com/opencv/opencv/tree/3.4/samples/python/tutorial_code/objectDetection/cascade_classifier/objectDetection.py)
112 @include samples/python/tutorial_code/objectDetection/cascade_classifier/objectDetection.py
113 @end_toggle
114
115 Result
116 ------
117
118 -#  Here is the result of running the code above and using as input the video stream of a built-in
119     webcam:
120
121     ![](images/Cascade_Classifier_Tutorial_Result_Haar.jpg)
122
123     Be sure the program will find the path of files *haarcascade_frontalface_alt.xml* and
124     *haarcascade_eye_tree_eyeglasses.xml*. They are located in
125     *opencv/data/haarcascades*
126
127 -#  This is the result of using the file *lbpcascade_frontalface.xml* (LBP trained) for the face
128     detection. For the eyes we keep using the file used in the tutorial.
129
130     ![](images/Cascade_Classifier_Tutorial_Result_LBP.jpg)
131
132 Additional Resources
133 --------------------
134
135 -#  Paul Viola and Michael J. Jones. Robust real-time face detection. International Journal of Computer Vision, 57(2):137ā€“154, 2004. @cite Viola04
136 -#  Rainer Lienhart and Jochen Maydt. An extended set of haar-like features for rapid object detection. In Image Processing. 2002. Proceedings. 2002 International Conference on, volume 1, pages Iā€“900. IEEE, 2002. @cite Lienhart02
137 -#  Video Lecture on [Face Detection and Tracking](https://www.youtube.com/watch?v=WfdYYNamHZ8)
138 -#  An interesting interview regarding Face Detection by [Adam
139     Harvey](https://web.archive.org/web/20171204220159/http://www.makematics.com/research/viola-jones/)
140 -#  [OpenCV Face Detection: Visualized](https://vimeo.com/12774628) on Vimeo by Adam Harvey