1

I'm trying to build a detector for face detection in images. I use the OpenCV library, version 2.4.11.

Now my source code looks like following :

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;

public class DetectFaces {

    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("pic.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);
    }
}

Now I get an FaceDetector cannot be resolved to a type exception every time FaceDetector.class is called.

While working on this implementation I am following the tutorial

Any suggestions are welcome. Thank You

Jürgen K.
  • 3,427
  • 9
  • 30
  • 66
  • 1
    is it in the same package as `DetectFaces` class? – jlordo Mar 05 '17 at 17:23
  • You didn't read, and copy-paste, the tutorial correctly. The class you are writing _is_ named `FaceDetector`, not `DetectFaces`...! – Tunaki Mar 05 '17 at 17:26
  • @Tunaki. You are right, thanks. But how is this a dublicate to the topic you have linked? – Jürgen K. Mar 05 '17 at 17:29
  • Because it explains everything that can cause the error you're having. One of them being that you typed the name incorrectly. – Tunaki Mar 05 '17 at 17:34
  • Now after following your advice and changing the name I get a Null Pointer Exception in the following line: DetectFaces.class.getResource("haarcascade_frontalface_alt.xml").getPath() – Jürgen K. Mar 05 '17 at 17:40

1 Answers1

0

Have you forgotten to import the class? Or is it in the same folder?

Jonas G
  • 23
  • 5