Facede detection [closed]

asked 2016-02-04 12:13:40 -0600

updated 2016-02-05 05:01:39 -0600

 package application;



import org.opencv.core.Core;
import org.opencv.core.Rect;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

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().substring(1));
        Mat image = Imgcodecs.imread(FaceDetector.class.getResource("D:\\wallpaper\\my.jpg").getPath().substring(1));

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

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

        for (Rect rect : faceDetections.toArray()) {


             Imgproc.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.jpg";
         System.out.println(String.format("Writing %s", filename));
         Imgcodecs.imwrite(filename, image);
     }
 }

OUTPUT::::::

Running FaceDetector......................... Exception in thread "main" java.lang.NullPointerException at application.FaceDetector.main(FaceDetector.java:22)

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-09-18 05:30:54.740004

Comments

1

n00b lesson 1: whenever you load resouces, check if it succeeded.

berak gravatar imageberak ( 2016-02-04 12:26:34 -0600 )edit

no it is not succeeded. what to do now? and thanks for responce

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-04 22:03:31 -0600 )edit
1

since this is on desktop, try to skip the FaceDetector.class.getResource() part, and use an absolute path (esp. for the cascade file)

berak gravatar imageberak ( 2016-02-04 22:47:55 -0600 )edit

sorry bro I don't get it...

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-04 23:09:08 -0600 )edit

CascadeClassifier faceDetector = new CascadeClassifier(FaceDetector.class.getResource("C:\opencv\sources\data\haarcascades\haarcascade_frontalface_alt").getPath().substring(1));

I add path but still same error occur

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-04 23:12:56 -0600 )edit

new CascadeClassifier("c:/opencv/data/haarcascades/haarcascade_frontalface_alt.xml"); or similar

berak gravatar imageberak ( 2016-02-04 23:14:03 -0600 )edit

I do what you say but still same error

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-04 23:21:01 -0600 )edit

new CascadeClassifier("c:/opencv/data/haarcascades/haarcascade_frontalface_alt.xml");

output:: Running FaceDetector Exception in thread "main" java.lang.NullPointerException at application.FaceDetector.main(FaceDetector.java:23)

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-04 23:27:58 -0600 )edit

You need to debug your code and identify the part where it goes wrong... until then it is quite difficult to state what is wrong. We now know your classifier is loaded correctly, but how about image? Can you visualise it before you do the classifier detector and report back the result?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-02-05 06:30:03 -0600 )edit

yes image is visualize and I do debug and it stop and give error in number 23 line "Mat image = Imgcodecs.imread(FaceDetector.class.getResource("D:/wallpaper/my.jpg").getPath().substring(1));" @StevenPuttemans

Rudy_1204 gravatar imageRudy_1204 ( 2016-02-06 06:53:50 -0600 )edit