OpenCV在JAVA面部识别器的准确性?

时间:2017-12-11 03:23:16

标签: java opencv face-detection face-recognition

我尝试用来自互联网(来自耶鲁)的样本训练分类器,但是当我输入一个训练图片时,我一直得到错误的预测(事实上它只输出类别" 1")测试样本。谁能给我一些暗示?我在下面附上了我的代码。

(我认为问题可能是训练样本,因为它们已经被灰度化。我再次灰度化它们,因为错误bad arguement, 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);
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,我知道我错了的地方。

Imgproc.cvtColor(img,grayImg,Imgproc.COLOR_BGR2GRAY); - &GT;

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

完成!

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