{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 11.5. Detecting faces in an image with OpenCV" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You need OpenCV and the Python wrapper. You can find installation instructions on [OpenCV's website](http://docs.opencv.org/trunk/doc/py_tutorials/py_tutorials.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On Windows, you can install [Chris Gohlke's package](http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You also need to download the *Family* dataset on the book's website. (http://ipython-books.github.io)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. First, we import the packages." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "import cv2\n", "import matplotlib.pyplot as plt\n", "import matplotlib as mpl\n", "%matplotlib inline\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. We open the JPG image with OpenCV." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "img = cv2.imread('data/pic3.jpg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Then, we convert it to a grayscale image using OpenCV's `cvtColor` function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. 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." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "face_cascade = cv2.CascadeClassifier('data/haarcascade_frontalface_default.xml')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Finally, the `detectMultiScale` method of the classifier detects the objects on a grayscale image, and returns a list of rectangles around these objects." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for x, y, w, h in face_cascade.detectMultiScale(gray, 1.3):\n", " cv2.rectangle(gray, (x,y), (x+w,y+h), (255,0,0), 2)\n", "plt.figure(figsize=(6,4));\n", "plt.imshow(gray, cmap=plt.cm.gray);\n", "plt.axis('off');" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }