Microsoft Azure AI Solution

Adding Faces to Persons in Microsoft Azure AI Solution

Question

You have a requirement in your application to group faces based on the similar looking faces.Assuming that you have initialized the variables, authorized the API call, created the PersonGroup and created the persons for the PersonGroup.

Review the code snippet given below for adding faces to the persons and complete it by choosing the most appropriate answer choice:

Parallel.For(0, PersonCount, async i => { Guid personId = persons[i].PersonId; string personImageDir = @"/path/to/person/i/images"; foreach (string imagePath in Directory.GetFiles(personImageDir, "*.jpg")) { await WaitCallLimitPerSecondAsync(); using (Stream stream = File.OpenRead(imagePath)) { await faceClient………………………………………………………….(personGroupId, personId, stream); }} }); 

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer: D.

Option A is incorrect because in order to use AddFaceFromUrlAsync method, you would need to provide image urls, such as “string imageUrl = "https://<path to jpg file>";

Option B is incorrect because the CreateAsync method is used for creating the person.

Here is a quick example “persons[i] = await faceClient.PersonGroupPerson.CreateAsync(personGroupId, personName)”

Option C is incorrect because the UpdateFaceAsync method is used to update a person's persisted face data.

Requirement here is to add new faces instead.

Option D is correct because the requirement here is to add faces to the PersonGroup using an stream input.

Here is the completed C# code.

<pre class="brush:java;">Parallel.For(0, PersonCount, async i =>

{

Guid personId = persons[i].PersonId;

string personImageDir = @"/path/to/person/i/images";

foreach (string imagePath in Directory.GetFiles(personImageDir, "*.jpg"))

{

await WaitCallLimitPerSecondAsync();

using (Stream stream = File.OpenRead(imagePath))

{

await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, personId, stream);

}}

});

</pre>

Reference:

To learn more about adding faces to a group, use the link given below:

The code snippet provided is using the Azure Cognitive Services Face API to add faces to persons in a PersonGroup. The snippet is looping through each person's image directory and adding each image as a face to the corresponding person.

To complete the code snippet, we need to choose the appropriate method to add a face to the person in the PersonGroup.

The available answer choices are:

A. PersonGroupPerson.AddFaceFromUrlAsync B. PersonGroupPerson.CreateAsync C. PersonGroupPerson.UpdateFaceAsync D. PersonGroupPerson.AddFaceFromStreamAsync.

Since we are reading the images from a directory and creating a stream from the image file, we need to use a method that accepts a stream as input. Therefore, we can eliminate option A since it is for adding faces from a URL.

Option B is for creating a new person in the PersonGroup and not for adding a face to an existing person, so it can be eliminated as well.

Option C is for updating an existing face for a person, but we are adding new faces, so this option can also be eliminated.

This leaves us with option D, which is the correct answer. The PersonGroupPerson.AddFaceFromStreamAsync method can be used to add a new face to an existing person in the PersonGroup using a stream.

Therefore, the completed code snippet should be:

csharp
Parallel.For(0, PersonCount, async i => { Guid personId = persons[i].PersonId; string personImageDir = @"/path/to/person/i/images"; foreach (string imagePath in Directory.GetFiles(personImageDir, "*.jpg")) { await WaitCallLimitPerSecondAsync(); using (Stream stream = File.OpenRead(imagePath)) { await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, personId, stream); } } });