This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.

# 11.5. Detecting faces in an image with OpenCV¶

You need OpenCV and the Python wrapper. You can find installation instructions on OpenCV's website.

On Windows, you can install Chris Gohlke's package.

You also need to download the Family dataset on the book's website. (http://ipython-books.github.io).

1. First, we import the packages.
In [ ]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib as mpl
%matplotlib inline

1. We open the JPG image with OpenCV.
In [ ]:
img = cv2.imread('data/pic3.jpg')

1. Then, we convert it to a grayscale image using OpenCV's cvtColor function.
In [ ]:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

1. To detect faces, we will use the Viola–Jones object detection framework. A cascade of Haar-like classifiers has been trained to detect faces. The result of the training is stored in a XML file (part of the Family dataset available on the book's website). We load this cascade from this XML file with OpenCV's CascadeClassifier class.
In [ ]:
face_cascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')

1. Finally, the detectMultiScale method of the classifier detects the objects on a grayscale image, and returns a list of rectangles around these objects.
In [ ]:
for x, y, w, h in face_cascade.detectMultiScale(gray, 1.3):
cv2.rectangle(gray, (x,y), (x+w,y+h), (255,0,0), 2)
plt.figure(figsize=(6,4));
plt.imshow(gray, cmap=plt.cm.gray);
plt.axis('off');


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.

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).