Find similar faces

Caution

Face service access is limited based on eligibility and usage criteria in order to support our Responsible AI principles. Face service is only available to Microsoft managed customers and partners. Use the Face Recognition intake form to apply for access. For more information, see the Face limited access page.

The Find Similar operation does face matching between a target face and a set of candidate faces, finding a smaller set of faces that look similar to the target face. This is useful for doing a face search by image.

This guide demonstrates how to use the Find Similar feature in the different language SDKs. The following sample code assumes you have already authenticated a Face client object. For details on how to do this, follow a quickstart.

Set up sample URL

This guide uses remote images that are accessed by URL. Save a reference to the following URL string. All of the images accessed in this guide are located at this URL path.

https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/

Detect faces for comparison

You need to detect faces in images before you can compare them. In this guide, the following remote image, called findsimilar.jpg, will be used as the source:

Photo of a man who is smiling.

The following face detection method is optimized for comparison operations. It doesn't extract detailed face attributes, and it uses an optimized recognition model.

private static async Task<List<FaceDetectionResult>> DetectFaceRecognize(FaceClient faceClient, string url)
{
    // Detect faces from image URL.
    var response = await faceClient.DetectAsync(new Uri(url), FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true, [FaceAttributeType.QualityForRecognition]);
    IReadOnlyList<FaceDetectionResult> detectedFaces = response.Value;
    List<FaceDetectionResult> sufficientQualityFaces = new List<FaceDetectionResult>();
    foreach (FaceDetectionResult detectedFace in detectedFaces)
    {
        QualityForRecognition? faceQualityForRecognition = detectedFace.FaceAttributes.QualityForRecognition;
        if (faceQualityForRecognition.HasValue && (faceQualityForRecognition.Value != QualityForRecognition.Low))
        {
            sufficientQualityFaces.Add(detectedFace);
        }
    }
    Console.WriteLine($"{detectedFaces.Count} face(s) with {sufficientQualityFaces.Count} having sufficient quality for recognition detected from image `{Path.GetFileName(url)}`");

    return sufficientQualityFaces;
}

The following code uses the above method to get face data from a series of images.

Console.WriteLine("========FIND SIMILAR========");
Console.WriteLine();

List<string> targetImageFileNames = new List<string>
                    {
                        "Family1-Dad1.jpg",
                        "Family1-Daughter1.jpg",
                        "Family1-Mom1.jpg",
                        "Family1-Son1.jpg",
                        "Family2-Lady1.jpg",
                        "Family2-Man1.jpg",
                        "Family3-Lady1.jpg",
                        "Family3-Man1.jpg"
                    };

string sourceImageFileName = "findsimilar.jpg";
List<Guid> targetFaceIds = new List<Guid>();
foreach (string targetImageFileName in targetImageFileNames)
{
    // Detect faces from target image url.
    List<FaceDetectionResult> faces = await DetectFaceRecognize(client, $"{baseUrl}{targetImageFileName}");
    // Add detected faceId to list of GUIDs.
    targetFaceIds.Add(faces[0].FaceId.Value);
}

// Detect faces from source image url.
List<FaceDetectionResult> detectedFaces = await DetectFaceRecognize(client, $"{baseUrl}{sourceImageFileName}");
Console.WriteLine();

Find and print matches

In this guide, the face detected in the Family1-Dad1.jpg image should be returned as the face that's similar to the source image face.

Photo of a man who is smiling; this is the same person as the previous image.

The following code calls the Find Similar API on the saved list of faces.

// Find a similar face(s) in the list of IDs. Comapring only the first in list for testing purposes.
var response = await client.FindSimilarAsync(detectedFaces[0].FaceId.Value, targetFaceIds);
List<FaceFindSimilarResult> similarResults = response.Value.ToList();

The following code prints the match details to the console:

foreach (FaceFindSimilarResult similarResult in similarResults)
{
    Console.WriteLine($"Faces from {sourceImageFileName} & ID:{similarResult.FaceId} are similar with confidence: {similarResult.Confidence}.");
}
Console.WriteLine();

Next steps

In this guide, you learned how to call the Find Similar API to do a face search by similarity in a larger group of faces. Next, learn more about the different recognition models available for face comparison operations.