Tuesday 25 October 2011

Android Face Detection Example

In Android Sdk provide rich class called android.media.FaceDetector to identify the faces of people in a Bitmap graphic object.
In this below example we can see using FaceDetector how i find any people faces in Bitmap image .

Here below class i am using FaceDetector i can apply one rectangle shape to face.

Here i am using one image(aishwarya.png) that can be converted into Bitmap image
and Apply FaceDetection view on Image.and set that image to Activity View.

Note: Dont forget to place (aishwarya.png) in drawable folder.you can use your own image insted of (aishwarya.png) no problem .but u should you should use (yourpngfilename.png) insted of (aishwarya.png) in the below code

Android Activity(FaceDetectorActivity.java)

package com.androidsurya.facemodule;

import android.app.Activity;
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.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

public class FaceDetectorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new faceView(this));
}

private class faceView extends View {
// numberOfFace is for how many faces you want to find
private int numberOfFace = 1;
private FaceDetector faceDectect;
private FaceDetector.Face[] faceObj;
float eyesDistance;
int numberOfFacesDetected;
Bitmap bitmapImag;

public faceView(Context context) {
super(context);
// TODO Auto-generated constructor stub

BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
bitmapImag = BitmapFactory.decodeResource(getResources(),
R.drawable.aishwarya, BitmapFactoryOptionsbfo);
faceObj = new FaceDetector.Face[numberOfFace];
faceDectect = new FaceDetector(bitmapImag.getWidth(),
bitmapImag.getHeight(), numberOfFace);
numberOfFacesDetected = faceDectect.findFaces(bitmapImag, faceObj);

}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub

canvas.drawBitmap(bitmapImag, 0, 0, null);

Paint myPaint = new Paint();
myPaint.setColor(Color.YELLOW);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(2);

for (int i = 0; i < numberOfFacesDetected; i++) {
Face face = faceObj[i];
PointF myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
eyesDistance = face.eyesDistance();
canvas.drawRect((int) (myMidPoint.x - eyesDistance),
(int) (myMidPoint.y - eyesDistance),
(int) (myMidPoint.x + eyesDistance),
(int) (myMidPoint.y + eyesDistance), myPaint);
}
}
}
}

Register Android Activity in Android Manifest File

<activity
            android:name="com.androidsurya.facemodule.FaceDetectorActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Output ScreenShot
















No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...