Thursday, November 3, 2011

Face and eyes detection in OpenCV

The goal of object detection is to find an object of a pre-defined class in an image. In this post we will see how to use the Haar Classifier implemented in OpenCV in order to detect faces and eyes in a single image. We are going to use two trained classifiers stored in two XML files:
  • haarcascade_frontalface_default.xml - that you can find in the directory /data/haarcascades/ of your OpenCV installation
  • haarcascade_eye.xml - that you can download from this website.
The first one is able to detect faces and the second one eyes. To use a trained classifier stored in a XML file we need to load it into memory using the function cv.Load() and call the function cv.HaarDetectObjects() to detect the objects. Let's see the snippet:
imcolor = cv.LoadImage('detectionimg.jpg') # input image
# loading the classifiers
haarFace = cv.Load('haarcascade_frontalface_default.xml')
haarEyes = cv.Load('haarcascade_eye.xml')
# running the classifiers
storage = cv.CreateMemStorage()
detectedFace = cv.HaarDetectObjects(imcolor, haarFace, storage)
detectedEyes = cv.HaarDetectObjects(imcolor, haarEyes, storage)

# draw a green rectangle where the face is detected
if detectedFace:
 for face in detectedFace:
  cv.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv.RGB(155, 255, 25),2)

# draw a purple rectangle where the eye is detected
if detectedEyes:
 for face in detectedEyes:
  cv.Rectangle(imcolor,(face[0][0],face[0][1]),
               (face[0][0]+face[0][2],face[0][1]+face[0][3]),
               cv.RGB(155, 55, 200),2)

cv.NamedWindow('Face Detection', cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage('Face Detection', imcolor) 
cv.WaitKey()
These images are produced running the script with two different inputs. The first one is obtained from an image that contains two faces and four eyes:
And the second one is obtained from an image that contains one face and two eyes (the shakira.jpg we used in the post about PCA):

10 comments:

  1. When I try this, the command

    haarEyes = cv.Load('haarcascade_eye.xml')

    results in the error message

    error: haarcascade_eye.xml(1): Unsupported encoding

    I'm running Ubuntu 11.04, using Python 2.7, and OpenCV 2.2 (the current one supported by Ubuntu).

    Any ideas as to why this .xml file won't load? It did load the frontal face xml file with no problems...

    Thanks,
    -Rafael Espericueta

    ReplyDelete
  2. Oops, please ignore that last question. The file was simply NOT in the right directory...

    -Rafael Espericueta

    ReplyDelete
  3. sorry for distrubing you, but i just want to share the article about face detection,
    let's check this link http://repository.gunadarma.ac.id/bitstream/123456789/3365/1/Automed%20Face%20Detection%20and%20Feature%20Extraction%20Using%20Color%20Feret%20Image%20Database.pdf
    i wish that article can be usefull

    ReplyDelete
  4. what if i want the exact position(co- ordinates) of both eyes and call another funtion with both eye co-ordinate.. plz suggest changes in your code

    ReplyDelete
    Replies
    1. Hello Atul, if you could get a a rough approximation of where the eye is taking the center of the rectangle returned by HaarDetectObjects.

      Delete
    2. Hi JustGlowing, could you post an example of extracting a rectangle's center?

      Delete
  5. Undefined variable from import: LoadImage

    this is the error i get also the same error of all functions in the other part of the code. can u please tell me why is this happening?

    ReplyDelete
    Replies
    1. This could be due to a different version of the OpenCV library.

      Delete
  6. Hi! I know you wrote this several years ago, but thank you, this was exactly what I was looking for. I played with it a bit, because in some images in found faces without eyes, and extra eyes in faces, and I wrote about it here, referencing this blog:
    http://operationlearnsomething.wordpress.com/2014/04/14/face-and-eye-detection-in-python-opencv/
    Thanks again!

    ReplyDelete
    Replies
    1. Thanks Sara, I'm going to post it on twitter. ;)

      Delete