Rekognition / Client / search_faces_by_image

search_faces_by_image#

Rekognition.Client.search_faces_by_image(**kwargs)#

For a given input image, first detects the largest face in the image, and then searches the specified collection for matching faces. The operation compares the features of the input face with faces in the specified collection.

Note

To search for all faces in an input image, you might first call the IndexFaces operation, and then use the face IDs returned in subsequent calls to the SearchFaces operation.

You can also call the DetectFaces operation and use the bounding boxes in the response to make face crops, which then you can pass in to the SearchFacesByImage operation.

You pass the input image either as base64-encoded image bytes or as a reference to an image in an Amazon S3 bucket. If you use the AWS CLI to call Amazon Rekognition operations, passing image bytes is not supported. The image must be either a PNG or JPEG formatted file.

The response returns an array of faces that match, ordered by similarity score with the highest similarity first. More specifically, it is an array of metadata for each face match found. Along with the metadata, the response also includes a similarity indicating how similar the face is to the input face. In the response, the operation also returns the bounding box (and a confidence level that the bounding box contains a face) of the face that Amazon Rekognition used for the input image.

If no faces are detected in the input image, SearchFacesByImage returns an InvalidParameterException error.

For an example, Searching for a Face Using an Image in the Amazon Rekognition Developer Guide.

The QualityFilter input parameter allows you to filter out detected faces that don’t meet a required quality bar. The quality bar is based on a variety of common use cases. Use QualityFilter to set the quality bar for filtering by specifying LOW, MEDIUM, or HIGH. If you do not want to filter detected faces, specify NONE. The default value is NONE.

Note

To use quality filtering, you need a collection associated with version 3 of the face model or higher. To get the version of the face model associated with a collection, call DescribeCollection.

This operation requires permissions to perform the rekognition:SearchFacesByImage action.

See also: AWS API Documentation

Request Syntax

response = client.search_faces_by_image(
    CollectionId='string',
    Image={
        'Bytes': b'bytes',
        'S3Object': {
            'Bucket': 'string',
            'Name': 'string',
            'Version': 'string'
        }
    },
    MaxFaces=123,
    FaceMatchThreshold=...,
    QualityFilter='NONE'|'AUTO'|'LOW'|'MEDIUM'|'HIGH'
)
Parameters:
  • CollectionId (string) –

    [REQUIRED]

    ID of the collection to search.

  • Image (dict) –

    [REQUIRED]

    The input image as base64-encoded bytes or an S3 object. If you use the AWS CLI to call Amazon Rekognition operations, passing base64-encoded image bytes is not supported.

    If you are using an AWS SDK to call Amazon Rekognition, you might not need to base64-encode image bytes passed using the Bytes field. For more information, see Images in the Amazon Rekognition developer guide.

    • Bytes (bytes) –

      Blob of image bytes up to 5 MBs. Note that the maximum image size you can pass to DetectCustomLabels is 4MB.

    • S3Object (dict) –

      Identifies an S3 object as the image source.

      • Bucket (string) –

        Name of the S3 bucket.

      • Name (string) –

        S3 object key name.

      • Version (string) –

        If the bucket is versioning enabled, you can specify the object version.

  • MaxFaces (integer) – Maximum number of faces to return. The operation returns the maximum number of faces with the highest confidence in the match.

  • FaceMatchThreshold (float) – (Optional) Specifies the minimum confidence in the face match to return. For example, don’t return any matches where confidence in matches is less than 70%. The default value is 80%.

  • QualityFilter (string) –

    A filter that specifies a quality bar for how much filtering is done to identify faces. Filtered faces aren’t searched for in the collection. If you specify AUTO, Amazon Rekognition chooses the quality bar. If you specify LOW, MEDIUM, or HIGH, filtering removes all faces that don’t meet the chosen quality bar. The quality bar is based on a variety of common use cases. Low-quality detections can occur for a number of reasons. Some examples are an object that’s misidentified as a face, a face that’s too blurry, or a face with a pose that’s too extreme to use. If you specify NONE, no filtering is performed. The default value is NONE.

    To use quality filtering, the collection you are using must be associated with version 3 of the face model or higher.

Return type:

dict

Returns:

Response Syntax

{
    'SearchedFaceBoundingBox': {
        'Width': ...,
        'Height': ...,
        'Left': ...,
        'Top': ...
    },
    'SearchedFaceConfidence': ...,
    'FaceMatches': [
        {
            'Similarity': ...,
            'Face': {
                'FaceId': 'string',
                'BoundingBox': {
                    'Width': ...,
                    'Height': ...,
                    'Left': ...,
                    'Top': ...
                },
                'ImageId': 'string',
                'ExternalImageId': 'string',
                'Confidence': ...,
                'IndexFacesModelVersion': 'string'
            }
        },
    ],
    'FaceModelVersion': 'string'
}

Response Structure

  • (dict) –

    • SearchedFaceBoundingBox (dict) –

      The bounding box around the face in the input image that Amazon Rekognition used for the search.

      • Width (float) –

        Width of the bounding box as a ratio of the overall image width.

      • Height (float) –

        Height of the bounding box as a ratio of the overall image height.

      • Left (float) –

        Left coordinate of the bounding box as a ratio of overall image width.

      • Top (float) –

        Top coordinate of the bounding box as a ratio of overall image height.

    • SearchedFaceConfidence (float) –

      The level of confidence that the searchedFaceBoundingBox, contains a face.

    • FaceMatches (list) –

      An array of faces that match the input face, along with the confidence in the match.

      • (dict) –

        Provides face metadata. In addition, it also provides the confidence in the match of this face with the input face.

        • Similarity (float) –

          Confidence in the match of this face with the input face.

        • Face (dict) –

          Describes the face properties such as the bounding box, face ID, image ID of the source image, and external image ID that you assigned.

          • FaceId (string) –

            Unique identifier that Amazon Rekognition assigns to the face.

          • BoundingBox (dict) –

            Bounding box of the face.

            • Width (float) –

              Width of the bounding box as a ratio of the overall image width.

            • Height (float) –

              Height of the bounding box as a ratio of the overall image height.

            • Left (float) –

              Left coordinate of the bounding box as a ratio of overall image width.

            • Top (float) –

              Top coordinate of the bounding box as a ratio of overall image height.

          • ImageId (string) –

            Unique identifier that Amazon Rekognition assigns to the input image.

          • ExternalImageId (string) –

            Identifier that you assign to all the faces in the input image.

          • Confidence (float) –

            Confidence level that the bounding box contains a face (and not a different object such as a tree).

          • IndexFacesModelVersion (string) –

            The version of the face detect and storage model that was used when indexing the face vector.

    • FaceModelVersion (string) –

      Version number of the face detection model associated with the input collection ( CollectionId).

Exceptions

  • Rekognition.Client.exceptions.InvalidS3ObjectException

  • Rekognition.Client.exceptions.InvalidParameterException

  • Rekognition.Client.exceptions.ImageTooLargeException

  • Rekognition.Client.exceptions.AccessDeniedException

  • Rekognition.Client.exceptions.InternalServerError

  • Rekognition.Client.exceptions.ThrottlingException

  • Rekognition.Client.exceptions.ProvisionedThroughputExceededException

  • Rekognition.Client.exceptions.ResourceNotFoundException

  • Rekognition.Client.exceptions.InvalidImageFormatException

Examples

This operation searches for faces in a Rekognition collection that match the largest face in an S3 bucket stored image.

response = client.search_faces_by_image(
    CollectionId='myphotos',
    FaceMatchThreshold=95,
    Image={
        'S3Object': {
            'Bucket': 'mybucket',
            'Name': 'myphoto',
        },
    },
    MaxFaces=5,
)

print(response)

Expected Output:

{
    'FaceMatches': [
        {
            'Face': {
                'BoundingBox': {
                    'Height': 0.3234420120716095,
                    'Left': 0.3233329951763153,
                    'Top': 0.5,
                    'Width': 0.24222199618816376,
                },
                'Confidence': 99.99829864501953,
                'FaceId': '38271d79-7bc2-5efb-b752-398a8d575b85',
                'ImageId': 'd5631190-d039-54e4-b267-abd22c8647c5',
            },
            'Similarity': 99.97036743164062,
        },
    ],
    'SearchedFaceBoundingBox': {
        'Height': 0.33481481671333313,
        'Left': 0.31888890266418457,
        'Top': 0.4933333396911621,
        'Width': 0.25,
    },
    'SearchedFaceConfidence': 99.9991226196289,
    'ResponseMetadata': {
        '...': '...',
    },
}