Javacv面部识别

时间:2014-03-23 20:19:31

标签: opencv javacv face-recognition

我试图将FaceRecognition与javacv一起使用。但是当我有超过5张火车图像时,我收到了这个错误:

  

#Java Runtime Environment检测到致命错误:
  #
  #EXCEPTION_ACCESS_VIOLATION(0xc0000005)在pc = 0x6a30b400,pid = 4856,tid = 32
  #
  #JRE版本:Java(TM)SE运行时环境(7.0_51-b13)(版本1.7.0_51-b13)
  #Java VM:Java HotSpot(TM)客户端VM(24.51-b03混合模式,共享windows-x86)
  #有问题的框架:
  #C [opencv_core246.dll + 0x4b400]
  #
  #无法编写核心转储。默认情况下,在Windows的客户端版本上未启用小型转储   #
  #包含更多信息的错误报告文件保存为:
  #C:\ Users \ reco \ workspace \ hellow \ hs_err_pid4856.log
  #
  #如果您想提交错误报告,请访问:
  #http://bugreport.sun.com/bugreport/crash.jsp
  #崩溃发生在Java虚拟机外部的本机代码中   #查看有问题的框架,了解报告错误的位置   #

这是我的代码:

import com.googlecode.javacv.cpp.opencv_core;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_contrib.*;
import java.io.File;
import java.io.FilenameFilter;

public class OpenCVFaceRecognizer {
public static void main(String[] args) {
    String trainingDir = "C:/Users/reco/workspace/hellow";

    IplImage testImage = cvLoadImage("C:/Users/reco/workspace/0.png");

    File root = new File(trainingDir);

    FilenameFilter pngFilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".png");
        }
    };

    File[] imageFiles = root.listFiles(pngFilter);

     MatVector images = new MatVector(imageFiles.length);

    int[] labels = new int[imageFiles.length];

    int counter = 0;
    int label;

    IplImage img;
    IplImage grayImg;

    for (File image : imageFiles) {
        img = cvLoadImage(image.getAbsolutePath());
        String temp= image.getName();
        label = Integer.parseInt(temp.charAt(0)+"");

        grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);

        cvCvtColor(img, grayImg, CV_BGR2GRAY);

        images.put(counter, grayImg);

        labels[counter] = label;

        counter++;
    }

    IplImage greyTestImage = IplImage.create(testImage.width(), testImage.height(), IPL_DEPTH_8U, 1);

    //FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
     FaceRecognizer faceRecognizer = createEigenFaceRecognizer();
    // FaceRecognizer faceRecognizer = createLBPHFaceRecognizer()

    faceRecognizer.train(images, labels);

    cvCvtColor(testImage, greyTestImage, CV_BGR2GRAY);

    int predictedLabel = faceRecognizer.predict(greyTestImage);

    System.out.println("Predicted label: " + predictedLabel);
}
}

编辑::我刚删除

    grayImg = IplImage.create(img.width(), img.height(), IPL_DEPTH_8U, 1);

    cvCvtColor(img, grayImg, CV_BGR2GRAY);

它有效:)

1 个答案:

答案 0 :(得分:2)

    IplImage img;
    IplImage grayImg=null;

    for (File image : imageFiles) {
        img = cvLoadImage(image.getAbsolutePath(),CV_BGR2GRAY);
        int yer = image.getName().indexOf(".");
        String isim=image.getName().substring(0,yer);

        label = Integer.parseInt(isim);

        images.put(counter, img);

        labels[counter] = label;

        counter++;
    }

这是我的代码的最后一个,它就像一个魅力:)