2015-09-06 27 views
1
public class FaceDetector { 

    public static void main(String[] args) { 

     System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
     System.out.println("\nRunning FaceDetector"); 

     CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("\\haarcascade_frontalface_alt.xml").getPath()); 
     Mat image = Highgui.imread(FaceDetector.class.getResource("abc.jpg").getPath()); 

     MatOfRect faceDetections = new MatOfRect(); 
     faceDetector.detectMultiScale(image, faceDetections); 

     System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); 

     for (Rect rect : faceDetections.toArray()) { 
      Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), 
        new Scalar(0, 255, 0)); 
     } 

     String filename = "ouput.png"; 
     System.out.println(String.format("Writing %s", filename)); 
     Highgui.imwrite(filename, image); 
    } 
} 

的路徑這是我的錯誤形象:獲得人臉檢測空指針異常而界定「\ haarcascade_frontalface_alt.xml」

error

越來越零點異常錯誤。如何解決我也嘗試給完整路徑,但同樣的問題。請幫幫我。

+0

你使用絕對路徑解決了這個問題嗎?它不適用於我 –

+0

沒有爲我工作 – vis

回答

0

您需要將絕對路徑傳遞至CascadeClassifier。例如:

CascadeClassifier faceDetector = new CascadeClassifier("D:\\haarcascade_frontalface_alt.xml"); 

並且對圖像進行相同的讀寫操作。這是有線的,但在這種情況下它不能讀取文件是事實。我的情況也是如此,我已經解決了。

+0

沒有爲我工作 –