1
votes

Can anyone help me with my problem? I can't seem to get any answer from the internet. I've been searching for like the whole day. So here's my problem. I am creating a face recognition android application using opencv4android 2.4.10 and Android Studio as my IDE.

I need to use FaceRecognition.predict(Mat src, int[] labels, double[] confidence) to get which person is being detected. But most tutorials I've research only have FaceRecognition.predict(Mat src) I'm not sure where to get int[] labels and double[] confidence. I would appreciate if anyone could teach me how to do this.

Sample code:

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

Mat labels = new Mat(imageFiles.length, 1, CvType.CV_32SC1);
List<Mat> images = (List<Mat>) new Mat(imageFiles.length);

int counter = 0;
int label;

//get all person id and images
for (File image : imageFiles) {
    Mat img = Highgui.imread(image.getAbsolutePath(), 0);
    images.add(counter, img);

    label = Integer.parseInt(image.getName().split("-")[0]);
    labels.put(counter, 0, label);

    counter++;
}
//I created a java wrapper for this method
//this works just fine.
FaceRecognizer facerec = new FisherFacesRecognizer();

facerec.train(images, labels);

//my problem is here, the parameters are supposed to be
//*Mat src, int[] labels, double[] confidence*
//but most tutorials only have *Mat src*
int predictedLabel = facerec.predict(grayImg);
1

1 Answers

3
votes

both functions essentially do the same thing - return the recognized id.

the other overload additionally returns the distance value to the best item from the database (smaller==better). since one cannot return more than 1 value from a function in java (or c++) this is done passing by reference. so either use:

   int predictedLabel = facerec.predict(grayImg);

or, (if you want to kow the distance) :

   int[] prediction = {0};
   double[] distance = {0};

   facerec.predict(grayImg, prediction, distance);

   int predictedLabel = prediction[0];
   double dist = distance[0];