Serverless Image Handler
Serverless Image Handler

Appendix C: Smart Cropping with Amazon Rekognition

The Serverless Image Handler solution leverages Amazon Rekognition for face detection in images submitted for smart cropping.

Image Request Use

To enable smart cropping on an image, add the smartCrop property to the edits property in the image request.

  • smartCrop: (optional, Boolean || Object) enables the smart cropping feature for an original image. If the value is true, the feature will return the first face detected from the original image with no additional options.

    const imageRequest = JSON.stringify({ bucket: “myImageBucket” key: “myImage.jpg”, edits: { smartCrop: true } })
  • smartCrop.faceIndex: (optional, Number) specifies which face to focus on if multiple are present within an original image. Detected faces are indexed in a zero-based array from the largest detected face to the smallest. If this value is not specified, Amazon Rekognition will return the largest face detected from the original image.

  • smartCrop.padding: (optional, Number) specifies an amount of padding in pixels to add around the cropped image. The padding value is applied to all sides of the cropped image. Additionally, the extend properties of the image handler can be used to apply more specific padding adjustments to the cropped image, as shown in the example below.

    const imageRequest = JSON.stringify({ bucket: “myImageBucket” key: “myImage.jpg”, edits: { smartCrop: { faceIndex: 1 // zero-based index of detected faces padding: 40 // padding expressed in pixels, applied to all sides } } })

On this page: