我在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保存在指定的位置。但是它给出了一个例外:
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
如何克服它,因为我找不到任何解决方案。请帮助
答案 0 :(得分:1)
试试此代码
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);
}
答案 1 :(得分:1)
当我尝试使应用程序检测面部时,我也遇到了同样的问题。我用了bytedeco/javacv。解析XML模型时出现问题。我使用了这个haarcascade_frontalface_alt.xml模型。然后我工作得很好。