title: Faster face detection code using OpenCV Java
body: <p>For my university project work, I have written a code to detect faces from the image and crop the faces into a differnt folder using OpenCV Java. In the code I have used "CascadeClassifier". Using this code I have detected the faces. However, is it possible improve the code, so that it is possible to detect the code faster. For example, it took 1.1520934 second to detect 5 faces from 5.jpg (resolution 1280*720). My pc has intel core i-5 @ 2.20 GHz. Is it possible to detect the face faster? 
  <strong>Opencv code</strong></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.imgproc.Imgproc;
  import org.opencv.objdetect.CascadeClassifier;
  
  public class DetectFaceDemo 
  {
  
      public void run() 
      {
          // TODO Auto-generated method stub
          System.out.println("\nRunning DetectFaceDemo");
  
          // Create a face detector from the cascade file in the resources
          // directory.
          long startTime = System.nanoTime();
      //    CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml");
          CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
  //       CascadeClassifier faceDetector = new CascadeClassifier("D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml");
  
  
  //      Mat image = Highgui.imread("d:\\face image\\aaa05.jpg");
          Mat image = Highgui.imread("D:\\photo test gallery_HD_2\\5.jpg");
         float size = Math.round(image.height() * 0.1f);
         //System.out.println("####= "+ size); 
          Rect rectCrop = null;
          // Detect faces in the image.
          // MatOfRect is a special container class for Rect.
          MatOfRect faceDetections = new MatOfRect();
          faceDetector.detectMultiScale(image, faceDetections);
  //      faceDetector.detectMultiScale(image, faceDetections, 1.1, 3, 0, new Size(90, 90),new Size(400, 400));
  
  
          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, 0, 255));
  
              rectCrop = new Rect(rect.x, rect.y, rect.width, rect.height);  
  
              Core.putText(image, "Edited by me", new Point(rect.x,rect.y),
                      Core.FONT_HERSHEY_PLAIN, 1.0 ,new  Scalar(0,255,255));
          }
  
  
          // crop individual image
          int counter=0;
          for (Rect rect : faceDetections.toArray())
          {
              Mat faceImage = image.submat(rect);
              Mat resizeimage = new Mat();
              Size sz = new Size(64, 128);
              Imgproc.resize(faceImage, resizeimage, sz);
              System.out.println(counter);
              Highgui.imwrite("D:\\image\\appface\\"+ counter+ ".jpg", resizeimage);
              counter++;
              System.out.println(";)");
  
          }
  
          //long stopTime = System.nanoTime();
          //float time_diff = stopTime - startTime;
          //System.out.println("timefacedetect-s:"+ time_diff/1000000000);
  
          // Save the visualized detection.
          String filename = "d:\\faceDetection.jpg";
          System.out.println(String.format("Writing %s", filename));
          Highgui.imwrite(filename, image);
  
      }
  
  }
  </code></pre>
  
  <p>Sample image file:
  <a href="https://drive.google.com/open?id=1kQHyF3E-pYMy1IESn8yzRs_6J4fZq_zi" rel="nofollow noreferrer">https://drive.google.com/open?id=1kQHyF3E-pYMy1IESn8yzRs_6J4fZq_zi</a></p>