gpt4 book ai didi

java - JAVA 中的 OpenCV 人脸识别器准确性?

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:06 26 4
gpt4 key购买 nike

我试图用来自互联网(来自耶鲁大学)的样本训练分类器,但是当我输入一张训练图片作为测试样本时,我总是得到错误的预测(实际上它只输出类别“1”)。谁能给我一些提示?我在下面附上了我的代码。

(我认为问题可能出在训练样本上,因为它们已经灰度化了。我再次对它们进行了灰度化,因为错误 bad argument, size(1,30000), (10000,66).... 如果我不这样做,事情就会出现。)

import org.opencv.face.FaceRecognizer;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.core.*;
import org.opencv.face.Face;
import org.opencv.imgcodecs.Imgcodecs;
import java.io.File;
import java.io.FilenameFilter;
import java.util.List;
import java.util.ArrayList;
import org.opencv.imgproc.Imgproc;

public class FaceRecognization {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String trainingDir = "C:\\Users\\songli\\Desktop\\yale\\train";
String testImgPath = "C:\\Users\\songli\\Desktop\\yale\\5-6.bmp";
Mat testImg = Imgcodecs.imread(testImgPath);
Mat gray = new Mat();
File root = new File(trainingDir);
FilenameFilter bmpFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".bmp");
}
};

File[] imageFiles = root.listFiles(bmpFilter);
List<Mat> list = new ArrayList<Mat>(imageFiles.length);
int[] labels = new int[imageFiles.length];

int counter = 0;
int label;
Mat grayImg = new Mat();
Mat grayTestImg = new Mat();
Mat img = new Mat();
for (File image : imageFiles) {
img = Imgcodecs.imread(image.getAbsolutePath());
// System.out.print(img.elemSize());
label = Integer.parseInt(image.getName().split("\\-")[0]);
grayImg.create(img.width(), img.height(), 1);
Imgproc.cvtColor(img, grayImg, Imgproc.COLOR_BGR2GRAY);
list.add(grayImg);
labels[counter] = label;
counter++;
}
// System.out.print(labels[11]);
MatOfInt labels1 = new MatOfInt();
labels1.fromArray(labels);
FaceRecognizer fr = Face.createEigenFaceRecognizer();
fr.train(list, labels1);
grayTestImg.create(testImg.width(), testImg.height(), 1);
Imgproc.cvtColor(testImg, grayTestImg, Imgproc.COLOR_BGR2GRAY);

int predictedlabel = fr.predict_label(grayTestImg);
// Imgcodecs.imwrite("C:\\Users\\songli\\Desktop\\testImg.jpg",
// testImg);

// int[] predLabel = new int[1];
// double[] confidence = new double[1];
// int result = -1;
// fr.predict(testImgGrey,predLabel,confidence);
// result = predLabel[0];
System.out.println("Predicted label: " + predictedlabel);
}
}

最佳答案

好了伙计们,我知道我错在哪里了。

Imgproc.cvtColor(img, grayImg, Imgproc.COLOR_BGR2GRAY); ->

Imgproc.cvtColor(img,img,Imgproc.COLOR_BGR2GRAY);

完成!

一个愚蠢的错误。对不起大家。

关于java - JAVA 中的 OpenCV 人脸识别器准确性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47746007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com