OpenCV 3.0.0 FaceDetect Sample fails

I also faced the problem. The problem is in the .getPath() return an absolute path of the format.

Eg: "/C:/Users/projects/FaceDetection/bin/com/face/detection/haarcascade_frontalface_alt.xml".

So change the code like this.

CascadeClassifier faceDecetor = new CascadeClassifier(FaceDetection.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));

There is a problem with the latest openCV it doesn't work when you have spaces in your path so do this:

String s =CameraPanel.class.getResource("lbpcascade_frontalface.xml").getPath().substring(1);
String[] split = s.split("%20");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < split.length-1; i++) {
    stringBuilder.append(split[i]+" ");
}
stringBuilder.append(split[split.length-1]);
faceDetector = new CascadeClassifier(stringBuilder.toString());

This happens usually for two reasons.

  1. Cascade classifier file lbpcascade_frontalface.xml not present at specified path.
  2. Cascade classifier file is corrupted.

To get an error message instead of exception during runtime, try code sample as below. The CascadeClassifier constructor is silent, if it cannot load the cascade classifier XML. The onus is on the developer to call the empty() method and check if classifier is loaded correctly

CascadeClassifier cascade = new CascadeClassifier( CASCADE_CLASSIFIER_PATH );
if ( cascade.empty() ) {
    //handler error here
}

Exception you got is from OpenCV native code assertion here.


I ran into this same error running on a Windows box. This sample runs on linux but not Windows.

The problem is in the .getPath() call after getResource() for both the xml file and the image.

The problem is that the URL.getPath() and the URL.getFile() both return an absolute path of the format "/c:/...".

The OpenCV routines choke on this it must be "c:/..." (no leading '/'). This seems like a bug in the early part of version 3.0.0?

I hope this helps, OpenCV for Java seems like a great tool ... but it is a bit frustrating when the examples don't work.

Tags:

Java

Opencv