Tuesday 11 February 2014

How to Detect Features, Objects [Face, Eye ,Ear ,Vehicle etc] in an imge using Java with the help of OpenCV & javaCV

 We can detect features like eyes, nose, face from an image using java with the help of opencv [it is an api provided by google to enable computer vision ] and javacv [java wrapper for opencv]..Here is a simple example to demonstrate this..

                                               STEPS FOR A SAMPLE APPLICATION

1 ] You need to download opencv [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.8/opencv-2.4.8.exe/download] & extract it to "C" drive

2 ] You have to add  some necessary jar files ,which are available  from :          https://code.google.com/p/javacv/downloads/list

3 ] Use the following code   - FaceDetection.java

import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;

import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

public class FaceDetection {

    // The cascade definition to be used for detection.
    private static final String CASCADE_FILE = "./haarcascade_frontalface_alt.xml";

    public static void main(String arg[]) throws Exception {
       
     
        // Load the original image.
        IplImage originalImage = cvLoadImage("c:\\Users\\Desktop\\Test\\input.jpg",1);

        // We need a grayscale image in order to do the recognition, so we
        // create a new image of the same size as the original one.
        IplImage grayImage = IplImage.create(originalImage.width(),
                originalImage.height(), IPL_DEPTH_8U, 1);

        // We convert the original image to grayscale.
         cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);

        CvMemStorage storage = CvMemStorage.create();

        // We instantiate a classifier cascade to be used for detection, using
        // the cascade definition.
        CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
                cvLoad(CASCADE_FILE));

        // We detect the faces.
        CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1,0);

        // We iterate over the discovered faces and draw yellow rectangles
        // around them.
        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.YELLOW, 1, CV_AA, 0);

        }
        // Save the image to a new file.
        cvSaveImage("c:\\Users\\Desktop\\Output\\output.jpg", originalImage);

    }

}

4 ]  Code contains a  "haarcascade_frontalface_alt.xml" file , which is available from
 C:\opencv\data\haarcascades  directory .Just copy that file into your project's folder

5 ] Haarcascade file is generated by training, with lot of similiar images .Opencv provide so many   Haarcascade files to detect left ear,right eye ,nose etc. This code will work for any case [just replace the xml file & its name in code]

6 ]  We can generate our own Haarcascade file [So , we can identify things as we wish , like number plate, vehicle, biological cell ,road lane etc.]. That will be publish soon.

2 comments: