我试图将DoGSIFTFeatureComparator与FaceSimilarityEngine一起使用。这是我的代码:
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import org.openimaj.image.DisplayUtilities;
import org.openimaj.image.FImage;
import org.openimaj.image.MBFImage;
import org.openimaj.image.colour.RGBColour;
import org.openimaj.image.ImageUtilities;
import org.openimaj.image.processing.face.detection.HaarCascadeDetector;
import org.openimaj.image.processing.face.detection.DetectedFace;
import org.openimaj.image.processing.face.feature.DoGSIFTFeature;
import org.openimaj.image.processing.face.feature.comparison.DoGSIFTFeatureComparator;
import org.openimaj.image.processing.face.similarity.FaceSimilarityEngine;
import org.openimaj.math.geometry.shape.Rectangle;
public class FaceSimilarity {
public static void main(String[] args) throws IOException {
final File iFile1 = new File("/Path/to/first/image.jpg");
final File iFile2 = new File("/Path/to/second/image.jpg");
final MBFImage mbImg1 = ImageUtilities.readMBF(iFile1);
final MBFImage mbImg2 = ImageUtilities.readMBF(iFile2);
final FImage image1 = mbImg1.flatten();
final FImage image2 = mbImg2.flatten();
final HaarCascadeDetector detector = HaarCascadeDetector.BuiltInCascade.frontalface_default.load();
final DoGSIFTFeature.Extractor extractor = new DoGSIFTFeature.Extractor();
final DoGSIFTFeatureComparator comparator = new DoGSIFTFeatureComparator();
final FaceSimilarityEngine<DetectedFace, DoGSIFTFeature, FImage> engine = new FaceSimilarityEngine<>(detector, extractor, comparator);
engine.setQuery(image1, "image1");
engine.setTest(image2, "image2");
engine.performTest();
// rest of the code
}
}
当FaceSimilarityEngine使用FKEFaceDetector,FacePathFeature Extractor和FaceFVComparator时,相同的代码也适用,但上面的代码导致以下错误消息:
Exception in thread "main" java.lang.NullPointerException
at org.openimaj.math.geometry.transforms.residuals.TransformedSITR2d.computeResidual(TransformedSITR2d.java:76)
at org.openimaj.math.model.fit.SimpleModelFitting.fitData(SimpleModelFitting.java:117)
at org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d.findMatches(ConsistentLocalFeatureMatcher2d.java:138)
at org.openimaj.image.processing.face.feature.comparison.DoGSIFTFeatureComparator.compare(DoGSIFTFeatureComparator.java:152)
at org.openimaj.image.processing.face.feature.comparison.DoGSIFTFeatureComparator.compare(DoGSIFTFeatureComparator.java:68)
at org.openimaj.image.processing.face.similarity.FaceSimilarityEngine.performTest(FaceSimilarityEngine.java:224)
at // line of code with engine.performTest();
虽然我可能DoGSIFTFeatureComparator没有使用FaceSimilarityEngine,所以我创建了这个类(除了FaceSimilarityEngine之外的相同导入):
public class DogSiftCompare {
public static void main(String[] args) throws IOException {
// created FImages the same way as previous code
final HaarCascadeDetector detector = HaarCascadeDetector.BuiltInCascade.frontalface_default.load();
final DoGSIFTFeature.Extractor extractor = new DoGSIFTFeature.Extractor();
final DoGSIFTFeatureComparator comparator = new DoGSIFTFeatureComparator();
List<DetectedFace> faces = detector.detectFaces(image1);
List<DetectedFace> faces2 = detector.detectFaces(image2);
List<DoGSIFTFeature> features1 = new ArrayList<>();
List<DoGSIFTFeature> features2 = new ArrayList<>();
if (!faces.isEmpty()) {
for (DetectedFace face : faces) {
DoGSIFTFeature feature = extractor.extractFeature(face);
features1.add(feature);
}
}
if (!faces2.isEmpty()) {
for (DetectedFace face : faces2) {
DoGSIFTFeature feature = extractor.extractFeature(face);
features2.add(feature);
}
}
DetectedFace face1 = null;
DetectedFace face2 = null;
double bestScore = Double.MAX_VALUE;
if (!features1.isEmpty() && !features2.isEmpty()) {
for (int i = 0; i < features1.size(); i++) {
for (int j = 0; j < features2.size(); j++) {
double score = comparator.compare(features1.get(i), features2.get(j));
if (score < bestScore) {
bestScore = score;
face1 = faces.get(i);
face2 = faces2.get(j);
}
}
}
}
// rest of code ...
}
}
但是,运行此代码会产生与上一代码相同的错误:
Exception in thread "main" java.lang.NullPointerException
at org.openimaj.math.geometry.transforms.residuals.TransformedSITR2d.computeResidual(TransformedSITR2d.java:76)
at org.openimaj.math.model.fit.SimpleModelFitting.fitData(SimpleModelFitting.java:117)
at org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d.findMatches(ConsistentLocalFeatureMatcher2d.java:138)
at org.openimaj.image.processing.face.feature.comparison.DoGSIFTFeatureComparator.compare(DoGSIFTFeatureComparator.java:152)
at //line of code with double score = comparator.compare(features1.get(i), features2.get(j));
所以我想我问我是否缺少这个特定功能比较器的关键变量,或者这是DoGSIFTFeatureComparator本身的问题?