FaceRecognizer在JavaCv中用Mat预测

时间:2016-03-18 01:31:18

标签: java opencv javacv

我试图预测网络摄像头抓住的脸,当程序抓取图像然后faceDetection的方法尝试找到一张脸。如果找到一个面,则将IplImage转换为Mat,然后使用该图像进行预测。但是当我尝试将IplImage转换为Mat并将其用于预测时,会被Opencv抛出错误。

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x53c5699e, pid=6076, tid=4256
#
# JRE version: Java(TM) SE Runtime Environment (8.0_40-b26) (build 1.8.0_40-b26)
# Java VM: Java HotSpot(TM) Client VM (25.40-b25 mixed mode, sharing windows-x86 )
# Problematic frame:
# C  [opencv_face300.dll+0x699e]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\HM\Desktop\detect\hs_err_pid6076.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

以下是两种方法:

detectFace:

private IplImage detectFace(IplImage image, CvScalar color) {

int number = 3;

IplImage imageGray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);

cvCvtColor(image, imageGray, CV_BGR2GRAY); 


IplImage smallImg = IplImage.create(imageGray.width()/2, imageGray.height()/2, IPL_DEPTH_8U, 1);
cvResize(imageGray, smallImg, CV_INTER_LINEAR);

cvEqualizeHist(smallImg, smallImg);

CvMemStorage storage = CvMemStorage.create();

if(faceDetector.isNull())
    faceDetector = new CvHaarClassifierCascade(cvLoad(dataBase+"\\haarcascade_frontalface_alt.xml"));

CvSeq faces = cvHaarDetectObjects(smallImg, faceDetector, storage, 1.3, number, CV_HAAR_DO_CANNY_PRUNING);

cvClearMemStorage(storage);

int total = faces.total();

for (int i = 0; i < total; i++) {
    CvRect r = new CvRect(cvGetSeqElem(faces, i));

    //Prints person recognized ID
    System.out.println("person recognized: "+personRecognizer(grisImagen));

    cvRectangle(image, cvPoint( r.x()*2, r.y()*2 ),cvPoint( (r.x() + r.width())*2,(r.y() + r.height())*2 ), color, 2, CV_AA, 0);
}

cvReleaseImage(imageGray);
cvReleaseImage(smallImg);

return image;
}//end of face detection

personRecognizer(此处):

    public int personRecognizer(IplImage image){
    int personRecognized = -1;

    //Get a mat from the IplImage
    Mat imageMat = new Mat(image);

    try{

    //Try to predicts the person within
    personRecognized = recognizer.predict(imageMat);

    }catch(Exception e){
        System.err.println("Error, Couldn't predict the image");
    }

    return personRecognized;
}

0 个答案:

没有答案