我有一大堆图像,需要过滤掉所有有人脸的图像。是否有这样一个 Java 库提供一个单一的方法,将图像作为输入并输出是或否?
最佳答案
您可以使用 JavaCV 进行人脸检测. JavaCV 是 OpenCV 的 Java 包装器。它不提供真/假,而是提供图片中人脸的位置。你可以这样做:
public class FaceDetect {
// Create memory for calculations
CvMemStorage storage = null;
// Create a new Haar classifier
CvHaarClassifierCascade classifier = null;
// List of classifiers
String[] classifierName = {
"./classifiers/haarcascade_frontalface_alt.xml",
"./classifiers/haarcascade_frontalface_alt2.xml",
"./classifiers/haarcascade_profileface.xml" };
public FaceDetect() {
// Allocate the memory storage
storage = CvMemStorage.create();
// Load the HaarClassifierCascade
classifier = new CvHaarClassifierCascade(cvLoad(classifierName[0]));
// Make sure the cascade is loaded
if (classifier.isNull()) {
System.err.println("Error loading classifier file");
}
}
public boolean find (Image value ){
// Clear the memory storage which was used before
cvClearMemStorage(storage);
if(!classifier.isNull()){
// Detect the objects and store them in the sequence
CvSeq faces = cvHaarDetectObjects(value.getImage(), classifier,
storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING);
// Get the number of faces found.
int total = faces.total();
if (total > 0) {
return true;
}
}
return false;
}
}
关于Java人脸检测库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12378567/