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/

70 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
  6. This comment has been removed by the author.

    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
    2. which version should be used ???? plz tell me same error i am also getting . Send me ans at aditya.24800@gmail.com plzzzz.

      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
  22. jniopencv_core.dll: Can't find dependent libraries

    ReplyDelete
  23. Thanks for sharing great information, it's worth reading and sharing for others.

    ReplyDelete
  24. getting following exception:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\Users\new\AppData\Local\Temp\javacpp10436703213805\jniopencv_core.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary1(Unknown Source)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.load0(Unknown Source)
    at java.lang.System.load(Unknown Source)
    at com.googlecode.javacpp.Loader.loadLibrary(Loader.java:418)
    at com.googlecode.javacpp.Loader.load(Loader.java:368)
    at com.googlecode.javacpp.Loader.load(Loader.java:315)
    at com.googlecode.javacv.cpp.opencv_core.(opencv_core.java:131)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:334)
    at com.googlecode.javacv.cpp.opencv_imgproc.(opencv_imgproc.java:96)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.googlecode.javacpp.Loader.load(Loader.java:334)
    at com.googlecode.javacv.cpp.opencv_highgui.(opencv_highgui.java:85)
    at FaceDetection.main(FaceDetection.java:15)

    ReplyDelete
  25. how is can create my own classifier. for example i want to make classifer for house

    ReplyDelete
  26. C:\Users\Hp\AppData\Local\Temp\javacpp1805438856875\jniopencv_core.dll I have any idea about this Error Its seems is a link to a libraries but I dont know how fix its

    ReplyDelete
  27. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  28. nice blog
    Great Information.
    Beauty Culture
    VLCC institute has a dedicated in-house placement cell which provides job opportunities
    to the students in major wellness and beauty companies across India.

    ReplyDelete
  29. mukesh Deepak Hello friends, It is such a beautiful day, and we have arranged an impressive collection

    ReplyDelete
  30. Hiv disease for the last 3 years and had pain hard to eat and cough are nightmares,especially the first year At this stage, the immune system is severely weakened, and the risk of contracting opportunistic infections is much greater. However, not everyone with HIV will go on to develop AIDS. The earlier you receive treatment, the better your outcome will be.I started taking ARV to avoid early death but I had faith in God that i would be healed someday.As a Hiv patent we are advise to be taking antiretroviral treatments to reduce our chance of transmitting the virus to others , few weeks ago i came on search on the internet if i could get any information on Hiv treatment with herbal medicine, on my search i saw a testimony of someone who has been healed from Hiv her name was Achima Abelard and other Herpes Virus patent Tasha Moore also giving testimony about this same man,Called Dr Itua Herbal Center.I was moved by the testimony and i contacted him by his Email.drituaherbalcenter@gmail.com We chatted and he send me a bottle of herbal medicine I drank it as he instructed me to.After drinking it he ask me to go for a test that how i ended my suffering life of Hiv patent,I'm cured and free of Arv Pills.I'm forever grateful to him Drituaherbalcenter.Here his contact Number +2348149277967...He assure me he can cure the following disease..Hiv,Cancer,Herpes Virus,Epilepsy, fibromyalgia ,ALS,Hepatitis,Copd,Parkinson disease.Genetic disease,Fibrodysplasia Ossificans Progressiva,Factor V Leiden Mutation ,Fatal Familial Insomnia ,Dupuytren's disease,Fibrodysplasia Ossificans Progressiva,Fluoroquinolone Toxicity Syndrome,Inflammatory bowel disease ,Huntington's disease ,Diabetes,Fibroid...

    ReplyDelete
  31. I never believed that i will be happy again after much frustration. I have been rejected by my husband after Six(6) years of marriage just because he met another woman and he left me and the kids to suffer. One day when i was reading through the web i stumbled at a post on how chief dr lucky the great spell caster has helped people to restore broken marriages and relationships and curing of diseases. I emailed him immediately to help my situation via: chiefdrlucky@gmail.com He told me not to worry that my husband will return to me begging and shedding tears of forgiveness. I am so happy that my husband actually returned to me in 48 hours after i obeyed the words and instructions of chief dr lucky. contact him today in any of your problems for a lasting solution via: chiefdrlucky@gmail.com or call/whats app +2348132777335 Website: http://chiefdrluckysolutionhome.website2.me/  

    ReplyDelete
  32. I want to thank Dr Emu a very powerful spell caster who help me to bring my husband back to me, few month ago i have a serious problem with my husband, to the extend that he left the house, and he started dating another woman and he stayed with the woman, i tried all i can to bring him back, but all my effort was useless until the day my friend came to my house and i told her every thing that had happened between me and my husband, then she told me of a powerful spell caster who help her when she was in the same problem I then contact Dr Emu and told him every thing and he told me not to worry my self again that my husband will come back to me after he has cast a spell on him, i thought it was a joke, after he had finish casting the spell, he told me that he had just finish casting the spell, to my greatest surprise within 48 hours, my husband really came back begging me to forgive him, if you need his help you can contact him with via email: Emutemple@gmail.com or add him up on his whatsapp +2347012841542 is willing to help any body that need his help.

    ReplyDelete
  33. Great Article
    Image Processing Projects


    Deep Learning Projects for Final Year


    JavaScript Training in Chennai

    JavaScript

    Training in Chennai


    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training Project Centers in Chennai

    ReplyDelete
  34. am having the following errors, how can I handle it.
    SEVERE: Can't open training database file 'data/facedata.xml'.
    OpenCV Error: Null pointer (Invalid classifier cascade) in cvHaarDetectObjectsForROC, file ..\..\..\..\opencv\modules\objdetect\src\haar.cpp, line 1514
    Exception in thread "main" java.lang.NullPointerException
    at FaceRecognizer.FaceRecognizer1.recognizeFromCam1(FaceRecognizer1.java:264)
    at FaceRecognizer.FaceRecognizer1.main(FaceRecognizer1.java:902)

    ReplyDelete
  35. I am so glad to be able to share my wonderful experience with everyone on my journey of getting herbal solution from Dr Akuna, I have suffered from Herpes simplex virus 1&2 for more than 10years, I came across a page of someone speaking of Dr Akuna. I decided to contact and behold i got very swift response from Dr Akuna and in few days i got sent my herbal cure and then i was directed on how to use it, in less than 2 weeks i was told to go and run a test, I was shock when i was told by my doc i am negative of Herpes Simplex virus 1&2. Here is his email handle if you are also needing help. Email: drakunasolutiontemple@gmail.com or What App him/ Call: +2348154625070

    ReplyDelete