Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions dotnet/Face/Quickstart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace FaceQuickstart
{
class Program
{
static readonly string personGroupId = Guid.NewGuid().ToString();
static readonly string largePersonGroupId = Guid.NewGuid().ToString();

// URL path for the images.
const string IMAGE_BASE_URL = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/";
Expand All @@ -32,8 +32,8 @@ static void Main(string[] args)
// Authenticate.
FaceClient client = Authenticate(ENDPOINT, SUBSCRIPTION_KEY);

// Identify - recognize a face(s) in a person group (a person group is created in this example).
IdentifyInPersonGroup(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();
// Identify - recognize a face(s) in a large person group (a large person group is created in this example).
IdentifyInLargePersonGroup(client, IMAGE_BASE_URL, RECOGNITION_MODEL4).Wait();

Console.WriteLine("End of quickstart.");
}
Expand Down Expand Up @@ -75,12 +75,12 @@ private static async Task<List<FaceDetectionResult>> DetectFaceRecognize(FaceCli

/*
* IDENTIFY FACES
* To identify faces, you need to create and define a person group.
* The Identify operation takes one or several face IDs from DetectedFace or PersistedFace and a PersonGroup and returns
* To identify faces, you need to create and define a large person group.
* The Identify operation takes one or several face IDs from DetectedFace or PersistedFace and a LargePersonGroup and returns
* a list of Person objects that each face might belong to. Returned Person objects are wrapped as Candidate objects,
* which have a prediction confidence value.
*/
public static async Task IdentifyInPersonGroup(FaceClient client, string url, FaceRecognitionModel recognitionModel)
public static async Task IdentifyInLargePersonGroup(FaceClient client, string url, FaceRecognitionModel recognitionModel)
{
Console.WriteLine("========IDENTIFY FACES========");
Console.WriteLine();
Expand All @@ -95,16 +95,16 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
// A group photo that includes some of the persons you seek to identify from your dictionary.
string sourceImageFileName = "identification1.jpg";

// Create a person group.
Console.WriteLine($"Create a person group ({personGroupId}).");
// Create a large person group.
Console.WriteLine($"Create a person group ({largePersonGroupId}).");
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SUBSCRIPTION_KEY);
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["name"] = personGroupId, ["recognitionModel"] = recognitionModel.ToString() }))))
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["name"] = largePersonGroupId, ["recognitionModel"] = recognitionModel.ToString() }))))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
await httpClient.PutAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}", content);
await httpClient.PutAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}", content);
}
// The similar faces will be grouped into a single person group person.
// The similar faces will be grouped into a single large person group person.
foreach (var groupedFace in personDictionary.Keys)
{
// Limit TPS
Expand All @@ -113,15 +113,15 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["name"] = groupedFace }))))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (var response = await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}/persons", content))
using (var response = await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}/persons", content))
{
string contentString = await response.Content.ReadAsStringAsync();
personId = (string?)(JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString)?["personId"]);
}
}
Console.WriteLine($"Create a person group person '{groupedFace}'.");

// Add face to the person group person.
// Add face to the large person group person.
foreach (var similarImage in personDictionary[groupedFace])
{
Console.WriteLine($"Check whether image is of sufficient quality for recognition");
Expand Down Expand Up @@ -149,27 +149,27 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
continue;
}

// add face to the person group
// add face to the large person group
Console.WriteLine($"Add face to the person group person({groupedFace}) from image `{similarImage}`");
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["url"] = $"{url}{similarImage}" }))))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}/persons/{personId}/persistedfaces?detectionModel=detection_03", content);
await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}/persons/{personId}/persistedfaces?detectionModel=detection_03", content);
}
}
}

// Start to train the person group.
// Start to train the large person group.
Console.WriteLine();
Console.WriteLine($"Train person group {personGroupId}.");
await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}/train", null);
Console.WriteLine($"Train person group {largePersonGroupId}.");
await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}/train", null);

// Wait until the training is completed.
while (true)
{
await Task.Delay(1000);
string? trainingStatus = null;
using (var response = await httpClient.GetAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}/training"))
using (var response = await httpClient.GetAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}/training"))
{
string contentString = await response.Content.ReadAsStringAsync();
trainingStatus = (string?)(JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString)?["status"]);
Expand All @@ -189,9 +189,9 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
// Add detected faceId to sourceFaceIds.
foreach (var detectedFace in detectedFaces) { sourceFaceIds.Add(detectedFace.FaceId.Value); }

// Identify the faces in a person group.
// Identify the faces in a large person group.
List<Dictionary<string, object>> identifyResults = new List<Dictionary<string, object>>();
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["faceIds"] = sourceFaceIds, ["largePersonGroupId"] = personGroupId }))))
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["faceIds"] = sourceFaceIds, ["largePersonGroupId"] = largePersonGroupId }))))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (var response = await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/identify", content))
Expand All @@ -212,7 +212,7 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
}

string? personName = null;
using (var response = await httpClient.GetAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}/persons/{candidates.First()["personId"]}"))
using (var response = await httpClient.GetAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}/persons/{candidates.First()["personId"]}"))
{
string contentString = await response.Content.ReadAsStringAsync();
personName = (string?)(JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString)?["name"]);
Expand All @@ -221,7 +221,7 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
$" confidence: {candidates.First()["confidence"]}.");

Dictionary<string, object> verifyResult = new Dictionary<string, object>();
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["faceId"] = faceId, ["personId"] = candidates.First()["personId"], ["largePersonGroupId"] = personGroupId }))))
using (var content = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new Dictionary<string, object> { ["faceId"] = faceId, ["personId"] = candidates.First()["personId"], ["largePersonGroupId"] = largePersonGroupId }))))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
using (var response = await httpClient.PostAsync($"{ENDPOINT}/face/v1.0/verify", content))
Expand All @@ -234,11 +234,11 @@ public static async Task IdentifyInPersonGroup(FaceClient client, string url, Fa
}
Console.WriteLine();

// Delete person group.
// Delete large person group.
Console.WriteLine("========DELETE PERSON GROUP========");
Console.WriteLine();
await httpClient.DeleteAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{personGroupId}");
Console.WriteLine($"Deleted the person group {personGroupId}.");
await httpClient.DeleteAsync($"{ENDPOINT}/face/v1.0/largepersongroups/{largePersonGroupId}");
Console.WriteLine($"Deleted the person group {largePersonGroupId}.");
Console.WriteLine();
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 24 additions & 24 deletions javascript/Face/Quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const main = async () => {

const imageBaseUrl =
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/";
const personGroupId = randomUUID();
const largePersonGroupId = randomUUID();

console.log("========IDENTIFY FACES========");
console.log();
Expand All @@ -31,27 +31,27 @@ const main = async () => {
// A group photo that includes some of the persons you seek to identify from your dictionary.
const sourceImageFileName = "identification1.jpg";

// Create a person group.
console.log(`Creating a person group with ID: ${personGroupId}`);
await client.path("/largepersongroups/{personGroupId}", personGroupId).put({
// Create a large person group.
console.log(`Creating a person group with ID: ${largePersonGroupId}`);
await client.path("/largepersongroups/{largePersonGroupId}", largePersonGroupId).put({
body: {
name: personGroupId,
name: largePersonGroupId,
recognitionModel: "recognition_04",
},
});

// The similar faces will be grouped into a single person group person.
// The similar faces will be grouped into a single large person group person.
console.log("Adding faces to person group...");
await Promise.all(
Object.keys(personDictionary).map(async (name) => {
console.log(`Create a persongroup person: ${name}`);
const createPersonGroupPersonResponse = await client
.path("/largepersongroups/{personGroupId}/persons", personGroupId)
const createLargePersonGroupPersonResponse = await client
.path("/largepersongroups/{largePersonGroupId}/persons", largePersonGroupId)
.post({
body: { name },
});

const { personId } = createPersonGroupPersonResponse.body;
const { personId } = createLargePersonGroupPersonResponse.body;

await Promise.all(
personDictionary[name].map(async (similarImage) => {
Expand Down Expand Up @@ -84,8 +84,8 @@ const main = async () => {
);
await client
.path(
"/largepersongroups/{personGroupId}/persons/{personId}/persistedfaces",
personGroupId,
"/largepersongroups/{largePersonGroupId}/persons/{personId}/persistedfaces",
largePersonGroupId,
personId,
)
.post({
Expand All @@ -98,11 +98,11 @@ const main = async () => {
);
console.log("Done adding faces to person group.");

// Start to train the person group.
// Start to train the large person group.
console.log();
console.log(`Training person group: ${personGroupId}`);
console.log(`Training person group: ${largePersonGroupId}`);
const trainResponse = await client
.path("/largepersongroups/{personGroupId}/train", personGroupId)
.path("/largepersongroups/{largePersonGroupId}/train", largePersonGroupId)
.post();
const poller = await getLongRunningPoller(client, trainResponse);
await poller.pollUntilDone();
Expand All @@ -127,21 +127,21 @@ const main = async () => {
});
const faceIds = detectResponse.body.filter((face) => face.faceAttributes?.qualityForRecognition !== "low").map((face) => face.faceId);

// Identify the faces in a person group.
// Identify the faces in a large person group.
const identifyResponse = await client.path("/identify").post({
body: { faceIds, largePersonGroupId: personGroupId },
body: { faceIds, largePersonGroupId: largePersonGroupId },
});
await Promise.all(
identifyResponse.body.map(async (result) => {
try {
const getPersonGroupPersonResponse = await client
const getLargePersonGroupPersonResponse = await client
.path(
"/largepersongroups/{personGroupId}/persons/{personId}",
personGroupId,
"/largepersongroups/{largePersonGroupId}/persons/{personId}",
largePersonGroupId,
result.candidates[0].personId,
)
.get();
const person = getPersonGroupPersonResponse.body;
const person = getLargePersonGroupPersonResponse.body;
console.log(
`Person: ${person.name} is identified for face in: ${sourceImageFileName} with ID: ${result.faceId}. Confidence: ${result.candidates[0].confidence}`,
);
Expand All @@ -150,7 +150,7 @@ const main = async () => {
const verifyResponse = await client.path("/verify").post({
body: {
faceId: result.faceId,
largePersonGroupId: personGroupId,
largePersonGroupId: largePersonGroupId,
personId: person.personId,
},
});
Expand All @@ -164,9 +164,9 @@ const main = async () => {
);
console.log();

// Delete person group.
console.log(`Deleting person group: ${personGroupId}`);
await client.path("/largepersongroups/{personGroupId}", personGroupId).delete();
// Delete large person group.
console.log(`Deleting person group: ${largePersonGroupId}`);
await client.path("/largepersongroups/{largePersonGroupId}", largePersonGroupId).delete();
console.log();

console.log("Done.");
Expand Down
Loading