OpenIMAJ中的rfaces.get(0).getSecondObject()面部识别始终返回null

时间:2014-07-15 17:58:54

标签: java nullpointerexception openimaj

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");
    }
  }
}

输出:

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

1 个答案:

答案 0 :(得分:1)

不幸的是,在KNNAnnotator类中,OpenIMAJ处理相似性度量(而不是距离度量)的方式似乎出现了一个错误。已提交修复程序(修订版#2560),并且应在几小时内提供包含它的新快照(版本1.3-SNAPSHOT)。目前,您应该可以将DoubleFVComparison.CORRELATION更改为DoubleFVComparison.EUCLIDEAN,然后就可以了。

我注意到你的代码中还有一些其他的东西应该修复:

  • 您应该使用ImageUtilities.createFImage(ImageIO.read(new File("...")));代替ImageUtilities.readF(new File("..."));,因为这可以解决ImageIO中许多记录的错误,特别是在jpeg读取方面,您还可以获得对其他一些图像文件的支持格式。
  • 您不需要自己在查询图像上手动执行面部检测; faceEngine.recogniseBest(FImage)方法在内部(与对齐一起)为您执行此操作