簡體   English   中英

OpenCV在JAVA人臉識別器中的准確性?

[英]OpenCV in JAVA face recognizer accuracy?

我嘗試使用來自Internet(來自yale)的樣本訓練分類器,但是當我輸入其中一張訓練圖片作為測試樣本時,我一直得到錯誤的預測(實際上,它僅輸出類別“ 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);
    }
}

好的,我知道我哪里出問題了。

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

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

做完了!

一個愚蠢的錯誤。 對不起大家。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM