Wednesday, March 13, 2013

Android FaceFinder/Face Detector demo app

we can implement such kind of activities where the face finding is achievable on the images. in a demo application a girl image is used. for this purpose write a code project with name FaceFinder

1)com.examples.androface.Main.java


package com.examples.androface;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new FaceDetectionView(this, "girl.jpg"));
}
}

2)com.examples.androface.ImageLoader.java


/**
 * 
 */
package com.examples.androface;

import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


public class ImageLoader
  {
    public static final String tag = "ImageLoader";
    public static final String IMAGE_FILE_PATH = "file:///android_asset/";
    private Context _context = null;
    private static ImageLoader uniqueInstance = null;
    private final Bitmap imageBmp = null;

    public ImageLoader(Context context)
      {
        _context = context;
      }

    public static ImageLoader getInstance(Context context)
      {
        if (uniqueInstance == null)
          {
            uniqueInstance = new ImageLoader(context);
          }
        return uniqueInstance;
      }

    public Bitmap loadFromFile(String pathName)
      {
        Bitmap bitmap = null;
        try
          {
            String imgFilepath = pathName;
            Log.d(tag, "ImageFile: " + imgFilepath);
            bitmap = BitmapFactory.decodeStream(this._context.getResources().getAssets().open(imgFilepath));

          }
        catch (IOException e)
          {
            e.printStackTrace();
          }
        return bitmap;
      }

    public void loadFromURL(String url)
      {

      }

  }



3)com.examples.androface.FaceDetectionView.java


/**
 * 
 */
package com.examples.androface;

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 FaceDetectionView extends View
  {
    private static final String tag = FaceDetectionView.class.getName();
    private static final int NUM_FACES = 10;
    private FaceDetector arrayFaces;
    private final FaceDetector.Face getAllFaces[] = new FaceDetector.Face[NUM_FACES];
    private FaceDetector.Face getFace = null;

    private final PointF eyesMidPts[] = new PointF[NUM_FACES];
    private final float eyesDistance[] = new float[NUM_FACES];

    private Bitmap sourceImage;

    private final Paint tmpPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint pOuterBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint pInnerBullsEye = new Paint(Paint.ANTI_ALIAS_FLAG);

    private int picWidth, picHeight;
    private float xRatio, yRatio;
    private ImageLoader mImageLoader = null;

    public FaceDetectionView(Context context, String imagePath)
      {
        super(context);
        init();
        mImageLoader = ImageLoader.getInstance(context);
        sourceImage = mImageLoader.loadFromFile(imagePath);
        detectFaces();

      }

    private void init()
      {
        Log.d(tag, "Init()...");
        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;

      }

    private void loadImage(String imagePath)
      {
        sourceImage = mImageLoader.loadFromFile(imagePath);
      }

    @Override
    protected void onDraw(Canvas canvas)
      {
        Log.d(tag, "onDraw()...");

        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 < 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);
              }
          }
      }

    private void detectFaces()
      {
        Log.d(tag, "detectFaces()...");

        picWidth = sourceImage.getWidth();
        picHeight = sourceImage.getHeight();

        arrayFaces = new FaceDetector(picWidth, picHeight, NUM_FACES);
        arrayFaces.findFaces(sourceImage, getAllFaces);

        for (int i = 0; i < getAllFaces.length; i++)
          {
            getFace = getAllFaces[i];
            try
              {
                PointF eyesMP = new PointF();
                getFace.getMidPoint(eyesMP);
                eyesDistance[i] = getFace.eyesDistance();
                eyesMidPts[i] = eyesMP;

                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)
              {
                Log.e("Face", i + " is null");
              }
          }
      }
  }

4)manifest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.examples.androface"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity android:name=".FaceDetectionView"/>
    </application>
</manifest> 




Note: put girl.jpg inside assets folder



Download the code from FaceFinder

No comments:

Post a Comment