Overview:
The
difference between the Face Detection & Face Recognization :
1) Face
detection
:-
Find
the face in the photo
2)
Face
recognition
:-
Identify
the face in camera
- FaceDetection is support by the android from the API level 1.
The android.media.FaceDetector class, configured with the size of the images to be
analysed and the maximum number of faces that can be detected.
These
parameters cannot be changed once the object is constructed. Note
that the width of the image must be even.
In real life you would probably want to capture the image using the camera, or choose the image from a Gallery to detect face in it.
Below
is the Code implementation of the Face Detection in android:
1)
Create a class FaceDetectionActivity.java
In
your onCreate() method write setContentView(new MyView(this)); after
call to super.
So our next step is to create MyView Class.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
So our next step is to create MyView Class.
public MyView(Context context)
{
super(context);
BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
myBitmap=BitmapFactory.decodeResource(getResources(),
R.drawable.faceswapping,bitmapFatoryOptions);
width=myBitmap.getWidth();
height=myBitmap.getHeight();
detectedFaces=new FaceDetector.Face[NUMBER_OF_FACES];
faceDetector=new FaceDetector(width,height,NUMBER_OF_FACES);
NUMBER_OF_FACE_DETECTED=faceDetector.findFaces(myBitmap, detectedFaces);
}
- For FaceDetection we need to convert in bitmap format that too in RGB_565.
- Now get the image from the drawable folder. Get the width and height of image.
- Now the reason I feel this API the simplest is coming now.
- You need to pass the number of faces you want to detect.It will return the array of Face type.Last three lines is having logic for that.So you must declare an array with the size of number of faces you want to detect.
Now
when the face gets detected we will draw a red rectangle on it.For
that we need to write few lines in our onDraw() method.
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(myBitmap, 0,0, null);
Paint myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++) {
Face face=detectedFaces[count];
PointF midPoint=new PointF();
face.getMidPoint(midPoint);
eyeDistance=face.eyesDistance();
canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);
}
}
drawRect
is taking five parameter left x,y and top x,y coordinate.From that
given pint it will start drawing rectangle.We need to pass paint
object also.
Here is the Full source of FacedetectionActivity.java.
public class FaceDetectionActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
/**
* Class which render the layout of the image and detect the face from the image.
*/
private class MyView extends View {
private Bitmap m_Bitmap;
private int m_width, m_height;
private FaceDetector.Face[] m_detectedFaces;
private int NUMBER_OF_FACES = 6;
private FaceDetector m_faceDetector;
private int NUMBER_OF_FACE_DETECTED;
private float m_eyeDistance;
public MyView(Context p_context) {
super(p_context);
BitmapFactory.Options m_bitmapFatoryOptions = new BitmapFactory.Options();
m_bitmapFatoryOptions.inPreferredConfig = Bitmap.Config.RGB_565;
m_Bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.faceswapping, m_bitmapFatoryOptions);
m_width = m_Bitmap.getWidth();
m_height = m_Bitmap.getHeight();
m_detectedFaces = new FaceDetector.Face[NUMBER_OF_FACES];
m_faceDetector = new FaceDetector(m_width, m_height, NUMBER_OF_FACES);
NUMBER_OF_FACE_DETECTED = m_faceDetector.findFaces(m_Bitmap,m_detectedFaces);
}
@Override
protected void onDraw(Canvas p_canvas)
{
p_canvas.drawBitmap(m_Bitmap, 0, 0, null);
Paint m_Paint = new Paint();
m_Paint.setColor(Color.GREEN);
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeWidth(3);
for (int m_count = 0; m_count < NUMBER_OF_FACE_DETECTED; m_count++)
{
Face m_face = m_detectedFaces[m_count];
PointF m_midPoint = new PointF();
m_face.getMidPoint(m_midPoint);
m_eyeDistance = m_face.eyesDistance();
p_canvas.drawRect(m_midPoint.x - m_eyeDistance, m_midPoint.y- m_eyeDistance, m_midPoint.x + m_eyeDistance, m_midPoint.y+ m_eyeDistance, m_Paint);
}
}
}
}
For more detailed implementation check out HERE
Thanks.
Enjoy.
No comments:
Post a Comment