2015-09-06 23 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

没有为我工作 –