Face Detection in JavaCV using haar classifier

OpenCV provides haar like feature detection algorithm which can be used for object detection. Wikipedia page http://en.wikipedia.org/wiki/Haar-like_features provides nice information about what are haar like feature.

OpenCV also provides haar training utility which can be used for training. It generates XML file from training samples which further can be used for fast object detection. Such XML file is provided with opencv package for face detection.

Gist below explains how to use haar classifier in JavaCV. Code loads classifier file haarcascade_frontalface_default.xml and uses cvHaarDetectObjects() to find faces in loaded image. More information about cvHaarDetectObjects() can be found at http://opencv.willowgarage.com/documentation/object_detection.html#haardetectobjects

Result of this code generates window which shows loaded picture and red rectangle around detected faces.


You can fork complete eclipse project at https://github.com/nikhil9/FaceDetection/

32 comments:

  1. Getting error:....

    Exception in thread "main" java.lang.RuntimeException: ..\..\..\src\opencv\modules\core\src\array.cpp:2382: error: (-27) NULL array pointer is passed

    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:243)
    at facedetect.Facedetect.detect(Facedetect.java:47)
    at facedetect.Facedetect.main(Facedetect.java:31)

    ReplyDelete
    Replies
    1. It seems path to your image is not correct hence it is throwing "NULL array pointer is passed". Make sure you are providing correct image path in 'cvLoadImage' function in above code.

      Delete
  2. Thanks for the code! It is working great. Can this be used with real-time webcam images ? I combined above code with your tutorial on webcam using javaCV .But the result was very slow performance and high CPU usage. How to increase performance ? Here is link to my code: https://www.box.com/s/pbi9ndi5b415231me17k

    ReplyDelete
    Replies
    1. Speed of code depends on various things like how much is frame rate from camera, cpu speed, memory, resolution of image etc. You should try release memory after certain time to process it at faster rates. In your code, I see you have given 40ms delay in line 69. May be this delay is making it appear slow. What is the camera frame rate and resolution you are using?

      Delete
    2. 15 fps VGA webcam, i3 2nd gen, 3GB ram. Removing delay has no noticeable effect. How to release memory ?

      Delete
  3. I used this code and got no error But it was unable to detect any face in any image..
    I didn't get any error..
    But got no result..

    ReplyDelete
  4. I used this code, it works fine on my friend pc but the same code on my pc didn't detect any face..
    I got no error no any warning but don't know what is the problem with me..

    ReplyDelete
  5. I have done with this code..
    Can u help me in doing the same face detection on android..??

    ReplyDelete
    Replies
    1. how you made the code work

      Delete
  6. après arriver a ce résultat et que mon algorithme détecte le visage et dessine ce carrer je veux maintenant calculer les dimensions de ce carrer dessiner comme le centre la langueur du cote et la langueur du diagonal avez vous des propositions s'il vous plait
    merci

    ReplyDelete
  7. Your code is working fine without any errors, but it doesn't recognize any faces as the others said reply on this asap

    ReplyDelete
  8. It's slowest use:

    CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage, 1.1, 3, classifier);

    instead of:

    CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage, 1.1, 3, classifier, cvSize(80, 80), cvSize(0, 0));

    Test it!

    ReplyDelete
  9. Me a salvado la vida justo lo que buscaba gracias por el aporte

    ReplyDelete
  10. hi this work thanks a lot. i want to learn depth any link or reference from the basic thanks in advance

    ReplyDelete
  11. hi friend i am using eclipse and when tried to export as runnable jar it is not binding the xml and img file to jar. xml and img file requires to copy on the same directory of jar file. i tried some codes on online to combine the file with jar but it dont work have any idea about combine files with jar?..

    ReplyDelete
  12. Getting error

    OpenCV Error: Null pointer (Invalid classifier cascade) in unknown function, file ..\..\..\src\opencv\modules\objdetect\src\haar.cpp, line 1514
    Exception in thread "main" java.lang.RuntimeException: ..\..\..\src\opencv\modules\objdetect\src\haar.cpp:1514: error: (-27) Invalid classifier cascade

    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(Native Method)
    at com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects(opencv_objdetect.java:238)
    at FaceDetection.FaceDetection.detect(FaceDetection.java:32)
    at FaceDetection.FaceDetection.main(FaceDetection.java:24)

    ReplyDelete
    Replies
    1. Which Javacv and opencv version are you using?

      Delete
  13. Hi
    I have follow all the steps and try to run complete eclipse project. But i am having the following error:

    Exception in thread "main" java.lang.ExceptionInInitializerError
    at face.detect.FaceDetection.main(FaceDetection.java:26)
    Caused by: java.lang.IllegalStateException: Can't overwrite cause
    at java.lang.Throwable.initCause(Throwable.java:320)
    at com.googlecode.javacpp.Loader.load(Loader.java:581)
    at com.googlecode.javacpp.Loader.load(Loader.java:532)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:79)
    ... 1 more

    Any idea on what could be the cause of this error ?

    Thanks

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. hi thanks for the great tutorial... also i want to know how to get the facial features using java

    ReplyDelete
    Replies
    1. You will have to train your own dataset using cascade training tool which comes with opencv package. This will create a .xml file which you can use with code posted in above post to detect your own features.

      Delete
  16. Will u please explain the contents of this "haarcascade_frontalface_default.xml" file.

    Thanks!

    ReplyDelete
    Replies
    1. This xml file contains training data required for feature detection which is generated after processing thousand of images using machine learning. Opencv package provides cascade training tool which you can use with your own dataset to generate such xml file for desired features.

      Delete
  17. Will u please suggest some books for learning opencv and javacv...

    ReplyDelete
    Replies
    1. Books related to opencv are mentioned in the following link
      http://opencv.org/books.html

      Delete
  18. Please answer this:- After detecting faces how to crop the faces and store it in the database?

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
    IplImage* img2;
    img2=cvCloneImage(img1);
    Is there any alternative for this code in JavaCV?

    ReplyDelete
  21. hi help me plz I have follow all the steps and try to run complete eclipse project. But i am having the following error:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_highgui in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1886)
    at java.lang.Runtime.loadLibrary0(Runtime.java:849)
    at java.lang.System.loadLibrary(System.java:1088)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:711)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:711)
    at com.googlecode.javacpp.Loader.load(Loader.java:586)
    at com.googlecode.javacpp.Loader.load(Loader.java:540)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:79)
    at facedetection.FaceDetection.main(FaceDetection.java:21)
    Caused by: java.lang.UnsatisfiedLinkError: C:\Users\Tarhouni\AppData\Local\Temp\javacpp18563232366895\jniopencv_highgui.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1965)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1890)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1851)
    at java.lang.Runtime.load0(Runtime.java:795)
    at java.lang.System.load(System.java:1062)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:700)
    ... 4 more

    ReplyDelete
    Replies
    1. Got the same problem

      Delete
    2. Please refer to my post about installation of JavaCV.
      http://opencvlover.blogspot.in/2012/04/javacv-setup-with-eclipse-on-windows-7.html

      Delete