SHARE
TWEET

Untitled

a guest Jan 12th, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.amlan.javacvfacerecognization.utils;
  2.  
  3. import android.util.Log;
  4.  
  5. import org.bytedeco.javacpp.opencv_core.Mat;
  6. import org.bytedeco.javacpp.opencv_core.MatVector;
  7. import org.bytedeco.javacpp.opencv_face.FaceRecognizer;
  8.  
  9. import java.io.File;
  10. import java.io.FilenameFilter;
  11. import java.nio.IntBuffer;
  12.  
  13. import static org.bytedeco.javacpp.opencv_core.CV_32SC1;
  14. import static org.bytedeco.javacpp.opencv_face.createEigenFaceRecognizer;
  15. import static org.bytedeco.javacpp.opencv_face.createFisherFaceRecognizer;
  16. import static org.bytedeco.javacpp.opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE;
  17. import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
  18.  
  19. public class OpenCVFaceRecognizer {
  20.  
  21.     private FaceRecognizer faceRecognizer;
  22.    
  23.     public void trainModel(String trainingDir) {
  24.  
  25.         File root = new File(trainingDir);
  26.  
  27.         FilenameFilter imgFilter = new FilenameFilter() {
  28.             public boolean accept(File dir, String name) {
  29.                 name = name.toLowerCase();
  30.                 return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
  31.             }
  32.         };
  33.  
  34.         File[] imageFiles = root.listFiles(imgFilter);
  35.  
  36.         MatVector images = new MatVector(imageFiles.length);
  37.  
  38.         Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
  39.         IntBuffer labelsBuf = labels.createBuffer();
  40.  
  41.         int counter = 0;
  42.  
  43.         for (File image : imageFiles) {
  44.             Mat img = imread(image.getAbsolutePath(), CV_LOAD_IMAGE_GRAYSCALE);
  45.  
  46.             String parentFolderName = image.getParent();
  47.             String folderName = parentFolderName.substring(parentFolderName.lastIndexOf("s") + 1);
  48.  
  49.             // lable is a int which denotes the type. Basically associate a set of image to a lable
  50.             int label = Integer.parseInt(folderName);
  51.  
  52.             images.put(counter, img);
  53.  
  54.             labelsBuf.put(counter, label);
  55.  
  56.             counter++;
  57.         }
  58.  
  59.  
  60.         // We are using Eigenfaces Algorithm
  61.         faceRecognizer = createEigenFaceRecognizer();
  62.         faceRecognizer.train(images, labels);
  63.  
  64.  
  65.     }
  66.  
  67.     public int preditImage(String fileName) {
  68.  
  69.         Mat testImage = imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
  70.         int predictedLabel = faceRecognizer.predict(testImage);
  71.         System.out.println("Predicted label: " + predictedLabel);
  72.         Log.e("amlan","Prediction :::: "+predictedLabel);
  73.         return predictedLabel;
  74.  
  75.     }
  76. }
RAW Paste Data
Top