I'm want to create a basic app that can detect faces. What SDK method or Opensource options could I use for this if someone can help me with detecting faces I'd appreciate it.
Asked
Active
Viewed 250 times
1
-
Please show what you have tried so far. Thanks! – Blue Ice Mar 18 '14 at 23:11
1 Answers
2
Hi Susan this is a basic example using FaceDetector.Face Class:
public class FaceDetection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
myBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.jorgesys, 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) {
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 * 2),
(int) (myMidPoint.y - myEyesDistance * 2),
(int) (myMidPoint.x + myEyesDistance * 2),
(int) (myMidPoint.y + myEyesDistance * 2), myPaint);
}
}
}
}
Inside your /drawable
folder add a an image with several faces to test, this example use an image called jorgesys.png (R.drawable.jorgesys)
-
For FaceDetection API, it seems to detect only the middle of the eyes and distance between them, but is it possible to get to individual eyes? Or is it possible to know that the face is slanted? – Lim Thye Chean Jul 26 '14 at 11:30