opencv – Face Detection in Android?-ThrowExceptions

Exception or error:

I am currently working on an experimental camera app. I’m looking into implementing face detection at the moment and am currently weighing up my options.

I have considered the OpenCV port available for Android and using their face detection functions, but from demos I have seen of previous implementations, the camera seems to lag a lot.

Considering the camera on the HTC Desire has face detection support, I know it must be possible to get at least a semi-decent face detection system in place. I was just wondering if anyone had an opinion on how I could get the best results… Using an available library? Implementing a particular algorithm myself?

How to solve:
  1. Try FaceDetector in the Android SDK. It returns face positions and angles in BMPs. But it’s not very fast.

    Here’s a realtime face detection sample using FaceDetector and OpenGL (draws rectangles) which works in Android 2.2.

  2. OpenCV in Android

    You’d better try this on Linux (I’ve tried it on Windows, but failed).

  3. JavaCV (strongly recommended)

    There is a sample code of realtime face detection using the camera. See “javacv-src-*.zip” on the download page.

###

The Android SDK comes with a FaceDetector which can be used to find faces in a given Bitmap.

I haven’t used it myself, but developers of facial distortion apps say it is not very accurate compared to OpenCV. It may fit your needs though, it’s probably what the HTC Desire’s camera app uses.

###

This is a basic example using FaceDetector class

public myView(Context context) {
        super(context);
        BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
        BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
        myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.threepoint_shooters_1990, 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);
        }
    }

enter image description here

Download the full source code here:
https://github.com/Jorgesys/Android_Face_Detection

Leave a Reply

Your email address will not be published. Required fields are marked *