Search code examples
javaandroidface-detection

Android Face Detection


I am trying to do face detection on android, and I am following the guide http://www.richardnichols.net/2011/01/java-facial-recognition-haar-cascade-with-jjil-guide/

but on android instead. When i do

Gray8DetectHaarMultiScale detectHaar = new Gray8DetectHaarMultiScale(is, minScale, maxScale);
RgbAvgGray toGray = new RgbAvgGray();
toGray.push(RgbImage);
detectHaar.pushAndReturn(toGray.getFront());

It seems that pushAndReturn is only returning one face from the image on Android although the exact code returns 2 faces using the netbeans code. The difference is only in the type of the image (RgbImage on android and RgbImageJ2se on netbeans)

I don't know what i m missing and why i can't detect more than one face on Android ?

I am using JJIL so i mean by RgbImage: jjil.core.RgbImage type, vs. RgbImageJ2SE type. The rest is just the same!! it seems that pushAndReturn is only returning one entry in the stack. This does not work on any image with more than one face.


Solution

  • Go for this its working and detecting all faces from a given picture

        public class AndroidFaceDetector extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.main);
            setContentView(new myView(this));
        }
    
        private class myView extends View{
    
         private int imageWidth, imageHeight;
         private int numberOfFace = 5;
         private FaceDetector myFaceDetect; 
         private FaceDetector.Face[] myFace;
         float myEyesDistance;
         int numberOfFaceDetected;
    
         Bitmap myBitmap;
    
    
        public myView(Context context) {
       super(context);
       // TODO Auto-generated constructor stub
    
       BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
       BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
       myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
          BitmapFactoryOptionsbfo);
       imageWidth = myBitmap.getWidth();
       imageHeight = myBitmap.getHeight();
       myFace = new FaceDetector.Face[numberOfFace];
       myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
       numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 
    
      }
    
      @Override
      protected void onDraw(Canvas canvas) {
       // TODO Auto-generated method stub
    
                canvas.drawBitmap(myBitmap, 0, 0, null);
    
                Paint myPaint = new Paint();
                myPaint.setColor(Color.GREEN);
                myPaint.setStyle(Paint.Style.STROKE); 
                myPaint.setStrokeWidth(3);
    
                for(int i=0; i < numberOfFaceDetected; i++)
                {
                 Face face = myFace[i];
                 PointF myMidPoint = new PointF();
                 face.getMidPoint(myMidPoint);
        myEyesDistance = face.eyesDistance();
                 canvas.drawRect(
                   (int)(myMidPoint.x - myEyesDistance),
                   (int)(myMidPoint.y - myEyesDistance),
                   (int)(myMidPoint.x + myEyesDistance),
                   (int)(myMidPoint.y + myEyesDistance),
                   myPaint);
                }
      }
        }
    }