Failure in training LBPHFaceRecognizer on Android Studio

I try to train my LBPHFaceRecognizer on my app written by Android studio. However, the trained LBPHFaceRecognizer can only recognize the person of the first image input.
I checked all photos are read successfully. The same set of photos could be trained successfully on PC python.

File photoDir = getDir("photo", Context.MODE_APPEND);
Integer total = 0;
Integer max = 100;
for (int t=1; t < Num+1; t++){
    if (ph[t] < max){
        max = ph[t];
    }
}
total = Num*max;
List<Mat> phData = new ArrayList<Mat>();
int labelBuff[] = new int[total];

Integer count = 0;
Integer label = 0;

Mat imgRgba = new Mat();
Mat imgGray = new Mat();

for (int j = 1; j < (max+1); j++){
    for (int i = 1; i < (careNum+1); i++){
        imgRgba = Imgcodecs.imread(photoDir.getAbsolutePath()+"/"+i+"_"+j+".jpg");
        label = i;
        Imgproc.cvtColor(imgRgba, imgGray, Imgproc.COLOR_RGB2GRAY);
        phData.add(count, imgGray);
        labelBuff[count] = label;//.put(count,label);
        count++;
    }
}
for (int k = 0; k < count; k++){
    labelList.put(k, 0, labelBuff[k]);
}

faceRecog = LBPHFaceRecognizer.create();
faceRecog.train(phData, new MatOfInt(labelBuff));


File ymlDir = getDir("yml", Context.MODE_APPEND);
File ymlfile = new File(ymlDir.getAbsolutePath() + "/" + name + ".yml");
faceRecog.save(String.valueOf(ymlfile));

could you check, if the yml file contains all similar lines as well ?

and you probably should not train this on your phone …

I have opened the yml file, but I could not understand the lines. How should I work on that?

i have a theory, that it is rather the last image , and that your java code is to blame here:

those are global var’s -
move them into the most inner loop,
so it really allocates new memory for the gs image (& does not reuse/overwrite the 1st only) !

or, add() a imgGray.clone() to phData …

p.s.:
what is it all good for ?
why on a phone ?
the LBPHFaceRecognizer does classification (1 of a fixed set). is that,what you need ?

a better approach is to take a model that’s been trained on lots and lots of faces (more than you would gather), and spits out a feature vector. then the problem is reduced to finding close matches to a feature vector.

1 Like

Is it a good idea if I use update function with an existing trained dataset instead?

again, which problem is your program trying to solve ?

Just a simple face recognition stuff with customize photos upload, supposed to be simple

Both of the method cannot help. It still says photo of everyone as label 1

nothing is simple, if you have no clue, what you’re doing.

please answer questions.