title: Opencv - detecting whether the eye is closed or open
body: <p>I am working on a project where we are trying to detect whether the eye is closed or open in a picture. What we have done so far is that we detected the face, then the eyes. Then we applied hough transform, hoping that the iris would be the only circle when the eye is open. The problem is that when the eye is closed, it produces a circle as well:</p>
  <p>Here is the code:</p>
  <pre><code>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.core.Size;
  import org.opencv.highgui.Highgui;
  import org.opencv.objdetect.CascadeClassifier;
  import org.opencv.imgproc.Imgproc;
  
  
  
   
  public class FaceDetector {
   
      public static void main(String[] args) {
          
      
   
          System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
          System.out.println(&quot;\nRunning FaceDetector&quot;);
   
          CascadeClassifier faceDetector = new CascadeClassifier(&quot;D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml&quot;);
          CascadeClassifier eyeDetector = new CascadeClassifier(&quot;D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml&quot;);
         
          Mat image = Highgui.imread(&quot;C:\\Users\\Yousra\\Desktop\\images.jpg&quot;);
          Mat gray = Highgui.imread(&quot;C:\\Users\\Yousra\\Desktop\\eyes\\E7.png&quot;);
          
          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);
          
     for (int i = 0; i&lt; faceDetections.toArray().length; i++){
              
              faces = &quot;Face&quot;+i+&quot;.png&quot;;
              
               face = image.submat(faceDetections.toArray()[i]);
               crop = face.submat(4, (2*face.width())/3, 0, face.height());
              Highgui.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(&quot; Not a face&quot; + i);
               }else{
                   
                   System.out.println(&quot;Face with &quot; + eyeDetections.toArray().length + &quot;eyes&quot; );
                   
                   for (int j = 0; j&lt; eyeDetections.toArray().length ; j++){
                       
                      System.out.println(&quot;Eye&quot; );
                      Mat eye = crop.submat(eyeDetections.toArray()[j]);
                      eyes = &quot;Eye&quot;+j+&quot;.png&quot;;
                      Highgui.imwrite(eyes, eye);
                       
                   }
               }
           }
               
               
            
          
              
               Imgproc.cvtColor(gray, gray, Imgproc.COLOR_BGR2GRAY);
               System.out.println(&quot;1 Hough :&quot; +circles.size());
   float circle[] = new float[3];
  
               for (int i = 0; i &lt; circles.cols(); i++)
               {
                       circles.get(0, i, circle);
                   org.opencv.core.Point center = new org.opencv.core.Point();
                   center.x = circle[0];
                   center.y = circle[1];
                   Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                   }
               
               
               Imgproc.Canny( gray, gray, 200, 10, 3,false);  
               
               Imgproc.HoughCircles( gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 100, 80, 10, 10, 50 );
               System.out.println(&quot;2 Hough:&quot; +circles.size());
               
               for (int i = 0; i &lt; circles.cols(); i++)
               {
                       circles.get(0, i, circle);
                   org.opencv.core.Point center = new org.opencv.core.Point();
                   center.x = circle[0];
                   center.y = circle[1];
                   Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                   }
               Imgproc.Canny( gray, gray, 200, 10, 3,false);  
               
               Imgproc.HoughCircles( gray, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 100, 80, 10, 10, 50 );
               System.out.println(&quot;3 Hough&quot; +circles.size());
               
               //float circle[] = new float[3];
  
               for (int i = 0; i &lt; circles.cols(); i++)
               {
                       circles.get(0, i, circle);
                   org.opencv.core.Point center = new org.opencv.core.Point();
                   center.x = circle[0];
                   center.y = circle[1];
                   Core.circle(gray, center, (int) circle[2], new Scalar(255,255,100,1), 4);
                   }
  
              String hough = &quot;afterhough.png&quot;;
              Highgui.imwrite(hough, gray);
     }     
  }
  </code></pre>
  <p>How to make it more accurate?</p>