by Rohit Arya

Face centering Android library build on top of Google Vision API

HuCxf6ShYRhMd3Elu9nJR6VEDF9QTFS6rPPm
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)
    .setTrackingEnabled(false)
    .build();

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:

uaukFMMKsp3BvFT7JUNMHn4Cvg-X4VpvIhpK
Original image to be cropped.
-yG3bvIhkNPDtAtgmbgEEsJGXjxg0eKYF6JA
Results after cropping
Hms3iIRztMyLsd6gDHInDvSbhIIhrD2e2NLH
Original image
WLQx73hHnBlvLriXqHJPA1agUmEedBcTRIsh
Results after cropping
np-29WMZZdUWy6XQHjxZhAAa5EaQWtY8YBdP
Original Image
slny81MI3mfiuITJE--qFn5q2qZqkHNnOqoL
Results after cropping
Lg6EwpnyTdemmnG97sljtu-o2LyucFQPNB5y
Original Image
osDqcv3pEObAsIHEaMD8VaTwM0EwfuKzkL6b
Results after cropping

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

For Glide:

aryarohit07/GlideFaceDetectionTransformation
GlideFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…
github.com

For Picasso:

aryarohit07/PicassoFaceDetectionTransformation
PicassoFaceDetectionTransformation - An Android image transformation library providing cropping above Face Detection…
github.com

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!