gpt4 book ai didi

java - 使用opencv进行人脸检测给出异常

转载 作者:行者123 更新时间:2023-11-30 09:09:36 25 4
gpt4 key购买 nike

我正在尝试在 java 中使用 opencv 进行人脸检测。我的代码是:

            String CASCADE_FILE ="C:/Users/admin/Desktop/javacv/src/javacv/lbpcascade_frontalface.xml";
String OUT_FILE = "markedFaces.jpg";

IplImage origImg = cvLoadImage("C:/Users/admin/Desktop/javacv/src/javacv/lena.png", 1);
//IplImage origImg = cvLoadImage(args[0]);

// convert to grayscale
IplImage grayImg = IplImage.create(origImg.width(),origImg.height(), IPL_DEPTH_8U, 1);
cvCvtColor(origImg, grayImg, CV_BGR2GRAY);

// scale the grayscale (to speed up face detection)
IplImage smallImg = IplImage.create(grayImg.width()/SCALE,grayImg.height()/SCALE, IPL_DEPTH_8U, 1);
cvResize(grayImg, smallImg, CV_INTER_LINEAR);

// equalize the small grayscale
IplImage equImg = IplImage.create(smallImg.width(),smallImg.height(), IPL_DEPTH_8U, 1);
cvEqualizeHist(smallImg, equImg);

// create temp storage, used during object detection
CvMemStorage storage = CvMemStorage.create();

// instantiate a classifier cascade for face detection

CvHaarClassifierCascade cascade =new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));
System.out.println("Detecting faces...");

CvSeq faces = cvHaarDetectObjects(equImg, cascade, storage,1.1, 3, CV_HAAR_DO_CANNY_PRUNING);

cvClearMemStorage(storage);

// draw thick yellow rectangles around all the faces
int total = faces.total();
System.out.println("Found " + total + " face(s)");

for (int i = 0; i < total; i++) {

CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(origImg, cvPoint( r.x()*SCALE, r.y()*SCALE ),cvPoint( (r.x() + r.width())*SCALE,(r.y() + r.height())*SCALE ),CvScalar.RED, 6, CV_AA, 0);

String strRect = String.format("CvRect(%d,%d,%d,%d)", r.x(), r.y(), r.width(), r.height());

System.out.println(strRect);
//undo image scaling when calculating rect coordinates
}

if (total > 0) {
System.out.println("Saving marked-faces version of " + " in " + OUT_FILE);

cvSaveImage(OUT_FILE, origImg);
}

我将 xml 和 origImg 保存在它们指定的位置。但它给出了一个异常(exception):

OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file ..\..\..\..\opencv\modules\core\src\persistence.cpp, line 4991
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\persistence.cpp:4991: error: (-2) The node does not represent a user object (unknown type?) in function cvRead

如何克服它,因为我找不到任何解决方案。请帮忙

最佳答案

试试这段代码

private static final String CASCADE_FILE = "./others/haarcascade_frontalface_alt.xml";

public FaceDetection(String inputFile, String outputFile) throws Exception {


// Load the original image.
IplImage originalImage = cvLoadImage(inputFile, 1);
// We need a grayscale image in order to do the recognition, so we create a new image of the same size as the original one.
IplImage grayImage = IplImage.create(originalImage.width(),originalImage.height(), IPL_DEPTH_8U, 1);
// We convert the original image to grayscale.
cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);
CvMemStorage storage = CvMemStorage.create();
// We instantiate a classifier cascade to be used for detection, using the cascade definition
CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(cvLoad(CASCADE_FILE));
// We detect the faces.
CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1, 0);
// We iterate over the discovered faces and draw yellow rectangles around them.
for (int i = 0; i < faces.total(); i++) {
CvRect r = new CvRect(cvGetSeqElem(faces, i));
cvRectangle(originalImage, cvPoint(r.x(), r.y()), cvPoint(r.x() + r.width(), r.y() + r.height()), CvScalar.RED, 1, CV_AA, 0);
}

// Save the image to a new file.
cvSaveImage(outputFile, originalImage);
}

关于java - 使用opencv进行人脸检测给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com