Home > Computer Vision, English > Face detection using Python and OpenCV

Face detection using Python and OpenCV

Most of the posts you will find in this blog are Erlang related (of course they are!), but sometimes I like writing also about my experiences at University of Trento as I am doing right now. During the last couple of years I have attended many courses about Computer Vision and Digital Signal Processing so today I would like to show you something about it.

In this post I will write about making some code for face detection purposes using python and OpenCV. This post will have no code, actually you can just grab my original code from here (the files needed are faces.py and haarcascade_frontalface_alt.xml).

Face detection is a computer technology that determines the locations and sizes of human faces in images or video. It detects facial features and ignores anything else, such as buildings, trees and bodies.

Face detection is used for different purposes as recognition or  video surveillance, but at University I mainly focused on human computer interface and image database management (for example, one of the project I did needed an accurate computation of the pupils center). 

In order to identify the face ROI (Region Of Interest), I exploit the work proposed in 2001 by Paul Viola and Michael Jones, which brings together new algorithms and insights to construct a framework for robust and extremely rapid object detection. This framework was mainly developed to solve the problem of faces detection, but can be trained to detect a large variety of object classes. In Viola-Jones’ algorithm three main contributions are introduced.

The first contribution is a new image representation called “integral image” which allows very fast features evaluation. Briefly, we may say that a set of features which are reminiscent of Haar Basis functions is used, such features can be computed very rapidly at many scales by introducing the integral image representation for images. Once computed, anyone of these Haar-like features can be computed at any scale or location in constant time.

The second contribution consists of a method oriented to the construction of a classifier by selecting a small number of important features using AdaBoost algorithm. Within any image subwindow the total number of Haar-like features is quite big, in fact it is much more larger than the number of pixels. To ensure fast classification, the learning process must exclude a large majority of the available features, and focus on a small set of critical features. In practice, the selection of feature is obtained using a simple modification of the AdaBoost: the weak learner is constrained so that each weak classifier returned can depend on only a single feature. This means that each stage of the boosting process, which selects a new weak classifier, can be viewed as a feature selection process.

The third major contribution is a method which combines successively more complex classifiers in a cascade structure. In such a way there is a boost in the speed of the detector: the focus is concentrated on promising regions of the image since in such a way it is often possible to rapidly determine whether an object might occur or not in an image. More complex and accurate processing is reserved only for promising regions.

OpenCV provides an implementation of the Viola-Jones’ algorithm. OpenCV (Open Source Computer Vision Library) is a cross-platfrom library developed by Intel, WillowGarage and Itseez for real-time computer vision.  The functions proposed by OpenCv can be used with C++ and Python but there are other some ports available (e.g. EMGU cv for C#).

The functions I used for a trivial face detection are:

  • CascadeClassifier: Loads a classifier from a file and returns an object of time CascadeClassifier. For my code I will load the file proposed for face detection by OpenCV. The aforesaid file is haarcascade_frontalface_alt.xml.
  • detectMultiScale: this function must by applied to a CascadeClassifier object. It detects objects of different sizes in the input image. The detected objects are returned as a list of OpenCV rectangles. 

As I said before you will not find any code in this post, since the code I provide in the git is quite self explanatory, but if you have any trouble don’t  hesitate and comment this post. Here follows  how to run the code and the output I got by applying the code to the most famous picture in Computer Vision: Lena.

python faces.py /path/picture.jpg

A couple of notes about the code:

  • I assume that at leat a face ROI is found in the image. This means that if you try to run this code with an image with no faces you will end up (I guess) with an error due to a non handled case
  • The detection can bring as result more faces which are stored in the vector “rects”. You may notice that I assume the first element of this array to be the face we were looking for. This in fact could lead to error, since the classifier may detect as faces some pattern in the background and store it in the first element of the array. You can adjust this by changing the parameters in line 6 of faces.py (especially changing the minimum size allowed).
  • The classifier is not bullet proof, if you run this code with a very difficult db (e.g. BioId database) you will find many pictures where the face is not found by the classifier, mostly because the face is to close to the camera or in a strange position).
  1. Joseph Boule
    February 20, 2013 at 1:07 am


    When I use this code to analyze an image, I get the following error:

    Traceback (most recent call last):
    File “face.py”, line 33, in
    x1 = rects[0][1]
    IndexError: list index out of range

    How can I fix this problem? Thank you.

    • pdincau
      February 20, 2013 at 7:39 am

      are you sure there is a face in your image? try to print len (rects) and check if it is 0 (no faces) or if you found at least 1 face. If you didn’t find any face then your image may fall in one of the cases I described in the notes (face in strange position, face small, etc)…

      what image did you use? did you try with lena?

  2. samir
    February 26, 2013 at 3:05 pm

    i tried many times and i always got this error

    File “face.py”, line 6
    rects = cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(10,10), flags = cv.CV_HAAR_SCALE_IMAGE)
    IndentationError: expected an indented block

    • pdincau
      February 26, 2013 at 3:27 pm

      as always I don’t want to be unpolite here….but did you try to google for the error? Do you know anything about python?

  3. Dmitrii
    May 14, 2013 at 11:40 am

    When I run this code with “lena” and similar-scale pictures everything is okay, but my aim is to get at least some result with the fotos of dense crowd (like this one http://mn.ru/images//33897/42/338974296.jpg). I changed minSize values but it doesn’t give a result (len(rects)=0).
    Is there any ways to change faces.py for my purpose? Or may be exists other version of haarcascade_frontalface_alt ?
    Thank you!

  4. Dmitrii
    May 14, 2013 at 8:35 pm

    Thank you for the answer!
    I tried, but people detector doesn’t work: in dense crowd only faces(heads) can be recognised.
    Do you know where I can find other versions of your classifier from the file haarcascade_frontalface_alt.xml?
    Or how can I change the current one to recognise smaller objects?

    • pdincau
      May 14, 2013 at 8:43 pm

      mhhh not really, have you tried setting clustering equal to zero?

  5. Dmitrii
    May 14, 2013 at 9:03 pm

    Yes, all exсepting scaleFactor. Setting it equal to zero (or just less than 1.1) returns error:

    OpenCV Error: Assertion failed (scaleFactor > 1 && image.depth() == CV_8U) in un
    known function, file ..\..\..\src\opencv\modules\objdetect\src\cascadedetect.cpp
    , line 1099
    Traceback (most recent call last):
    File “D:/faces.py”, line 30, in
    rects = detect(gray, cascade)
    File “D:/faces.py”, line 6, in detect
    rects = cascade.detectMultiScale(img, scaleFactor=0.9, minNeighbors=0, minSi
    ze=(0, 0), flags = cv.CV_HAAR_SCALE_IMAGE)
    cv2.error: ..\..\..\src\opencv\modules\objdetect\src\cascadedetect.cpp:1099: err
    or: (-215) scaleFactor > 1 && image.depth() == CV_8U

    Setting minNeighbors and minSize to zero does not relly help.

    • pdincau
      May 15, 2013 at 6:37 am

      Then I suggest you to create your own classifier

  6. Dmitrii
    May 29, 2013 at 4:21 pm


    What dataset did you use to train haarcascade_frontalface_alt.xml?

  1. November 17, 2012 at 9:23 pm
  2. November 24, 2014 at 3:37 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: