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
326 changes: 171 additions & 155 deletions javascript/Face/sdk_quickstart-single.js
Original file line number Diff line number Diff line change
@@ -1,159 +1,175 @@
// <snippet_single>
'use strict';

const msRest = require("@azure/ms-rest-js");
const Face = require("@azure/cognitiveservices-face");
const { v4: uuid } = require('uuid');

const key = process.env.VISION_KEY;
const endpoint = process.env.VISION_ENDPOINT;

const credentials = new msRest.ApiKeyCredentials({ inHeader: { 'Ocp-Apim-Subscription-Key': key } });
const client = new Face.FaceClient(credentials, endpoint);


const image_base_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/";
const person_group_id = uuid();

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function DetectFaceRecognize(url) {
// Detect faces from image URL. Since only recognizing, use the recognition model 4.
// We use detection model 3 because we are only retrieving the qualityForRecognition attribute.
// Result faces with quality for recognition lower than "medium" are filtered out.
let detected_faces = await client.face.detectWithUrl(url,
{
detectionModel: "detection_03",
recognitionModel: "recognition_04",
returnFaceAttributes: ["QualityForRecognition"]
const { randomUUID } = require("crypto");

const { AzureKeyCredential } = require("@azure/core-auth");

const createFaceClient = require("@azure-rest/ai-vision-face").default,
{ FaceAttributeTypeRecognition04, getLongRunningPoller } = require("@azure-rest/ai-vision-face");

/**
* NOTE This sample might not work with the free tier of the Face service because it might exceed the rate limits.
* If that happens, try inserting calls to sleep() between calls to the Face service.
*/
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const main = async () => {
const endpoint = process.env["FACE_ENDPOINT"] ?? "<endpoint>";
const apikey = process.env["FACE_APIKEY"] ?? "<apikey>";
const credential = new AzureKeyCredential(apikey);
const client = createFaceClient(endpoint, credential);

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

console.log("========IDENTIFY FACES========");
console.log();

// Create a dictionary for all your images, grouping similar ones under the same key.
const personDictionary = {
"Family1-Dad": ["Family1-Dad1.jpg", "Family1-Dad2.jpg"],
"Family1-Mom": ["Family1-Mom1.jpg", "Family1-Mom2.jpg"],
"Family1-Son": ["Family1-Son1.jpg", "Family1-Son2.jpg"],
"Family1-Daughter": ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"],
"Family2-Lady": ["Family2-Lady1.jpg", "Family2-Lady2.jpg"],
"Family2-Man": ["Family2-Man1.jpg", "Family2-Man2.jpg"],
};

// 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("/persongroups/{personGroupId}", personGroupId).put({
body: {
name: personGroupId,
recognitionModel: "recognition_04",
},
});

// The similar faces will be grouped into a single 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("/persongroups/{personGroupId}/persons", personGroupId)
.post({
body: { name },
});
return detected_faces.filter(face => face.faceAttributes.qualityForRecognition == 'high' || face.faceAttributes.qualityForRecognition == 'medium');
}

async function AddFacesToPersonGroup(person_dictionary, person_group_id) {
console.log ("Adding faces to person group...");
// The similar faces will be grouped into a single person group person.

await Promise.all (Object.keys(person_dictionary).map (async function (key) {
const value = person_dictionary[key];


let person = await client.personGroupPerson.create(person_group_id, { name : key });
console.log("Create a persongroup person: " + key + ".");

// Add faces to the person group person.
await Promise.all (value.map (async function (similar_image) {

// Wait briefly so we do not exceed rate limits.
await sleep (1000);


// Check if the image is of sufficent quality for recognition.
let sufficientQuality = true;
let detected_faces = await client.face.detectWithUrl(image_base_url + similar_image,
{
returnFaceAttributes: ["QualityForRecognition"],
detectionModel: "detection_03",
recognitionModel: "recognition_03"
});
detected_faces.forEach(detected_face => {
if (detected_face.faceAttributes.qualityForRecognition != 'high'){
sufficientQuality = false;
}
});

// Wait briefly so we do not exceed rate limits.
await sleep (1000);

// Quality is sufficent, add to group.
if (sufficientQuality){
console.log("Add face to the person group person: (" + key + ") from image: " + similar_image + ".");
await client.personGroupPerson.addFaceFromUrl(person_group_id, person.personId, image_base_url + similar_image);
}
// Wait briefly so we do not exceed rate limits.
await sleep (1000);
}));
}));

console.log ("Done adding faces to person group.");
}

async function WaitForPersonGroupTraining(person_group_id) {
// Wait so we do not exceed rate limits.
console.log ("Waiting 10 seconds...");
await sleep (10000);
let result = await client.personGroup.getTrainingStatus(person_group_id);
console.log("Training status: " + result.status + ".");
if (result.status !== "succeeded") {
await WaitForPersonGroupTraining(person_group_id);
}
}

/* NOTE This function might not work with the free tier of the Face service
because it might exceed the rate limits. If that happens, try inserting calls
to sleep() between calls to the Face service.
*/
async function IdentifyInPersonGroup() {
console.log("========IDENTIFY FACES========");
console.log();

// Create a dictionary for all your images, grouping similar ones under the same key.
const person_dictionary = {
"Family1-Dad" : ["Family1-Dad1.jpg", "Family1-Dad2.jpg"],
"Family1-Mom" : ["Family1-Mom1.jpg", "Family1-Mom2.jpg"],
"Family1-Son" : ["Family1-Son1.jpg", "Family1-Son2.jpg"],
"Family1-Daughter" : ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"],
"Family2-Lady" : ["Family2-Lady1.jpg", "Family2-Lady2.jpg"],
"Family2-Man" : ["Family2-Man1.jpg", "Family2-Man2.jpg"]
};

// A group photo that includes some of the persons you seek to identify from your dictionary.
let source_image_file_name = "identification1.jpg";


// Create a person group.
console.log("Creating a person group with ID: " + person_group_id);
await client.personGroup.create(person_group_id, person_group_id, {recognitionModel : "recognition_04" });

await AddFacesToPersonGroup(person_dictionary, person_group_id);

// Start to train the person group.
console.log();
console.log("Training person group: " + person_group_id + ".");
await client.personGroup.train(person_group_id);

await WaitForPersonGroupTraining(person_group_id);
console.log();

// Detect faces from source image url and only take those with sufficient quality for recognition.
let face_ids = (await DetectFaceRecognize(image_base_url + source_image_file_name)).map (face => face.faceId);

// Identify the faces in a person group.
let results = await client.face.identify(face_ids, { personGroupId : person_group_id});
await Promise.all (results.map (async function (result) {
try{
let person = await client.personGroupPerson.get(person_group_id, result.candidates[0].personId);

console.log("Person: " + person.name + " is identified for face in: " + source_image_file_name + " with ID: " + result.faceId + ". Confidence: " + result.candidates[0].confidence + ".");

// Verification:
let verifyResult = await client.face.verifyFaceToPerson(result.faceId, person.personId, {personGroupId : person_group_id});
console.log("Verification result between face "+ result.faceId +" and person "+ person.personId+ ": " +verifyResult.isIdentical + " with confidence: "+ verifyResult.confidence);

} catch(error) {
//console.log("no persons identified for face with ID " + result.faceId);
console.log(error.toString());
}

}));
console.log();
}

async function main() {
await IdentifyInPersonGroup();
console.log ("Done.");
}
main();
const { personId } = createPersonGroupPersonResponse.body;

await Promise.all(
personDictionary[name].map(async (similarImage) => {
// Check if the image is of sufficent quality for recognition.
const detectResponse = await client.path("/detect").post({
contentType: "application/json",
queryParameters: {
detectionModel: "detection_03",
recognitionModel: "recognition_04",
returnFaceId: false,
returnFaceAttributes: [FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION],
},
body: { url: `${imageBaseUrl}${similarImage}` },
});

const sufficientQuality = detectResponse.body.every(
(face) => face.faceAttributes?.qualityForRecognition === "high",
);
if (!sufficientQuality) {
return;
}

// Quality is sufficent, add to group.
console.log(
`Add face to the person group person: (${name}) from image: (${similarImage})`,
);
await client
.path(
"/persongroups/{personGroupId}/persons/{personId}/persistedfaces",
personGroupId,
personId,
)
.post({
queryParameters: { detectionModel: "detection_03" },
body: { url: `${imageBaseUrl}${similarImage}` },
});
}),
);
}),
);
console.log("Done adding faces to person group.");

// Start to train the person group.
console.log();
console.log(`Training person group: ${personGroupId}`);
const trainResponse = await client
.path("/persongroups/{personGroupId}/train", personGroupId)
.post();
const poller = await getLongRunningPoller(client, trainResponse);
await poller.pollUntilDone();
console.log(`Training status: ${poller.getOperationState().status}`);
if (poller.getOperationState().status !== "succeeded") {
return;
}

// Detect faces from source image url and only take those with sufficient quality for recognition.
const detectResponse = await client.path("/detect").post({
contentType: "application/json",
queryParameters: {
detectionModel: "detection_03",
recognitionModel: "recognition_04",
returnFaceId: true,
},
body: { url: `${imageBaseUrl}${sourceImageFileName}` },
});
const faceIds = detectResponse.body.map((face) => face.faceId);

// Identify the faces in a person group.
const identifyResponse = await client.path("/identify").post({
body: { faceIds, personGroupId },
});
await Promise.all(
identifyResponse.body.map(async (result) => {
try {
const getPersonGroupPersonResponse = await client
.path(
"/persongroups/{personGroupId}/persons/{personId}",
personGroupId,
result.candidates[0].personId,
)
.get();
const person = getPersonGroupPersonResponse.body;
console.log(
`Person: ${person.name} is identified for face in: ${sourceImageFileName} with ID: ${result.faceId}. Confidence: ${result.candidates[0].confidence}`,
);

// Verification:
const verifyResponse = await client.path("/verify").post({
body: {
faceId: result.faceId,
personGroupId,
personId: person.personId,
},
});
console.log(
`Verification result between face ${result.faceId} and person ${person.personId}: ${verifyResponse.body.isIdentical} with confidence: ${verifyResponse.body.confidence}`,
);
} catch (error) {
console.log(`No persons identified for face with ID ${result.faceId}`);
}
}),
);
console.log();

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

console.log("Done.");
};

main().catch(console.error);
// </snippet_single>
Loading