You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 115-detecting-faces-in-an-image-with-opencv/index.html
+77-76
Original file line number
Diff line number
Diff line change
@@ -90,82 +90,83 @@ <h1>11.5. Detecting faces in an image with OpenCV</h1>
90
90
▶ <em><ahref="https://github.com/ipython-books/cookbook-2nd-code">Code on GitHub</a> with a <ahref="https://opensource.org/licenses/MIT">MIT license</a></em></p>
91
91
<p>▶ <ahref="http://ipython-books.github.io/chapter-11-image-and-audio-processing/"><strong><em>Go to</em></strong><em>Chapter 11 : Image and Audio Processing</em></a><br/>
92
92
▶ <ahref="https://github.com/ipython-books/cookbook-2nd-code/blob/master/chapter11_image/05_faces.ipynb"><em><strong>Get</strong> the Jupyter notebook</em></a></p>
93
-
<p><strong>OpenCV (Open Computer Vision)</strong> is an open source C++ library for computer vision. It features algorithms for image segmentation, object recognition, augmented reality, face detection, and other computer vision tasks.</p>
94
-
<p>In this recipe, we will use OpenCV in Python to detect faces in a picture.</p>
95
-
<h2>Getting ready</h2>
96
-
<p>You need OpenCV and the Python wrapper. You can install them with <code>conda install -c conda-forge opencv</code>.</p>
97
-
<h2>How to do it...</h2>
98
-
<p><strong>1. </strong> First, we import the packages:</p>
<p><strong>4. </strong> Then, we convert it to a grayscale image using OpenCV's <code>cvtColor()</code> function. For face detection, it is sufficient and faster to use grayscale images.</p>
<p><strong>5. </strong> To detect faces, we will use the <strong>Viola–Jones object detection framework</strong>. A cascade of Haar-like classifiers has been trained on many images to detect faces (more details are given in the next section). The result of the training is stored in an XML file which is part of the archive that was downloaded in step 2. We load this cascade from this XML file with OpenCV's <code>CascadeClassifier</code> class:</p>
<p><strong>6. </strong> Finally, the <code>detectMultiScale()</code> method of the classifier detects the objects on a grayscale image and returns a list of rectangles around these objects:</p>
<p><imgalt="<matplotlib.figure.Figure at 0x7ef02b0>" src="http://ipython-books.github.io/pages/chapter11_image/05_faces_files/05_faces_17_0.png" /></p>
152
-
<p>We see that, although all detected objects are indeed faces, one face out of four is not detected. This is probably due to the fact that this face is not perfectly facing the camera, whereas the faces in the training set were. This shows that the efficacy of this method is limited by the quality and generality of the training set.</p>
153
-
<h2>How it works...</h2>
154
-
<p>The Viola–Jones object detection framework works by training a cascade of boosted classifiers with Haar-like features. First, we consider a set of features:</p>
<p>A feature is positioned at a particular location and size in the image. It covers a small window in the image (for example, 24 x 24 pixels). The sum of all pixels in the black area is subtracted to the sum of the pixels in the white area. This operation can be done efficiently with integral images.</p>
157
-
<p>Then, the set of all classifiers is trained with a boosting technique; only the best features are kept for the next stage during training. The training set contains positive and negative images (with and without faces). Although the classifiers yield poor performance individually, the cascade of boosted classifiers is both efficient and fast. This method is therefore well-adapted to real-time processing.</p>
158
-
<p>The XML file has been obtained in OpenCV's package. There are multiple files corresponding to different training sets. You can also train your own cascade with your own training set.</p>
159
-
<h2>There's more...</h2>
160
-
<p>Here are a few references:</p>
161
-
<ul>
162
-
<li>A cascade tutorial with OpenCV (C++) available at <ahref="http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html">http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html</a></li>
163
-
<li>Documentation to train a cascade, available at <ahref="http://docs.opencv.org/doc/user_guide/ug_traincascade.html">http://docs.opencv.org/doc/user_guide/ug_traincascade.html</a></li>
164
-
<li>Haar cascades library, available at <ahref="https://github.com/Itseez/opencv/tree/master/data/haarcascades">https://github.com/Itseez/opencv/tree/master/data/haarcascades</a></li>
165
-
<li>OpenCV's cascade classification API reference available at <ahref="http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html">http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html</a></li>
166
-
<li>The Viola–Jones object detection framework on Wikipedia, available at <ahref="https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework">https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework</a></li>
167
-
<li>Boosting or how to create one strong classifier from many weak classifiers, explained at <ahref="https://en.wikipedia.org/wiki/Boosting_%28machine_learning%29">https://en.wikipedia.org/wiki/Boosting_%28machine_learning%29</a></li>
168
-
</ul>
93
+
<p><ahref="https://www.packtpub.com/big-data-and-business-intelligence/ipython-interactive-computing-and-visualization-cookbook-second-e">The recipe is available in the book, to be purchased on Packt.</a></p>
94
+
<!-- REMOVE AS PER PACKT AGREEMENT
95
+
96
+
**OpenCV (Open Computer Vision)** is an open source C++ library for computer vision. It features algorithms for image segmentation, object recognition, augmented reality, face detection, and other computer vision tasks.
97
+
98
+
In this recipe, we will use OpenCV in Python to detect faces in a picture.
99
+
100
+
## Getting ready
101
+
102
+
You need OpenCV and the Python wrapper. You can install them with `conda install -c conda-forge opencv`.
103
+
104
+
## How to do it...
105
+
106
+
**1. ** First, we import the packages:
107
+
108
+
109
+
wzxhzdk:0
110
+
111
+
112
+
**2. ** We download and extract the dataset in the `data/` subfolder:
113
+
114
+
115
+
wzxhzdk:1
116
+
117
+
118
+
**3. ** We open the JPG image with OpenCV:
119
+
120
+
121
+
wzxhzdk:2
122
+
123
+
124
+
**4. ** Then, we convert it to a grayscale image using OpenCV's `cvtColor()` function. For face detection, it is sufficient and faster to use grayscale images.
125
+
126
+
127
+
wzxhzdk:3
128
+
129
+
130
+
**5. ** To detect faces, we will use the **Viola–Jones object detection framework**. A cascade of Haar-like classifiers has been trained on many images to detect faces (more details are given in the next section). The result of the training is stored in an XML file which is part of the archive that was downloaded in step 2. We load this cascade from this XML file with OpenCV's `CascadeClassifier` class:
131
+
132
+
133
+
wzxhzdk:4
134
+
135
+
136
+
**6. ** Finally, the `detectMultiScale()` method of the classifier detects the objects on a grayscale image and returns a list of rectangles around these objects:
137
+
138
+
139
+
wzxhzdk:5
140
+
141
+
142
+

143
+
144
+
We see that, although all detected objects are indeed faces, one face out of four is not detected. This is probably due to the fact that this face is not perfectly facing the camera, whereas the faces in the training set were. This shows that the efficacy of this method is limited by the quality and generality of the training set.
145
+
146
+
## How it works...
147
+
148
+
The Viola–Jones object detection framework works by training a cascade of boosted classifiers with Haar-like features. First, we consider a set of features:
A feature is positioned at a particular location and size in the image. It covers a small window in the image (for example, 24 x 24 pixels). The sum of all pixels in the black area is subtracted to the sum of the pixels in the white area. This operation can be done efficiently with integral images.
153
+
154
+
Then, the set of all classifiers is trained with a boosting technique; only the best features are kept for the next stage during training. The training set contains positive and negative images (with and without faces). Although the classifiers yield poor performance individually, the cascade of boosted classifiers is both efficient and fast. This method is therefore well-adapted to real-time processing.
155
+
156
+
The XML file has been obtained in OpenCV's package. There are multiple files corresponding to different training sets. You can also train your own cascade with your own training set.
157
+
158
+
## There's more...
159
+
160
+
Here are a few references:
161
+
162
+
* A cascade tutorial with OpenCV (C++) available at [http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html](http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html)
163
+
* Documentation to train a cascade, available at [http://docs.opencv.org/doc/user_guide/ug_traincascade.html](http://docs.opencv.org/doc/user_guide/ug_traincascade.html)
164
+
* Haar cascades library, available at [https://github.com/Itseez/opencv/tree/master/data/haarcascades](https://github.com/Itseez/opencv/tree/master/data/haarcascades)
165
+
* OpenCV's cascade classification API reference available at [http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html](http://docs.opencv.org/modules/objdetect/doc/cascade_classification.html)
166
+
* The Viola–Jones object detection framework on Wikipedia, available at [https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework](https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework)
167
+
* Boosting or how to create one strong classifier from many weak classifiers, explained at [https://en.wikipedia.org/wiki/Boosting_%28machine_learning%29](https://en.wikipedia.org/wiki/Boosting_%28machine_learning%29)
0 commit comments