1

我正在尝试在android中使用opencv开发面部识别我已经成功地将opencv与JavaCv和Javacpp集成我从sdcard训练数据图像然后用相同的图像识别但每次都获得'0'的信心虽然我改变了我的数据,但仍然它即使使用相似的图像,也显示出“0”的信心。

  private void imagePrediction() {

    String trainingDir = Environment.getExternalStorageDirectory()
            + File.separator + "SpySignage_filter";
    File root = new File(trainingDir);
    FilenameFilter jpgFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".jpg");
        }
    };

    File[] imageFiles = root.listFiles(jpgFilter);
    opencv_core.MatVector images = new opencv_core.MatVector(imageFiles.length);
    int[] labels = new int[imageFiles.length];
    int counter = 0;
    int label;
    opencv_core.IplImage img;
    opencv_core.IplImage grayImg;
    for (int i = 0; i < imageFiles.length; i++) {
        // Get image and label:
        img = cvLoadImage(imageFiles[i].getAbsolutePath());
        label = Integer.parseInt(imageFiles[i].getName().split("\\-")[0]);

        // Convert image to grayscale:
        grayImg = opencv_core.IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);
        cvCvtColor(img, grayImg, CV_BGR2GRAY);
        // Append it in the image list:
        images.put(counter, grayImg);
        // And in the labels list:
        labels[counter] = label;
        // Increase counter for next image:
        counter++;
    }

    // opencv_contrib.FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
    //   opencv_contrib.FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
    // FaceRecognizer faceRecognizer = createLBPHFaceRecognizer()
    opencv_contrib.FaceRecognizer faceRecognizer =  com.googlecode.javacv.cpp.opencv_contrib.createLBPHFaceRecognizer();

 //   opencv_contrib.FaceRecognizer faceRecognizer = createLBPHFaceRecognizer();
    faceRecognizer.train(images, labels);



    String dataTorecognize = Environment.getExternalStorageDirectory()
            + File.separator + "SpySignage_filter";
    File rootDir = new File(dataTorecognize);
    File[] imagelist = rootDir.listFiles(jpgFilter);



 //        for (int j = 0; j < imagelist.length; j++) {
 //            opencv_core.IplImage testImage = cvLoadImage( imagelist[j].getAbsolutePath());
    opencv_core.IplImage testImage = cvLoadImage(Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpySignage_filter/2-hrithik_two.jpg");

    opencv_core.IplImage greyTestImage = opencv_core.IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);
        cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);
        // And get a prediction:
     //   int predictedLabel = faceRecognizer.predict(greyTestImage);
     //   System.out.println("Predicted label: " + predictedLabel);
        int n[] = new int[1];
        double p[] = new double[1];
        //  opencv_core.IplImage ipl = MatToIplImage(m,WIDTH, HEIGHT);
        // IplImage ipl = MatToIplImage(m,-1, -1);
        faceRecognizer.predict(greyTestImage, n, p);
        if (n[0] != -1) {
            mProb = (int) p[0];
        } else
            mProb = -1;
        //  if ((n[0] != -1)&&(p[0]<95))
        if (n[0] != -1) {
            Toast.makeText(getApplicationContext(), "Image Recognized Confidence =" + mProb, Toast.LENGTH_LONG).show();
            // return label.get(n[0]);
        } else {
            Toast.makeText(getApplicationContext(), "Image Not Recognized", Toast.LENGTH_LONG).show();
        }

}
4

0 回答 0