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));
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.