我试图使用OpenCV在java中制作面部识别应用程序。人脸检测工作完美,但识别会返回不存在的标签。我试图使用来自火车照片的测试照片,它返回0,但是照片的标签是4.有时它返回一些疯狂的数字(底片或非常大)。
以下是培训的ma代码:
public void trainRecognizer(){
File root = new File("photos");
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
}
};
File[] imageFiles = root.listFiles(imgFilter);
List<Mat> images = new ArrayList<>();
Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
int[] labelsArray = new int[imageFiles.length];
int counter = 0;
for (File image : imageFiles) {
Mat img = imread(image.getAbsolutePath(), CV_LOAD_IMAGE_GRAYSCALE);
int label = Integer.parseInt(image.getName().split("\\-")[0]);
images.add(img);
labels.put(counter, label);
labelsArray[counter] = label;
counter++;
}
faceRecognizer = FisherFaceRecognizer.create();
System.out.println("Starting train");
faceRecognizer.train(images, labels);
System.out.println("Training complete");
recognitionStart = true;
}
这是测试:
int[] label = {-1};
double[] conf = {0.0};
Mat testImage = imread("photos/4-7.jpg", CV_LOAD_IMAGE_GRAYSCALE);
faceRecognizer.predict(testImage, label, conf);
System.out.println("Predicted label: " + label[0]);
System.out.println("Confidence of the prediction: " + conf[0]);