【问题标题】:OpenCV in JAVA face recognizer accuracy?JAVA人脸识别器中的OpenCV准确率?
【发布时间】:2018-05-24 13:37:24
【问题描述】:

我尝试使用来自互联网(来自耶鲁大学)的样本来训练分类器,但是当我输入一张训练图片作为测试样本时,我一直得到错误的预测(实际上它只输出类别“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);
    }
}

【问题讨论】:

    标签: java opencv face-detection face-recognition


    【解决方案1】:

    好的,伙计们,我知道我出错的地方。

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

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

    完成!

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

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 2020-10-18
      • 2016-03-17
      • 2017-12-06
      • 2015-07-05
      • 2012-03-18
      • 2023-03-19
      • 2021-02-20
      相关资源
      最近更新 更多