Facial detection - null pointer error [closed]
Hello,
I've followed this quide to detect faces using OpenCV: http://docs.opencv.org/doc/tutorials/introduction/desktop_java/java_dev_intro.html# The same code in the quide gave me the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Cannot make a static reference to the non-static method getClass() from the type Object Cannot make a static reference to the non-static method getClass() from the type Object
at faced.DetectFaceDemo.main(DetectFaceDemo.java:23)
I have changed the lines with errors: CascadeClassifier faceDetector = new CascadeClassifier(DetectFaceDemo.class.getResource("/lbpcascade_frontalface.xml").getPath()); Mat image = Highgui.imread(DetectFaceDemo.class.getResource("/lena.png").getPath());
Now I have the following errors:
Running DetectFaceDemo Exception in thread "main" java.lang.NullPointerException at faced.DetectFaceDemo.main(DetectFaceDemo.java:26)
My code is:
package faced;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
//
//Detects faces in an image, draws boxes around them, and writes the results
//to "faceDetection.png".
//
class DetectFaceDemo {
public static void main(String[] args) {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
System.out.println("\nRunning DetectFaceDemo");
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());
CascadeClassifier faceDetector = new CascadeClassifier(DetectFaceDemo.class.getResource("/lbpcascade_frontalface.xml").getPath());
Mat image = Highgui.imread(DetectFaceDemo.class.getResource("/lena.png").getPath());
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// Draw a bounding box around each face.
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));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
So, why it dowsn't find my xml? Or what should I do? I have tryed to coppy the xml and lena's image from home/src/main/resources as they say in the guide to home/workspace/face_detection/src/faced where I have the file "DetectFaceDemo.java" that I'm trying to run. I am using eclipse for my project. I am sorry if the way I wrote is not arranged.
Is the .xml where your .class is?