title: Facial recognition using java cv
body: <p>I'm trying to create facial recognition application using open cv in java but I have only managed to invoke the web cam and perform face detection. I haven't been able to perform facial recognition i.e. comparing faces in the database with the captured face in webcam. Here is the code:</p>
  
  <pre><code>package com.gordon.trial;
  
  import java.awt.*;  
  
  import java.awt.image.BufferedImage;  
  import java.io.ByteArrayInputStream;  
  import java.io.IOException;  
  import javax.imageio.ImageIO;  
  import javax.swing.*;  
  import org.opencv.core.Core;  
  import org.opencv.core.Mat;  
  import org.opencv.core.MatOfByte;  
  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.highgui.VideoCapture;  
  import org.opencv.imgproc.Imgproc;  
  import org.opencv.objdetect.CascadeClassifier;  
  
  class FacePanel extends JPanel{  
       private static final long serialVersionUID = 1L;  
       private BufferedImage image;  
       // Create a constructor method  
       public FacePanel(){  
            super();   
       }  
       /*  
        * Converts/writes a Mat into a BufferedImage.  
        *   
        * @param matrix Mat of type CV_8UC3 or CV_8UC1  
        * @return BufferedImage of type TYPE_3BYTE_BGR or TYPE_BYTE_GRAY  
        */       
       public boolean matToBufferedImage(Mat matrix) {  
            MatOfByte mb=new MatOfByte();  
            Highgui.imencode(".jpg", matrix, mb);  
            try {  
                 this.image = ImageIO.read(new ByteArrayInputStream(mb.toArray()));  
            } catch (IOException e) {  
                 e.printStackTrace();  
                 return false; // Error  
            }  
         return true; // Successful  
       }  
       public void paintComponent(Graphics g){  
            super.paintComponent(g);   
            if (this.image==null) return;         
             g.drawImage(this.image,10,10,this.image.getWidth(),this.image.getHeight(), null);
       }
  
  }  
  class FaceDetector {  
       private CascadeClassifier face_cascade;  
       // Create a constructor method  
       public FaceDetector(){  
           // face_cascade=new CascadeClassifier("./cascades/lbpcascade_frontalface_alt.xml");  
  
           //..didn't have not much luck with the lbp
  
  //./cascades/haarcascade_frontalface_alt.xml
          face_cascade=new CascadeClassifier("C://opencv//build//java//haarcascade_frontalface_alt.xml"); 
            if(face_cascade.empty())  
            {  
                 System.out.println("--(!)Error loading A\n");  
                  return;  
            }  
            else  
            {  
                       System.out.println("Face classifier loooaaaaaded up");  
            }  
       }  
       public Mat detect(Mat inputframe){  
            Mat mRgba=new Mat();  
            Mat mGrey=new Mat();  
            MatOfRect faces = new MatOfRect();  
            inputframe.copyTo(mRgba);  
            inputframe.copyTo(mGrey);  
            Imgproc.cvtColor( mRgba, mGrey, Imgproc.COLOR_BGR2GRAY);  
            Imgproc.equalizeHist( mGrey, mGrey );  
            face_cascade.detectMultiScale(mGrey, faces);  
            System.out.println(String.format("Detected %s faces", faces.toArray().length));  
            for(Rect rect:faces.toArray())  
            {  
                 Point center= new Point(rect.x + rect.width*0.5, rect.y + rect.height*0.5 );  
                 //draw a blue eclipse around face
  
               //  Size s = new Size( rect.width*0.5, rect.height*0.5), 0, 0, 360, new Scalar( 0, 0, 255 )
  
                 // Core.ellipse( mRgba, center,s , 4, 8, 0 );  
            }  
            return mRgba;  
       }  
  }  
  </code></pre>
  
  <p>This is the main class:</p>
  
  <pre><code>package com.gordon.trial;
  
  import java.awt.BorderLayout;
  import java.awt.Color;
  
  import javax.swing.JFrame;
  
  import org.opencv.core.Core;
  import org.opencv.core.Mat;
  import org.opencv.highgui.VideoCapture;
  
  public class Main {
      public static void main(String arg[]) throws InterruptedException{  
            // Load the native library.  
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
            //or ...     System.loadLibrary("opencv_java244");       
  
            //make the JFrame
            JFrame frame = new JFrame("WebCam Capture - Face detection");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  
            FaceDetector faceDetector=new FaceDetector();  
            FacePanel facePanel = new FacePanel();  
            frame.setSize(400,400); //give the frame some arbitrary size 
            frame.setBackground(Color.BLUE);
            frame.add(facePanel,BorderLayout.CENTER);       
            frame.setVisible(true);       
  
            //Open and Read from the video stream  
             Mat webcam_image=new Mat();  
             VideoCapture webCam =new VideoCapture(0);   
  
              if( webCam.isOpened())  
                {  
                 Thread.sleep(500); /// This one-time delay allows the Webcam to initialize itself  
                 while( true )  
                 {  
                   webCam.read(webcam_image);  
                   if( !webcam_image.empty() )  
                    {   
                        Thread.sleep(200); /// This delay eases the computational load .. with little performance leakage
                         frame.setSize(webcam_image.width()+40,webcam_image.height()+60);  
                         //Apply the classifier to the captured image  
                         webcam_image=faceDetector.detect(webcam_image);  
                        //Display the image  
                         facePanel.matToBufferedImage(webcam_image);  
                         facePanel.repaint();   
                    }  
                    else  
                    {   
                         System.out.println(" --(!) No captured frame from webcam !");   
                         break;   
                    }  
                   }  
                  }
                 webCam.release(); //release the webcam
  
            } //end main 
  }
  </code></pre>