Using the Viola-Jones Framework
Training a Viola-Jones classifier from scratch can take a long time. Fortunately, a pre-trained Viola-Jones classifier comes out-of-the-box with OpenCV! You will use that one to see the algorithm in action. If you really want to train the classifier yourself,
scikit-image offers a tutorial with the accompanying code on their website.
To follow the exercise in this lesson, you’re going to need to dowload the following image:
You’ll also need to go to the OpenCV GitHub repo.
00:00 You’ll need a few critical files in order to follow along. First, grab this image from the link below this video. This is a stock image that we’re going to run through the face detection algorithm to hopefully get some faces. Now, head over to this GitHub repository linked down below.
This is the official OpenCV repo. Download the file named
haarcascade_frontalface_alt.xml. Rather than manually training the algorithm with lots of training data, we’re going to use the file which has all of our weak classifiers nicely arranged into a single cascade.
00:54 This is going to allow us to create a virtual environment for all of our project’s packages and other various dependencies. Conda comes with either Anaconda or Miniconda, and either should work fine for this project. All right.
The first one is
scikit-learn, which I think is pronounced that way. And so I’ll type
conda install scikit-learn, and make sure you allow it to install whatever packages it needs. Next is
scikit-image, so I’ll type
conda install—and I want to get this from
scikit-image. There we go. Finally, I’ll grab OpenCV by typing
conda install -c menpo opencv3.
Now that we’ve got everything we need, we need to actually create our Python file. I’ll choose File > New File and I’ll name this
program.py—very original, I know. In Visual Studio, I’m going to click on this button down here in the bottom-left corner to change my Python interpreter, and I’m going to choose the one that we just installed with Conda.
passing in the path to our image. Next, we need to convert this image to grayscale for the algorithm to process it, so we’ll get that with
cv.cvtColor(), passing in the
cv.COLOR_BGR2GRAY (BGR to gray).
Next, we need to grab the classifier we downloaded, so I’ll type
face_cascade = cv.CascadeClassifier() and I’ll give it the path to the XML file. Finally, we can actually run the whole algorithm. For this I’ll type
detected_faces = face_cascade.detectMultiScale() and I’ll pass in our
I found that a scale factor of
1.3 works best for this picture. Now,
detected_faces is an
ndarray showing the coordinates of each face, so we just have to draw boxes around those coordinates and show them on the screen. To do this, I’ll type
for (column, row, width, height) in detected_faces:
cv.rectangle()—and we want to draw on the original image starting at the
row, and ending at
(column + width) and
(row + height).
I want this to be green, which is going to be
(0, 255, 0) in RGB, as we learned earlier. I will give this a thickness of
2, which should be fine. Now, just for presentation purposes, I’ll use
cv.resize() to shrink the image since it’s fairly high-resolution and it may appear cut-off on my screen here.
cv.waitKey(), passing in
0, is going to set
0 as the kill key, which is especially useful on Windows since OpenCV tends to be kind of finicky over there. Finally, I’ll say, “When
0 is pressed, I want to destroy all windows.” And that’s it! I’ll right-click here and select to run in terminal and…wait for it… wait for it. Through the power of editing, there it is.
Become a Member to join the conversation.