2012-09-11 98 views
2

我有一大堆圖像,需要過濾掉所有人臉。有沒有這樣一個Java庫,它提供了一個將圖像作爲輸入並輸出yes或no的單一方法?Java人臉檢測庫

回答

4

您可以使用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; 
    } 
}