0

I'm using opencv with java for a project and I need to detect only open eyes of the face image. But With the java code I'm using it detects both closed and open eyes. below is the code I have.

This is the url of how to set OpenCV in eclipse - http://docs.opencv.org/2.4/doc/tutorials/introduction/java_eclipse/java_eclipse.html

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;import org.opencv.core.Size;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.imgcodecs.Imgcodecs;


public class Hello {

 public static void main( String[] args )
   {



        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        System.out.println("\nRunning FaceDetector");

        CascadeClassifier faceDetector = new CascadeClassifier("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
        CascadeClassifier eyeDetector = new CascadeClassifier("C:\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");

        Mat image = Imgcodecs.imread("E:/hasah/PPHOTOO/seanciminoeyesclosedlar.jpg");


        String faces;
        String eyes;


        MatOfRect faceDetections = new MatOfRect();
        MatOfRect eyeDetections = new MatOfRect();

        Mat face;
        Mat crop = null;
        Mat circles = new Mat();
        faceDetector.detectMultiScale(image, faceDetections);

   System.out.println("face detectino array size: " + faceDetections.toArray().length);
   for (int i = 0; i< faceDetections.toArray().length; i++){

            faces = "Face"+i+".png";

             face = image.submat(faceDetections.toArray()[i]);
             crop = face.submat(4, (2*face.width())/3, 0, face.height());
            Imgcodecs.imwrite(faces, face);
             eyeDetector.detectMultiScale(crop, eyeDetections, 1.1, 2, 0,new Size(30,30), new Size()); 

             if(eyeDetections.toArray().length ==0){

                 System.out.println(" Not a face" + i);
             }else{

                 System.out.println("Face with " + eyeDetections.toArray().length + "eyes" );

                 for (int j = 0; j< eyeDetections.toArray().length ; j++){

                    System.out.println("Eye" );
                    Mat eye = crop.submat(eyeDetections.toArray()[j]);
                    eyes = "Eye"+j+".png";
                    Imgcodecs.imwrite(eyes, eye);

                 }
             }
         }

   }

}

UHS
  • 9
  • 5
  • 1
    You mat have a look at this [thread](http://opencv-users.1802565.n2.nabble.com/with-OpenCV-haarcascades-can-i-detect-only-open-eye-or-closed-eye-td7534953.html) – ZdaR Jun 02 '17 at 09:03
  • Thanks a lot @ZdaR . it does help me :) . But these depend on the quality of the images. for some images it worked very well and for some it didn't :( it's not a problem of the code though. – UHS Jun 02 '17 at 10:22
  • Is there a way I can edit XMLs or something and fix it. it detect partially closed eyes as opened too. – UHS Jun 02 '17 at 10:57
  • If you want to have full control over the haar cascade, then you may try [training custom haar cascade](http://docs.opencv.org/trunk/dc/d88/tutorial_traincascade.html), Or if you are open to install other external library, then you may look for [dlib](http://dlib.net/face_landmark_detection.py.html) which would give you 68 facial landmarks and you can easily write some logic to detect if the eye is opened or closed. – ZdaR Jun 02 '17 at 11:10
  • Thanks a lot. will look into these – UHS Jun 26 '17 at 03:55

0 Answers0