Face-detection based transformations

Uploadcare allows you to use face recognition algorithms to make transformations based on detected faces in an image.

In the most popular use cases, such as creating user pics, blurring faces and cropping based on detected faces, and overlaying text or special things on specified facial attributes, face-based transformations are used in combination with other image processing operations.

List of face-detection based processing operations

Face-detection based cropping

Cropping by faces

Crops the image to the object specified by the :tag parameter with :face value — the largest detected face in the image is used as a crop basis.

There are several ways to crop a file:

Crop by face

-/crop/face/3:3/

Crop to square
-/crop/1:1/50p,30p/
Crop by face with space
-/crop/face/200px200p/

Keeping proportions

If you want to keep the proportions but fill the entire tile, use -/smart_resize/ to generate missing parts of the picture.

Smart resize
-/smart_resize/250x250/

Create a user’s pic

If you want to create a circular user profile photo, combine the operations -/crop/face/ and -/border_radius/.

Smart resize
-/crop/face/200px200p/-/crop/1:1/50p,30p/-/border_radius/50p/

You can also create a circular user photo, combine the operations -/scale_crop/ and -/border_radius/.

Scale crop
-/scale_crop/440x440/smart/-/border_radius/50p/

Filling an empty area

If you have an empty space when creating a user’s pic, you can fill it using the -/setfill/:color/ operation.

You can find out the required fill color with the -/main_colors/ operation or through other external apps.

Original image

Original

Crop image
Crop image
Fill empty area
Fill empty area

Blur faces

If you need to blur faces in photos to comply with privacy laws, apply a blur effect to automatically detect faces in the image using the -/blur_region/faces/ operation.

You can adjust the strength of the blur effect. It determines with an optional additional strength value, ranging to 5000 (default: 10).

For example, -/blur_region/faces/100/ uses a small blur effect with a strength of 100:

Small blur effect with a strength=100
-/blur_region/faces/100/

You can “bake in” the applied changes and create a new file that can no longer be reverted to its original state.

Coordinates of facial landmarks

/:uuid/detect_faces/

The detect_faces operation returns the coordinates of faces found in an input image. The output is similar to the json operation. The output is a JSON with the additional list of faces that holds the coordinates of faces that were detected.

Data for each of the found faces are put into separate lists that look like this:

1[x, y, x_size, y_size]

Further, lists within faces contain:

  • x, y — coordinates of the upper-left corner of an area where a face was found.
  • x_size, y_size — dimensions of that area.

Note, detect_faces is not divided from a file UUID by the /-/ separator. Hence, it can not be piped to other operations.

Run a face-check for the following image on our CDN:

Compound image with three faces

Put detect_faces into the image URL, separating it with the forward slash / from the UUID:

1https://ucarecdn.com/5128ec65-9957-47b8-a6ad-4c2f172ef660/detect_faces/

Get the following faces list in the response JSON:

1"faces": [
2 [45, 142, 207, 207],
3 [460, 113, 238, 238],
4 [892, 43, 265, 265]
5]

detect_faces uses an algorithm that better detects the fronts of faces rather than facial profiles. Also, covering important face features with different objects leads to a decline in detection accuracy.

Technically, the operation detects faces using Haar Cascades. That approach deals with machine learning processes that rely on classifiers holding cascades of features specific to faces, eyes, etc.

Position overlays avoiding detected faces

You can combine text overlay and -/detect_faces/ operations to add text to an image while avoiding text overlapping with faces.

Overlaying text without affecting the face

-/font/50/0f1/-/text/320x150/320,650/This%20is%20a%20test%20text/
Overlaying text without affecting the face