From 5860952b371066b504c5427b3736010dc364f4e6 Mon Sep 17 00:00:00 2001 From: Han Chiang Date: Tue, 20 Aug 2024 04:01:35 +0000 Subject: [PATCH] Sample for stateless --- .../Sample4_StatelessFaceRecognition.md | 113 ++++++++++++++++++ .../Sample4_StatelessFaceRecognitionAsync.md | 113 ++++++++++++++++++ .../Sample4_StatelessFaceRecognition.cs | 113 ++++++++++++++++++ .../Sample4_StatelessFaceRecognitionAsync.cs | 7 +- 4 files changed, 345 insertions(+), 1 deletion(-) create mode 100644 sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognition.md create mode 100644 sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognitionAsync.md create mode 100644 sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognition.cs diff --git a/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognition.md b/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognition.md new file mode 100644 index 0000000000000..0ded40f945e4c --- /dev/null +++ b/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognition.md @@ -0,0 +1,113 @@ +# Stateless face Recognition + +This sample demonstrates how to recognize faces in an image without data structure. + +To get started you'll need an Azure AI resource or a Face resource. See [README][README] for prerequisites and instructions. + +## Creating a `FaceClient` + +To create a new `FaceClient` you need the endpoint and credentials from your resource. In the sample below you'll use a `DefaultAzureCredential` object to authenticate. You can set `endpoint` based on an environment variable, a configuration setting, or any way that works for your application. See [Authenticate the client][README_authticate] for instructions. + +```C# Snippet:CreateFaceClient +Uri endpoint = new Uri(""); +DefaultAzureCredential credential = new DefaultAzureCredential(); +var client = new FaceClient(endpoint, credential); +``` + +## Verify whether two faces belong to the same person + +To verify whether two faces belong to the same person, you can use the `VerifyFaceToFace` method. This method returns a `FaceVerificationResult` object that contains a `Confidence` score indicating the similarity between the two faces. + +```C# Snippet:VerifyFaceToFace +var data = new (string Name, Uri Uri)[] { + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) +}; +var faceIds = new List(); + +foreach (var tuple in data) +{ + var detectResponse = client.Detect(tuple.Uri, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{tuple.Name}'."); + faceIds.Add(detectResponse.Value.Single().FaceId.Value); +} + +var verifyDad1Dad2Response = client.VerifyFaceToFace(faceIds[0], faceIds[1]); +Console.WriteLine($"Verification between Dad image 1 and Dad image 2: {verifyDad1Dad2Response.Value.Confidence}"); +Console.WriteLine($"Is the same person: {verifyDad1Dad2Response.Value.IsIdentical}"); + +var verifyDad1SonResponse = client.VerifyFaceToFace(faceIds[0], faceIds[2]); +Console.WriteLine($"Verification between Dad image 1 and Son image 1: {verifyDad1SonResponse.Value.Confidence}"); +Console.WriteLine($"Is the same person: {verifyDad1SonResponse.Value.IsIdentical}"); +``` + +## Find similar faces from a list of faces + +To find similar faces from a list of faces, you can use the `FindSimilar` method. This method returns a list of `FaceFindSimilarResult` objects that contain the `FaceId` of the face and a `Confidence` score indicating the similarity between the face and the query face. + +```C# Snippet:FindSimilar +var dadImage = new Uri(FaceTestConstant.UrlFamily1Dad1Image); +var detectDadResponse = client.Detect(dadImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); +Console.WriteLine($"Detected {detectDadResponse.Value.Count} face(s) in the Dad image."); +var dadFaceId = detectDadResponse.Value.Single().FaceId.Value; + +var targetImage = new Uri(FaceTestConstant.UrlIdentification1Image); +var detectResponse = client.Detect(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); +Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image."); +var faceIds = detectResponse.Value.Select(face => face.FaceId.Value); + +var response = client.FindSimilar(dadFaceId, faceIds); +var similarFaces = response.Value; +Console.WriteLine($"Found {similarFaces.Count} similar face(s) in the target image."); +foreach (var similarFace in similarFaces) +{ + Console.WriteLine($"Face ID: {similarFace.FaceId}, confidence: {similarFace.Confidence}"); +} +``` + +## Group faces + +To group faces, you can use the `Group` method. This method returns a `FaceGroupingResult` objects that contain a 2 dimensional array of faces. Each array represents a group of faces that belong to the same person. There is also a faces array that contains all the faces that were not grouped. + +```C# Snippet:Group +var targetImages = new (string, Uri)[] { + ("Group image", new Uri(FaceTestConstant.UrlIdentification1Image)), + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) +}; +var faceIds = new Dictionary(); + +foreach (var (imageName, targetImage) in targetImages) +{ + var detectResponse = client.Detect(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{imageName}'."); + foreach (var face in detectResponse.Value) + { + faceIds[face.FaceId.Value] = (face, imageName); + } +} + +var groupResponse = client.Group(faceIds.Keys); +var groups = groupResponse.Value; + +Console.WriteLine($"Found {groups.Groups.Count} group(s) in the target images."); +foreach (var group in groups.Groups) +{ + Console.WriteLine($"Group: "); + foreach (var faceId in group) + { + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); + } +} + +Console.WriteLine($"Found {groups.MessyGroup.Count} face(s) that are not in any group."); +foreach (var faceId in groups.MessyGroup) +{ + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); +} +``` + +[README]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/face/Azure.AI.Vision.Face#getting-started +[README_authticate]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/face/Azure.AI.Vision.Face#authenticate-the-client diff --git a/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognitionAsync.md b/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognitionAsync.md new file mode 100644 index 0000000000000..206de64937c07 --- /dev/null +++ b/sdk/face/Azure.AI.Vision.Face/samples/Sample4_StatelessFaceRecognitionAsync.md @@ -0,0 +1,113 @@ +# Stateless face Recognition + +This sample demonstrates how to recognize faces in an image without data structure. + +To get started you'll need an Azure AI resource or a Face resource. See [README][README] for prerequisites and instructions. + +## Creating a `FaceClient` + +To create a new `FaceClient` you need the endpoint and credentials from your resource. In the sample below you'll use a `DefaultAzureCredential` object to authenticate. You can set `endpoint` based on an environment variable, a configuration setting, or any way that works for your application. See [Authenticate the client][README_authticate] for instructions. + +```C# Snippet:CreateFaceClient +Uri endpoint = new Uri(""); +DefaultAzureCredential credential = new DefaultAzureCredential(); +var client = new FaceClient(endpoint, credential); +``` + +## Verify whether two faces belong to the same person + +To verify whether two faces belong to the same person, you can use the `VerifyFaceToFaceAsync` method. This method returns a `FaceVerificationResult` object that contains a `Confidence` score indicating the similarity between the two faces. + +```C# Snippet:VerifyFaceToFaceAsync +var data = new (string Name, Uri Uri)[] { + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) +}; +var faceIds = new List(); + +foreach (var tuple in data) +{ + var detectResponse = await client.DetectAsync(tuple.Uri, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{tuple.Name}'."); + faceIds.Add(detectResponse.Value.Single().FaceId.Value); +} + +var verifyDad1Dad2Response = await client.VerifyFaceToFaceAsync(faceIds[0], faceIds[1]); +Console.WriteLine($"Verification between Dad image 1 and Dad image 2: {verifyDad1Dad2Response.Value.Confidence}"); +Console.WriteLine($"Is the same person: {verifyDad1Dad2Response.Value.IsIdentical}"); + +var verifyDad1SonResponse = await client.VerifyFaceToFaceAsync(faceIds[0], faceIds[2]); +Console.WriteLine($"Verification between Dad image 1 and Son image 1: {verifyDad1SonResponse.Value.Confidence}"); +Console.WriteLine($"Is the same person: {verifyDad1SonResponse.Value.IsIdentical}"); +``` + +## Find similar faces from a list of faces + +To find similar faces from a list of faces, you can use the `FindSimilarAsync` method. This method returns a list of `FaceFindSimilarResult` objects that contain the `FaceId` of the face and a `Confidence` score indicating the similarity between the face and the query face. + +```C# Snippet:FindSimilarAsync +var dadImage = new Uri(FaceTestConstant.UrlFamily1Dad1Image); +var detectDadResponse = await client.DetectAsync(dadImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); +Console.WriteLine($"Detected {detectDadResponse.Value.Count} face(s) in the Dad image."); +var dadFaceId = detectDadResponse.Value.Single().FaceId.Value; + +var targetImage = new Uri(FaceTestConstant.UrlIdentification1Image); +var detectResponse = await client.DetectAsync(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); +Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image."); +var faceIds = detectResponse.Value.Select(face => face.FaceId.Value); + +var response = await client.FindSimilarAsync(dadFaceId, faceIds); +var similarFaces = response.Value; +Console.WriteLine($"Found {similarFaces.Count} similar face(s) in the target image."); +foreach (var similarFace in similarFaces) +{ + Console.WriteLine($"Face ID: {similarFace.FaceId}, confidence: {similarFace.Confidence}"); +} +``` + +## Group faces + +To group faces, you can use the `GroupAsync` method. This method returns a `FaceGroupingResult` objects that contain a 2 dimensional array of faces. Each array represents a group of faces that belong to the same person. There is also a faces array that contains all the faces that were not grouped. + +```C# Snippet:GroupAsync +var targetImages = new (string, Uri)[] { + ("Group image", new Uri(FaceTestConstant.UrlIdentification1Image)), + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) +}; +var faceIds = new Dictionary(); + +foreach (var (imageName, targetImage) in targetImages) +{ + var detectResponse = await client.DetectAsync(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{imageName}'."); + foreach (var face in detectResponse.Value) + { + faceIds[face.FaceId.Value] = (face, imageName); + } +} + +var groupResponse = await client.GroupAsync(faceIds.Keys); +var groups = groupResponse.Value; + +Console.WriteLine($"Found {groups.Groups.Count} group(s) in the target images."); +foreach (var group in groups.Groups) +{ + Console.WriteLine($"Group: "); + foreach (var faceId in group) + { + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); + } +} + +Console.WriteLine($"Found {groups.MessyGroup.Count} face(s) that are not in any group."); +foreach (var faceId in groups.MessyGroup) +{ + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); +} +``` + +[README]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/face/Azure.AI.Vision.Face#getting-started +[README_authticate]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/face/Azure.AI.Vision.Face#authenticate-the-client diff --git a/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognition.cs b/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognition.cs new file mode 100644 index 0000000000000..7daf2a5826194 --- /dev/null +++ b/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognition.cs @@ -0,0 +1,113 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Collections.Generic; +using System.Linq; +using Azure.AI.Vision.Face.Tests; +using Azure.Core.TestFramework; +using NUnit.Framework; + +namespace Azure.AI.Vision.Face.Samples +{ + public partial class Sample4_StatelessFaceRecognition : FaceSamplesBase + { + [Test] + public void Grouping() + { + var client = CreateClient(); + #region Snippet:Group + var targetImages = new (string, Uri)[] { + ("Group image", new Uri(FaceTestConstant.UrlIdentification1Image)), + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) + }; + var faceIds = new Dictionary(); + + foreach (var (imageName, targetImage) in targetImages) + { + var detectResponse = client.Detect(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{imageName}'."); + foreach (var face in detectResponse.Value) + { + faceIds[face.FaceId.Value] = (face, imageName); + } + } + + var groupResponse = client.Group(faceIds.Keys); + var groups = groupResponse.Value; + + Console.WriteLine($"Found {groups.Groups.Count} group(s) in the target images."); + foreach (var group in groups.Groups) + { + Console.WriteLine($"Group: "); + foreach (var faceId in group) + { + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); + } + } + + Console.WriteLine($"Found {groups.MessyGroup.Count} face(s) that are not in any group."); + foreach (var faceId in groups.MessyGroup) + { + Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); + } + #endregion + } + + [Test] + public void Verification() + { + var client = CreateClient(); + #region Snippet:VerifyFaceToFace + var data = new (string Name, Uri Uri)[] { + ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), + ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), + ("Son image 1", new Uri(FaceTestConstant.UrlFamily1Son1Image)) + }; + var faceIds = new List(); + + foreach (var tuple in data) + { + var detectResponse = client.Detect(tuple.Uri, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image '{tuple.Name}'."); + faceIds.Add(detectResponse.Value.Single().FaceId.Value); + } + + var verifyDad1Dad2Response = client.VerifyFaceToFace(faceIds[0], faceIds[1]); + Console.WriteLine($"Verification between Dad image 1 and Dad image 2: {verifyDad1Dad2Response.Value.Confidence}"); + Console.WriteLine($"Is the same person: {verifyDad1Dad2Response.Value.IsIdentical}"); + + var verifyDad1SonResponse = client.VerifyFaceToFace(faceIds[0], faceIds[2]); + Console.WriteLine($"Verification between Dad image 1 and Son image 1: {verifyDad1SonResponse.Value.Confidence}"); + Console.WriteLine($"Is the same person: {verifyDad1SonResponse.Value.IsIdentical}"); + #endregion + } + + [Test] + public void FindSimilar() + { + var client = CreateClient(); + #region Snippet:FindSimilar + var dadImage = new Uri(FaceTestConstant.UrlFamily1Dad1Image); + var detectDadResponse = client.Detect(dadImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectDadResponse.Value.Count} face(s) in the Dad image."); + var dadFaceId = detectDadResponse.Value.Single().FaceId.Value; + + var targetImage = new Uri(FaceTestConstant.UrlIdentification1Image); + var detectResponse = client.Detect(targetImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); + Console.WriteLine($"Detected {detectResponse.Value.Count} face(s) in the image."); + var faceIds = detectResponse.Value.Select(face => face.FaceId.Value); + + var response = client.FindSimilar(dadFaceId, faceIds); + var similarFaces = response.Value; + Console.WriteLine($"Found {similarFaces.Count} similar face(s) in the target image."); + foreach (var similarFace in similarFaces) + { + Console.WriteLine($"Face ID: {similarFace.FaceId}, confidence: {similarFace.Confidence}"); + } + #endregion + } + } +} diff --git a/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognitionAsync.cs b/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognitionAsync.cs index 53025e67e1eae..f32e0dfe5e77c 100644 --- a/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognitionAsync.cs +++ b/sdk/face/Azure.AI.Vision.Face/tests/samples/Sample4_StatelessFaceRecognitionAsync.cs @@ -18,6 +18,7 @@ public partial class Sample4_StatelessFaceRecognition : FaceSamplesBase public async Task GroupingAsync() { var client = CreateClient(); + #region Snippet:GroupAsync var targetImages = new (string, Uri)[] { ("Group image", new Uri(FaceTestConstant.UrlIdentification1Image)), ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), @@ -54,12 +55,14 @@ public async Task GroupingAsync() { Console.WriteLine($" {faceId} from '{faceIds[faceId].Item2}', face rectangle: {faceIds[faceId].Item1.FaceRectangle.Left}, {faceIds[faceId].Item1.FaceRectangle.Top}, {faceIds[faceId].Item1.FaceRectangle.Width}, {faceIds[faceId].Item1.FaceRectangle.Height}"); } + #endregion } [Test] public async Task VerificationAsync() { var client = CreateClient(); + #region Snippet:VerifyFaceToFaceAsync var data = new (string Name, Uri Uri)[] { ("Dad image 1", new Uri(FaceTestConstant.UrlFamily1Dad1Image)), ("Dad image 2", new Uri(FaceTestConstant.UrlFamily1Dad2Image)), @@ -81,13 +84,14 @@ public async Task VerificationAsync() var verifyDad1SonResponse = await client.VerifyFaceToFaceAsync(faceIds[0], faceIds[2]); Console.WriteLine($"Verification between Dad image 1 and Son image 1: {verifyDad1SonResponse.Value.Confidence}"); Console.WriteLine($"Is the same person: {verifyDad1SonResponse.Value.IsIdentical}"); + #endregion } [Test] public async Task FindSimilarAsync() { var client = CreateClient(); - + #region Snippet:FindSimilarAsync var dadImage = new Uri(FaceTestConstant.UrlFamily1Dad1Image); var detectDadResponse = await client.DetectAsync(dadImage, FaceDetectionModel.Detection03, FaceRecognitionModel.Recognition04, true); Console.WriteLine($"Detected {detectDadResponse.Value.Count} face(s) in the Dad image."); @@ -105,6 +109,7 @@ public async Task FindSimilarAsync() { Console.WriteLine($"Face ID: {similarFace.FaceId}, confidence: {similarFace.Confidence}"); } + #endregion } } }