Thursday, January 9, 2014

Face Detection With JavaCV

For those who have waited for latest Person of Interest it's been telecast. Backup of project Samaritan has been stolen.
Lets get started. As I mentioned in earlier post after watching the latest episode, again my head bumped up with that crazy idea "The Machine" Up to now I have tried to capture a single image from web cam then I step up and capture a video. Now I think it is time to get touch with some rel image processing stuffs. The thing that I'm going to try today can be seen in most of capturing stuffs today. Face detection. It is in digital cameras, smart phones and even facebook now days having some awesome face detection algorithm where when you upload a photo it suggest the person to tag with great accuracy. Above all The Machine is heavily using face detection."It sees everything"

First let's try to detect a face in a photo.
You may think that code is just few lines and this is so easy. Yes that is true if you only want to touch stuffs that are being already built. You will never explorer new stuffs if you stick to just to them.
Computer can see via a capturing device, but it never understand what it seeing until we teach it what it see. If you like this google and read on Computer vision. It is where people are trying to teach computer how to behave like a human eye. This teaching process is not so easy. It is called Machine Learning. There are various ways that are been developed by geeks throughout the last couple of decades. Mainly these learning techniques can be categorized to 3 broad areas.

  1. Supervised Learning - This is similar to parents taught us when we were small and we by hard them. (You are having labeled data set and learn  based on that)
  2. Unsupervised Learning - In this method we don't have labeled data.
  3. Reinforcement Learning - When parents scold or beat us we know it is bad. If they appreciate we know it is good.
This is a so much interesting subject area if you like to dig it in. You can find lot of books in this area.
  • Artificial Neural Network
  • Decision Trees
  • Bayesian Networks
  • Hidden Markov Models
  • Support Vector Machines

are some of the commonly used techniques in Machine learning. Lot of people hate mathematics stuffs when it comes to computing. Most of the people think that there is no need of having knowledge about mathematics to do programming. Yes you can survive even at the industry, but if you really like advanced stuffs it is a must to have a good mathematical knowledge. All most all the machine learning techniques used mathematical formulas, integration, differentiation, etc...  It is enough about machine learning and advice. Let get back on to track.

Create a new java class and have following code in that


 class FaceDetection {  
   private static final String CASCADE_FILE = "F:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml";  
   public static void main(String[] args) {  
     IplImage originalImage = cvLoadImage("E:/Images/Faces/Image1275.jpg", 1);  // Specify and image which has at least a single face
     IplImage grayImage = IplImage.create(originalImage.width(),  
         originalImage.height(), IPL_DEPTH_8U, 1);  
     cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);  
     CvMemStorage storage = CvMemStorage.create();  
     CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));  
     CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);  
     for (int i = 0; i < faces.total(); i++) {  
       CvRect r = new CvRect(cvGetSeqElem(faces, i));  
       cvRectangle(originalImage, cvPoint(r.x(), r.y()), cvPoint(r.x() + r.width(), r.y() + r.height()), CvScalar.BLUE, 5, CV_AA, 0);  
     }  
     cvSaveImage("E:/JavaCV/FaceDetection.jpg", originalImage);  // saved image with detected face(s)
   }  
 }  

Here CASCADE_FILE is the data file that we used to learn the machine to identify the faces. This file can be find in the OpenCV zip file that you have downloaded. I'm not going to talk about what is in it in this post. But you can see lot of numeric values in that if you open that xml file.

Now run this file (You must include javacpp.jar, javacv.jar, opencv....jar) and see the output image file