How to use FaceDetector.Face for face recognition on Android

This is my first post, so I'm sorry if my question is not clear or there is not enough information.

I am currently working on an Android application that can recognize faces from images.

My first approach was to use JavaCV, and everything works just fine, except for the fact that face recognition takes too long to complete!

After that, I tried to detect faces using FaceDetector.Face. Then I used the detected faces to train my face recognition model. No errors have been discovered so far.

My problem is that my model could not recognize the detected face given by FaceDetector.Face. I always get -1 from the prediction function. Can anyone say what could be wrong? Thank you in advance!

Here's how I crop faces after detection:

for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++) { Face face=detectedFaces[count]; PointF midPoint=new PointF(); face.getMidPoint(midPoint); eyeDistance=face.eyesDistance(); left = midPoint.x - (float)(1.4 * eyeDistance); top = midPoint.y - (float)(1.8 * eyeDistance); bmFace = Bitmap.createBitmap(origiImage, (int) left, (int) top, (int) (2.8 * eyeDistance), (int) (3.6 * eyeDistance)); bmFaces.add(bmFace); } 

Here is the main part of model training.

  MatVector images = new MatVector(imageFiles.length); int[] labels = new int[imageFiles.length]; IplImage img; IplImage grayImage; FaceRecognizer faceRecognizer = createLBPHFaceRecognizer(1, 8, 8, 8, binaryTreshold); try { FileInputStream fstream = new FileInputStream(working_Dir.getAbsolutePath()+"/csv.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String imgInfo; for (int i = 0; (imgInfo = br.readLine()) != null; i++) { String info[] = imgInfo.split(";"); String imagePath = info[0]; img = cvLoadImage(imagePath); grayImage = IplImage.create(img.width(),img.height(), IPL_DEPTH_8U, 1); cvCvtColor(img, grayImage, CV_BGR2GRAY); images.put(i, grayImage); labels[i] = Integer.parseInt(info[1]);; } in.close(); //train the FaceRecognizer model faceRecognizer.train(images, labels); }catch (Exception e) { System.err.println("Error: " + e.getMessage()); } 

Finally, I recognize the face with the following code:

  public static String identifyFace(IplImage grayImg) { String predictedName = ""; //identify face from the image int predictedLabel = faceRecognizer.predict(grayImg); if(predictedLabel != -1 ) { predictedName = new String(idToName.get(predictedLabel)); } return predictedName; } 
+7
source share
2 answers

This can only happen if you do not set the appropriate threshold, see the documentation:

Method for creating LBPHFaceRecognizer :

 Ptr<FaceRecognizer> createLBPHFaceRecognizer(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold=DBL_MAX) 

Where:

  • threshold - the threshold used in the prediction. If the distance to the nearest neighbor is greater than the threshold, this method returns -1.

So, in the above method signature, you see that the threshold is set to DBL_MAX by default. Therefore, if you just leave the threshold, then it will never give -1 . On the other hand, if you set the threshold too low, FaceRecognizer will always give -1 . However, check that you have installed binaryTreshold in your code. Finding the appropriate decision threshold for your data is a classic optimization problem where you need to optimize for a better threshold according to your criteria (for example, based on the False Acceptance Rate / False Rejection Rate).

+2
source

I know this is really very late, but try using the cascading Haar classifier from JavaCV instead of colliding with the Android device itself

+1
source

All Articles