title: OpenCV code for Face Detection(followed by eyes,nose & mouth detection) in android
body: <p>I'm a beginner to OpenCV. I'm trying to do a sample android application to detect mouth in a given image using OpenCV . I searched in the internet and I couldn't find a proper android or java code which satisfy my requirement. But I have C++ code. I dont know how to translate it.   </p>
  
  <p><a href="http://opencvfacedetect.blogspot.com/2010/10/face-detectionfollowed-by-eyesnose.html" rel="nofollow noreferrer">http://opencvfacedetect.blogspot.com/2010/10/face-detectionfollowed-by-eyesnose.html</a></p>
  
  <p>Can you please help me to find a proper java or android code. Or else please help me with translate this C++ code into java, which I can use inside android application.</p>
  
  <p>Thank you in advance.</p>
  
  <h2>C++ code</h2>
  
  <pre><code>/* program to detect facial features using opencv*/
  
  /* detectFacialfeatures() is the main function that detects the face followed by Eyes,nose and mouth detection.*/
  
  #include "stdafx.h"
      /**
       * Loads static images from database and detect faces
       */
      #include &lt;stdio.h&gt;
      #include&lt;conio.h&gt;
      #include "cv.h"
      #include "highgui.h"
      #include "cvaux.h"
  
      CvHaarClassifierCascade *cascade,*cascade_e,*cascade_nose,*cascade_mouth;
      CvMemStorage            *storage;
      char *face_cascade="haarcascade_frontalface_alt2.xml";
      char *eye_cascade="haarcascade_mcs_eyepair_big.xml";
      char *nose_cascade="haarcascade_mcs_nose.xml";
      char *mouth_cascade="haarcascade_mcs_mouth.xml";
  
      /*Mouth detect ion*/
      void detectMouth( IplImage *img,CvRect *r)
      {
         CvSeq *mouth;
         //mouth detecetion - set ROI
         cvSetImageROI(img,/* the source image */ 
                       cvRect(r-&gt;x,            /* x = start from leftmost */
                              r-&gt;y+(r-&gt;height *2/3), /* y = a few pixels from the top */
                              r-&gt;width,        /* width = same width with the face */
                              r-&gt;height/3    /* height = 1/3 of face height */
                             )
                      );
          mouth = cvHaarDetectObjects(img,/* the source image, with the estimated location defined */ 
                                      cascade_mouth,      /* the eye classifier */ 
                                      storage,        /* memory buffer */
                                      1.15, 4, 0,     /* tune for your app */ 
                                      cvSize(25, 15)  /* minimum detection scale */
                                     );
  
              for( int i = 0; i &lt; (mouth ? mouth-&gt;total : 0); i++ )
              {
  
                CvRect *mouth_cord = (CvRect*)cvGetSeqElem(mouth, i);
                /* draw a red rectangle */
                cvRectangle(img, 
                            cvPoint(mouth_cord-&gt;x, mouth_cord-&gt;y), 
                            cvPoint(mouth_cord-&gt;x + mouth_cord-&gt;width, mouth_cord-&gt;y + mouth_cord-&gt;height),
                            CV_RGB(255,255, 255), 
                            1, 8, 0
                          );
              }
           //end mouth detection
  
      }
  
      /*Nose detection*/
      void detectNose( IplImage *img,CvRect *r)
      {
        CvSeq *nose;
  
        //nose detection- set ROI
        cvSetImageROI(img,                    /* the source image */ 
                      cvRect(r-&gt;x,            /* x = start from leftmost */
                             r-&gt;y , /* y = a few pixels from the top */
                             r-&gt;width,        /* width = same width with the face */
                             r-&gt;height  /* height = 1/3 of face height */
                            )
                     );
  
  
        nose = cvHaarDetectObjects(img, /* the source image, with the estimated location defined */ 
                                   cascade_nose,      /* the eye classifier */ 
                                   storage,        /* memory buffer */
                                   1.15, 3, 0,     /* tune for your app */ 
                                   cvSize(25, 15)  /* minimum detection scale */
                                  );
  
        for( int i = 0; i &lt; (nose ? nose-&gt;total : 0); i++ )
            {
                CvRect *nose_cord = (CvRect*)cvGetSeqElem(nose, i);
  
                /* draw a red rectangle */
                cvRectangle(img, 
                            cvPoint(nose_cord-&gt;x, nose_cord-&gt;y), 
                            cvPoint(nose_cord-&gt;x + nose_cord-&gt;width, nose_cord-&gt;y + nose_cord-&gt;height),
                            CV_RGB(0,255, 0), 
                            1, 8, 0
                          );
  
            }
      }
  
      /*Eyes detection*/
      void detectEyes( IplImage *img,CvRect *r)
      {
          char *eyecascade;
          CvSeq *eyes;
          int eye_detect=0;
  
  
         //eye detection starts
        /* Set the Region of Interest: estimate the eyes' position */
  
          cvSetImageROI(img,                    /* the source image */ 
                cvRect
                (
                    r-&gt;x,            /* x = start from leftmost */
                    r-&gt;y + (r-&gt;height/5.5), /* y = a few pixels from the top */
                    r-&gt;width,        /* width = same width with the face */
                    r-&gt;height/3.0    /* height = 1/3 of face height */
                )
            );
  
            /* detect the eyes */
            eyes = cvHaarDetectObjects( img,            /* the source image, with the estimated location defined */ 
                                        cascade_e,      /* the eye classifier */ 
                                        storage,        /* memory buffer */
                                        1.15, 3, 0,     /* tune for your app */ 
                                        cvSize(25, 15)  /* minimum detection scale */
                                      );
  
            printf("\no of eyes detected are %d",eyes-&gt;total);
  
  
              /* draw a rectangle for each detected eye */
              for( int i = 0; i &lt; (eyes ? eyes-&gt;total : 0); i++ )
                {
                    eye_detect++;
                    /* get one eye */
                    CvRect *eye = (CvRect*)cvGetSeqElem(eyes, i);
                    /* draw a red rectangle */
                              cvRectangle(img, 
                                          cvPoint(eye-&gt;x, eye-&gt;y), 
                                          cvPoint(eye-&gt;x + eye-&gt;width, eye-&gt;y + eye-&gt;height),
                                          CV_RGB(0, 0, 255), 
                                          1, 8, 0
                                         );
                 }
  
  
      }
      void detectFacialFeatures( IplImage *img,IplImage *temp_img,int img_no)
      {
  
          char image[100],msg[100],temp_image[100];
          float m[6];
          double factor = 1;
          CvMat M = cvMat( 2, 3, CV_32F, m );
          int w = (img)-&gt;width;
          int h = (img)-&gt;height;
          CvSeq* faces;
          CvRect *r;
  
          m[0] = (float)(factor*cos(0.0));
          m[1] = (float)(factor*sin(0.0));
          m[2] = w*0.5f;
          m[3] = -m[1];
          m[4] = m[0];
          m[5] = h*0.5f;
  
          cvGetQuadrangleSubPix(img, temp_img, &amp;M);
          CvMemStorage* storage=cvCreateMemStorage(0);
          cvClearMemStorage( storage );
  
          if( cascade )
              faces = cvHaarDetectObjects(img,cascade, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(20, 20));
          else
              printf("\nFrontal face cascade not loaded\n");
  
          printf("\n no of faces detected are %d",faces-&gt;total);
  
  
          /* for each face found, draw a red box */
          for(int i = 0 ; i &lt; ( faces ? faces-&gt;total : 0 ) ; i++ )
          {        
              r = ( CvRect* )cvGetSeqElem( faces, i );
              cvRectangle( img,cvPoint( r-&gt;x, r-&gt;y ),cvPoint( r-&gt;x + r-&gt;width, r-&gt;y + r-&gt;height ),
                           CV_RGB( 255, 0, 0 ), 1, 8, 0 );    
  
              printf("\n face_x=%d face_y=%d wd=%d ht=%d",r-&gt;x,r-&gt;y,r-&gt;width,r-&gt;height);
  
              detectEyes(img,r);
              /* reset region of interest */
              cvResetImageROI(img);
              detectNose(img,r);
              cvResetImageROI(img);
              detectMouth(img,r);
              cvResetImageROI(img);
          }
          /* reset region of interest */
            cvResetImageROI(img);
  
            if(faces-&gt;total&gt;0)
              {
                  sprintf(image,"D:\\face_output\\%d.jpg",img_no);
                  cvSaveImage( image, img );
              }
      }
  
      int main( int argc, char** argv )
      {
          CvCapture *capture;
          IplImage  *img,*temp_img;
          int       key;
  
          char image[100],temp_image[100];
  
  
          /* load the classifier 
             note that I put the file in the same directory with
             this code */
          storage = cvCreateMemStorage( 0 );
              cascade = ( CvHaarClassifierCascade* )cvLoad( face_cascade, 0, 0, 0 );
          cascade_e = ( CvHaarClassifierCascade* )cvLoad( eye_cascade, 0, 0, 0 );
          cascade_nose = ( CvHaarClassifierCascade* )cvLoad( nose_cascade, 0, 0, 0 );
          cascade_mouth = ( CvHaarClassifierCascade* )cvLoad( mouth_cascade, 0, 0, 0 );
  
  
  
          if( !(cascade || cascade_e ||cascade_nose||cascade_mouth) )
              {
              fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
              return -1;
              }
  
          for(int j=20;j&lt;27;j++)
          {
  
               sprintf(image,"D:\\ptz images\\%d.jpg",j);
  
              img=cvLoadImage(image);
              temp_img=cvLoadImage(image);
  
              if(!img)
              {
              printf("Could not load image file and trying once again: %s\n",image);
              }
              printf("\n curr_image = %s",image);
  
              detectFacialFeatures(img,temp_img,j);
          }
  
  
          cvReleaseHaarClassifierCascade( &amp;cascade );
          cvReleaseHaarClassifierCascade( &amp;cascade_e );
  
          cvReleaseHaarClassifierCascade( &amp;cascade_nose );
          cvReleaseHaarClassifierCascade( &amp;cascade_mouth );
          cvReleaseMemStorage( &amp;storage );
  
           cvReleaseImage(&amp;img);
           cvReleaseImage(&amp;temp_img);
  
  
          return 0;
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Y1zJ1.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y1zJ1.jpg" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/U2PQN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U2PQN.jpg" alt="enter image description here"></a></p>