简体   繁体   中英

rfaces.get(0).getSecondObject() in OpenIMAJ face recognition always returns null

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.codehaus.jackson.map.introspect.Annotated;
import org.openimaj.data.dataset.GroupedDataset;
import org.openimaj.data.dataset.ListDataset;
import org.openimaj.data.dataset.VFSGroupDataset;
import org.openimaj.experiment.dataset.split.GroupedRandomSplitter;
import org.openimaj.experiment.dataset.util.DatasetAdaptors;
import org.openimaj.feature.DoubleFV;
import org.openimaj.feature.DoubleFVComparison;
import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.FImage;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.model.EigenImages;
import org.openimaj.image.processing.face.alignment.RotateScaleAligner;
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
import org.openimaj.image.processing.face.detection.keypoints.FKEFaceDetector;
import org.openimaj.image.processing.face.detection.keypoints.KEDetectedFace;
import org.openimaj.image.processing.face.recognition.EigenFaceRecogniser;
import org.openimaj.image.processing.face.recognition.FaceRecognitionEngine;
import org.openimaj.ml.annotation.AnnotatedObject;
import org.openimaj.ml.annotation.ScoredAnnotation;
import org.openimaj.util.pair.IndependentPair;

public class Demo {

  public static void main(String ... s) throws IOException {

    FKEFaceDetector faceDetector = new FKEFaceDetector(new HaarCascadeDetector());

    EigenFaceRecogniser<KEDetectedFace, Integer> faceRecognizer = EigenFaceRecogniser.create(20, new RotateScaleAligner(), 1, DoubleFVComparison.CORRELATION, 0.9f);

    FaceRecognitionEngine<KEDetectedFace, Integer> faceEngine = FaceRecognitionEngine.create(faceDetector, faceRecognizer);


    for(int i = 1; i < 15; i ++){
      System.out.println(i);
      FImage pamu = ImageUtilities.createFImage(ImageIO.read(new File("/home/nagarjuna/Pictures/Webcam/"+i+".jpg")));
      List<KEDetectedFace> faces = faceEngine.getDetector().detectFaces(pamu);
      if(faces.size() > 0)
      faceEngine.train(faces.get(0), i);
    }

    FImage pamu = ImageUtilities.createFImage(ImageIO.read(new File("/home/nagarjuna/Pictures/Webcam/"+12+".jpg")));

    List<KEDetectedFace> faces = faceEngine.getDetector().detectFaces(pamu);

    List<IndependentPair<KEDetectedFace, ScoredAnnotation<Integer>>> rfaces = faceEngine.recogniseBest(faces.get(0).getFacePatch());
    ScoredAnnotation<Integer> score = rfaces.get(0).getSecondObject();
    DisplayUtilities.display(rfaces.get(0).getFirstObject().getFacePatch());
    if(score != null) {
      System.out.println("confidence: "+score.confidence);
      System.out.println("annotation: "+score.annotation);
    }else {
      System.out.println("score is null");
    }
  }
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
score is null

Unfortunately it appears a bug crept in with the way OpenIMAJ handles similarity measures (as opposed to distance measures) in the KNNAnnotator class. A fix has been committed (revision #2560), and a new snapshot (version 1.3-SNAPSHOT) incorporating it should be available in a few hours. For the time being, you should be able to change DoubleFVComparison.CORRELATION to DoubleFVComparison.EUCLIDEAN and it should then work.

I noticed a couple of other things in your code that should probably be fixed:

  • Instead of ImageUtilities.createFImage(ImageIO.read(new File("..."))); , you should use ImageUtilities.readF(new File("...")); as this works around a number of documented bugs in ImageIO, especially with respect to jpeg reading, and you also get support for a number of other image file formats.
  • You don't need to manually perform the face detection on the query image yourself; the faceEngine.recogniseBest(FImage) method does that for you internally (together with alignment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM