I want to use OpenCV 3 in Java with FaceRecognition.

asked 2015-09-10 23:27:59 -0600

Pritam gravatar image

updated 2015-09-19 12:42:35 -0600

Hi I am very new to OpenCV,using opencv 3, I am facing problem while training and prediction the image could you please help me to solve the problem. There is no any tutorial related to FaceRecognition in OpenCV through Java. I Started using SVM bug I am not very conversant with the SVM what ever I did coding It gives me 0 as a predict lable always I think I am missing something . Please help me .... :(

public class FaceRecDemo{

public static void main(String[] args) {

    System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
    String trainingDir = args[0];
    Mat testImage = Imgcodecs.imread("F:\\personal\\OpenCVTraining\\lena1.png",Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

    File root = new File(trainingDir);

    FilenameFilter imgFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            name = name.toLowerCase();
            return name.endsWith(".jpg") || name.endsWith(".pgm")
                    || name.endsWith(".png");
        }
    };

    File[] imageFiles = root.listFiles(imgFilter);


    int num_files = imageFiles.length;
    int img_area = 480*640;
    Mat trainingMat = new Mat(num_files,img_area,CvType.CV_32FC1);
    Mat labels = new Mat(num_files, 1, CvType.CV_32SC1);

    int ii = 0; // Current column in training_mat
    int file_num=0;
    for (File image : imageFiles) {
        Mat img_mat = Imgcodecs.imread(image.getAbsolutePath(), Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);         
        for (int i = 0; i<img_mat.rows(); i++) {
            for (int j = 0; j < img_mat.cols(); j++) {
                trainingMat.put(file_num, ii++, img_mat.get(i, j));
            }
        }
        labels.put(file_num,0,file_num+10);
        file_num++;
    }
    Mat predictData = new Mat(1, testImage.cols()*testImage.rows(), CvType.CV_32F);
    int predictDataCol = 0;
    for(int i=0;i<testImage.rows();i++)
    {
        for (int j=0;j<testImage.cols();j++) {
            predictData.put(1,predictDataCol++,testImage.get(i, j));
        }
    }
    SVM svm = SVM.create();
    svm.setKernel(2);
     svm.train(trainingMat,Ml.ROW_SAMPLE,labels);
     Mat results = new Mat();
     int label = (int) svm.predict(predictData, results, 0);
     System.out.println(label);
}

}

edit retag flag offensive close merge delete

Comments

  • shouldn't that be : predictData.put(0,predictDataCol++,testImage.get(i, j)); ?

    note, that it's probaby better to use a plain

    Mat predictData= new Mat(); testImage.reshape(1,1).convertTo(predictData,CV_32F);

    to flatten it to a single (float)row. those for loops are terribly slow, and error-prone.

  • your images are far too large, and contain too much border. use face-detection, crop the face region, and resize that to 100x100. (for both training and testing)
  • fine tuning an SVM ;) try a LINEAR or POLY kernel, also try to vary C in 1,10,100,1000 steps
berak gravatar imageberak ( 2015-09-10 23:59:29 -0600 )edit
1

while trying with an SVM is definitely a good idea, note , that you can actually try to use opencv's builtin face recognition in opencv3, you will have to get the contrib repo , and enable the java wrapper by adding 'java' at the end here . demo

berak gravatar imageberak ( 2015-09-11 00:10:22 -0600 )edit

Is it possible to predict multiple match for a single face image.

Pritam gravatar imagePritam ( 2015-09-15 03:22:09 -0600 )edit