# Face Detection Image Crop

Automatically detect faces in your image and crop them. Sirv uses deep learning neural networks, trained on millions of images to accurately detect faces in images.

Using an AI coding agent?

Install the [`sirv-dynamic-imaging`](https://www.skills.sh/sirv/image-optimization-skill/sirv-dynamic-imaging) skill, and your agent can build these image URLs for you. Ask in plain words, such as “Serve our product photos as 800px WebP.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

Faces can be detected in as little as 200ms.

Try it

![Original image](https://demo.sirv.com/swing.jpg?w=800)

Original

![Image with the options applied](https://demo.sirv.com/swing.jpg?crop.type=face&crop.aspectratio=1:1)

With options

Crop type

facepoitrim

Aspect ratio

:

Crop width

px%

Crop height

px%

Padding and scale options4

Padding X

px%

Padding Y

px%

Scale width

px%

Scale height

px%

https://demo.sirv.com/swing.jpg**?crop.type=face&crop.aspectratio=1:1**

[](https://demo.sirv.com/swing.jpg?crop.type=face&crop.aspectratio=1:1)

## How to use face detection

Use the `crop.type` parameter to initiate face detection on an image. Specify `face` as the type of crop you want to apply, like so:

`https://demo.sirv.com/lady.jpg?crop.type=face`

![Original image, not cropped](https://demo.sirv.com/lady.jpg?h=280&text.text=Original&text.color=black&text.position=north&text.size=50)

![Face detection cropped image](https://demo.sirv.com/lady.jpg?ch=280&crop.type=face&text.text=Cropped&text.color=black&text.position=north&text.size=90)

To scale down the size of the face, use `ch` and/or `cw`:

`https://demo.sirv.com/lady.jpg?crop.type=face&ch=150`

![Face detected image, scaled to 150px](https://demo.sirv.com/lady.jpg?crop.type=face&ch=150)

## Crop to uniform size

To create a particular aspect ratio for images, add `crop.aspectratio=1:1`. The ratio is width:height, so 1:1 will make a square image. Use any value to choose another e.g. 16:9.

`https://demo.sirv.com/lady.jpg?crop.type=face&crop.aspectratio=1:1`

To scale the image to a particular width or height, use `crop.scale.width` and/or `crop.scale.height`:

`https://demo.sirv.com/lady.jpg?crop.type=face&crop.aspectratio=1:1&crop.scale.width=350`

All these images have `crop.aspectratio=1:1` and `crop.scale.height=180`:

![AI image face detection, cropped square to 180px](https://demo.sirv.com/research/faces/37.jpg?crop.type=face&crop.aspectratio=1:1&crop.scale.height=180)

![AI image face detection, cropped square to 180px](https://demo.sirv.com/research/faces/38.jpg?crop.type=face&crop.aspectratio=1:1&crop.scale.height=180)

![AI image face detection, cropped square to 180px](https://demo.sirv.com/research/faces/39.jpg?crop.type=face&crop.aspectratio=1:1&crop.scale.height=180)

![AI image face detection, cropped square to 180px](https://demo.sirv.com/research/faces/41.jpg?crop.type=face&crop.aspectratio=1:1&crop.scale.height=180)

## Face identification

To see which faces Sirv detected, add `crop.dev=1` to the URL. A red box will be drawn around the face:

`https://demo.sirv.com/lady.jpg?crop.type=face&crop.dev=1`

![AI image face detection with confidence value](https://demo.sirv.com/lady.jpg?crop.type=face&crop.dev=1&w=700&q=90)

## Face confidence

Sirv’s AI algorithm provides a level of confidence for each face detected. Values range from 0.00 to 1.00, where 1.00 is complete confidence.

By default, face crop will initiate if confidence is at least 0.50. This can be adjusted by setting `crop.confidence`.

To crop only faces that Sirv is very sure of, raise it, for example to 0.99:

`https://demo.sirv.com/lady.jpg?crop.type=face&crop.confidence=0.99`

If no face reaches the value you set, Sirv returns the whole image, uncropped.

## Multiple faces

If an image contains more than one face, the face with the highest confidence level will be shown first i.e. the face which the AI algorithm deems most likely a human face.

This image has 2 faces with confidence of 1.00 and one incorrectly identified face with confidence of 0.82:

![AI face detection on image with multiple people](/help/img/help/face-detection-multiple-faces.jpg)

To specify a particular face to crop, use `crop.face`.

Face numbering starts from 0. When multiple faces have the same confidence level, the first face identified will be 0. The following URLs will show the first 2 faces:

`https://demo.sirv.com/dance.jpg?crop.type=face&crop.face=0`

`https://demo.sirv.com/dance.jpg?crop.type=face&crop.face=1`

![First face detected](https://demo.sirv.com/dance.jpg?crop.type=face&crop.face=0)

First face

![Second face detected](https://demo.sirv.com/dance.jpg?crop.type=face&crop.face=1)

Second face

## Face coordinates

You can check how many faces were detected and what their coordinates are.

Face detection is triggered the first time `crop.type=face` is used, so to ensure that you receive face meta data, make a request e.g.

`https://demo.sirv.com/lady.jpg?crop.type=face`

Then you can request JSON meta data by appending `?info` in the image URL e.g.

`https://demo.sirv.com/lady.jpg?info`

Look for “faces” inside the “smartcrop” section of the JSON response:

```json
{
  "faces": {
    "_fileversion": 0,
    "_version": "mtcnn-1.0",
    "faces": [
      [
        0.44916666666666666,
        0.335,
        0.5266666666666666,
        0.47625,
        0.9999679327011108
      ],
      [
        0.4041666666666667,
        0.5725,
        0.4508333333333333,
        0.6725,
        0.9816775918006897
      ]
    ]
  }
}
```

The example above shows that 2 faces were detected (the second is the tattoo on the arm).

Each face has 5 numbers - the first 4 are coordinates for the corners of the box around the face and the 5th is the confidence score, where 0.00 is zero confidence and 1.00 is total confidence.
