title: Face Detection in Android
body: <p>I am trying to develop a sample face detection application in Android.  I have tried with the <code>FaceDetecor</code> class which is available in the Android SDK itself, but it doesn't provide the correct results.  I have a bitmap.  The library should analyze and should say whether face is available in the bitmap or not.  Please give me some suggestions. Hoping for better result.  I have tried the following <a href="http://www.google.co.in/imgres?q=men%20group%20images&amp;um=1&amp;hl=en&amp;sa=N&amp;tbm=isch&amp;tbnid=tkfZ0kvTmIG9xM%3a&amp;imgrefurl=http://greenfyre.wordpress.com/2009/11/21/cru-hack-time-to-hit-back-hard/&amp;docid=nSXV_GprtGSX1M&amp;imgurl=http://www.sosolid.co.uk/photos/So_Sold_Crew_men_group.jpg&amp;w=471&amp;h=360&amp;ei=H_uXT4zhOoKJrAfC34DEAQ&amp;zoom=1&amp;iact=rc&amp;dur=449&amp;sig=102755378228027067517&amp;page=2&amp;tbnh=136&amp;tbnw=178&amp;start=15&amp;ndsp=17&amp;ved=1t:429,r:2,s:15,i:109&amp;tx=91&amp;ty=87&amp;biw=1024&amp;bih=639" rel="nofollow">Reference Image</a> image for this image it says no faces.</p>
  
  <p><strong>Main class:</strong></p>
  
  <pre><code>import android.app.Activity;
  import android.os.Bundle;
  import android.widget.ImageView;
  
  public class DetectFace1Activity extends Activity {
      /** Called when the activity is first created. */
  
      ImageView image;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          image=(ImageView)findViewById(R.id.image);
          FaceView faceView = new FaceView(this);
          setContentView(faceView);
      }
  }    
  </code></pre>
  
  <p><strong>Face Detection Class :</strong> </p>
  
  <pre><code> import android.content.Context;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.graphics.Canvas;
   import android.graphics.Color;
   import android.graphics.Paint;
   import android.graphics.PointF;
   import android.graphics.Rect;
   import android.media.FaceDetector;
   import android.util.Log;
   import android.view.View;
  
   public class FaceView extends View {
   private static final int NUM_FACES = 10; // max is 64
   private static final boolean DEBUG = true;
  
   private FaceDetector arrayFaces;
   private FaceDetector.Face getAllFaces[] = new FaceDetector.Face[NUM_FACES];
   private FaceDetector.Face getFace = null;
  
   private PointF eyesMidPts[] = new PointF[NUM_FACES];
   private float  eyesDistance[] = new float[NUM_FACES];
  
   private Bitmap sourceImage;
  
   private Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
   private Paint pOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
   private Paint pInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
  
   private int picWidth, picHeight;
   private float xRatio, yRatio;
  
   public FaceView(Context context) {
        super(context);
  
        pInnerBullsEye.setStyle(Paint.Style.FILL);
        pInnerBullsEye.setColor(Color.RED);
  
        pOuterBullsEye.setStyle(Paint.Style.STROKE);
        pOuterBullsEye.setColor(Color.RED);
  
        tmpPaint.setStyle(Paint.Style.STROKE);
        tmpPaint.setTextAlign(Paint.Align.CENTER);
  
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inPreferredConfig = Bitmap.Config.RGB_565;
  
        sourceImage = BitmapFactory.decodeResource( getResources() ,R.drawable.black, bfo);
  
        picWidth = sourceImage.getWidth();
        picHeight = sourceImage.getHeight();
  
        arrayFaces = new FaceDetector( picWidth, picHeight, NUM_FACES );
        arrayFaces.findFaces(sourceImage, getAllFaces);
  
        for (int i = 0; i &lt; getAllFaces.length; i++)
        {
             getFace = getAllFaces[i];
             try {
                  PointF eyesMP = new PointF();
                  getFace.getMidPoint(eyesMP);
                  eyesDistance[i] = getFace.eyesDistance();
                  eyesMidPts[i] = eyesMP;
  
                  if (DEBUG)
                  {
                       Log.i("Face", i +  " " + getFace.confidence() + " " +     getFace.eyesDistance() + " " + "Pose: ("+ getFace.pose(FaceDetector.Face.EULER_X) + ","
  + getFace.pose(FaceDetector.Face.EULER_Y) + "," +     getFace.pose(FaceDetector.Face.EULER_Z) + ")" + "Eyes Midpoint: ("+eyesMidPts[i].x + "," + eyesMidPts[i].y +")");
                  }
             } catch (Exception e) {
                  if (DEBUG) Log.e("Face", i + " is null");
             }
  
        }
   }
  
   @Override
   protected void onDraw(Canvas canvas)
   {
        xRatio = getWidth()*1.0f / picWidth;
        yRatio = getHeight()*1.0f / picHeight;
        canvas.drawBitmap( sourceImage, null , new Rect(0,0,getWidth(),getHeight()),tmpPaint);
        for (int i = 0; i &lt; eyesMidPts.length; i++)
        {
             if (eyesMidPts[i] != null)
             {
                  pOuterBullsEye.setStrokeWidth(eyesDistance[i] /6);
                  canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 2 , pOuterBullsEye);
                  canvas.drawCircle(eyesMidPts[i].x*xRatio, eyesMidPts[i].y*yRatio, eyesDistance[i] / 6 , pInnerBullsEye);
             }
        }
   }
  </code></pre>