我有以下代码,用于使用OpenCV
拍摄并改编的here来检测图像中的人脸:
import org.junit.Test;
import org.opencv.core.*;
import org.opencv.objdetect.CascadeClassifier;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.rectangle;
public class FaceRecognizer
{
@Test
public void recognizeFace() throws IOException
{
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Create a face detector from the cascade file in the resources directory.
ClassPathResource classifierResource = new ClassPathResource("lbpcascade_frontalface.xml");
CascadeClassifier faceDetector = new CascadeClassifier(classifierResource.getFile().getAbsolutePath());
ClassPathResource classPathResource = new ClassPathResource("lena.png");
Mat image = imread(classPathResource.getFile().getAbsolutePath());
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray())
{
rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
imwrite(filename, image);
//FaceRecognizer fr;//= new LBPHFaceRecognizer();
}
}
当我运行它时,它会尽早终止JVM
:
Process finished with exit code -1073740791 (0xC0000409)
这种情况发生在
faceDetector.detectMultiScale(image, faceDetections);
行。
我使用的图像如下(这是一个众所周知的示例图像):
lbpcascade_frontalface.xml
文件也是OpenCV
release pack的一部分。
为什么会崩溃?代码有问题吗?我使用OpenCV
3.4.1
和其他OpenCV
代码示例工作。