by Rohit Arya

Face centering Android library build on top of Google Vision API

Results after cropping

In our Android apps, when we crop photos to display them, we often encounter the problem of positioning faces properly.

This inspired me to create a tool that will locate faces and in an image (if there are any) and center the cropped image around them.

Here’s how I did it.

I started with Face Detection API of Google’s mobile vision. This API provides:

  • Face detection (not face recognition)
  • Face tracking (extends face detection to video sequences)
  • A landmark — a point of interest within a face (like eyes, nose, etc)
  • Classification of a face to determine if the face is smiling, eyes are open or closed, and other features

Since I just wanted the position of the face, I only used the face detection component. To start with that, I created the face detector:

FaceDetector detector = new FaceDetector.Builder(context)

Now given a bitmap, I created a frame instance from the bitmap to supply to the detector:

Frame frame = new Frame.Builder().setBitmap(bitmap).build();

Now, I tried to detect faces synchronously in the frame:

SparseArray<Face> faces = detector.detect(frame);

Once I got faces, I chose one face (for now) to crop the image around, keeping that face in the center.

Now to begin cropping the image:

  • I created a scaled new bitmap to fit the target view (ImageView).
  • I then recalculated the position of the face in the new bitmap.
  • Keeping the face in the center, I cropped the original bitmap to get a new bitmap.
  • If no face is detected, then I fallback to CENTER CROP.

You can find the full code in my GitHub repositories below.

Here are some results of it:

Original image to be cropped.
Results after cropping
Original image
Results after cropping
Original Image
Results after cropping
Original Image
Results after cropping

I finally exported this as a library, which you can find below.

For Glide:

GlideFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…

For Picasso:

PicassoFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…

I am planning to release it for Fresco too.

Feel free to make use of this tool, and help me improve it over on GitHub.

If you enjoyed reading this article, it would mean a lot if you recommend it using the ❤ icon and share with your colleagues and friends. Thanks!