var subscriptionKey = "Copy your Subscription key here";
var imageUrl = "http://davidgiard.com/themes/Giard/images/logo.png";
var webSvcUrl = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,smile,facialHair,headPose,glasses";
var outputDiv = $("#OutputDiv");
outputDiv.text("Thinking...");
$.ajax({
type: "POST",
url: webSvcUrl,
headers: { "Ocp-Apim-Subscription-Key": subscriptionKey },
contentType: "application/json",
data: '{ "Url": "' + imageUrl + '" }'
}).done(function (data) {
if (data.length > 0) {
var firstFace = data[0];
var faceId = firstFace.faceId;
var faceRectange = firstFace.faceRectangle;
var faceWidth = faceRectange.width;
var faceHeight = faceRectange.height;
var faceLeft = faceRectange.left;
var faceTop = faceRectange.top;
var faceLandmarks = firstFace.faceLandmarks;
var faceAttributes = firstFace.faceAttributes;
var leftPupil = faceLandmarks.pupilLeft;
var rightPupil = faceLandmarks.pupilRight;
var mouth = faceLandmarks.mouthLeft;
var nose = faceLandmarks.noseLeftAlarOutTip;
var mouthTop = faceLandmarks.upperLipTop;
var mouthBottom = faceLandmarks.underLipBottom;
leftEyeWidth = faceLandmarks.eyebrowLeftInner.x - faceLandmarks.eyebrowLeftOuter.x;
rightEyeWidth = faceLandmarks.eyebrowRightOuter.x - faceLandmarks.eyebrowRightInner.x;
mouthWidth = faceLandmarks.mouthRight.x - faceLandmarks.mouthLeft.x;
var mouthLeft = faceLandmarks.mouthLeft;
var mouthRight = faceLandmarks.mouthRight;
var mouthTop = faceLandmarks.upperLipTop;
var mouthBottom = faceLandmarks.underLipBottom;
var outputText = "";
outputText += "Face ID: " + faceId + "<br>";
outputText += "Top: " + faceTop + "<br>";
outputText += "Left: " + faceLeft + "<br>";
outputText += "Width: " + faceWidth + "<br>";
outputText += "Height: " + faceHeight + "<br>";
outputText += "Right Pupil: " + rightPupil.x + ", " + rightPupil.y + "<br>";
outputText += "Left Pupil: " + leftPupil.x + ", " + leftPupil.y + "<br>";
outputText += "Mouth: <br>";
outputText += " -Left: " + mouthLeft.x + ", " + mouthLeft.y + "<br>";
outputText += " -Right: " + mouthRight.x + ", " + mouthRight.y + "<br>";
outputText += " -Top: " + mouthTop.x + ", " + mouthTop.y + "<br>";
outputText += " -Bottom: " + mouthBottom.x + ", " + mouthBottom.y + "<br>";
outputText += "Attributes:" + "<br>";
outputText += "age: " + faceAttributes.age + "<br>";
outputText += "gender: " + faceAttributes.gender + "<br>";
outputText += "smile: " + (faceAttributes.smile || "n/a") + "<br>";
outputText += "glasses: " + faceAttributes.glasses + "<br>";
outputDiv.html(outputText);
}
else {
outputDiv.text("No faces detected.");
}
}).fail(function (err) {
$("#OutputDiv").text("ERROR!" + err.responseText);
});
service call is performed by the following line:
$.ajax({
type: "POST",
url: webSvcUrl,
headers: { "Ocp-Apim-Subscription-Key": subscriptionKey },
contentType: "application/json",
data: '{ "Url": "' + imageUrl + '" }'
This request is Asynchronous, so the "done" function is called when it returns successfully.
The function tied to the "done" event parses through the returned JSON and displays it on the screen.
If an error occurs, we output a simple error message to the user in the "fail" function.
The rest of the code above simply grabs the first face in the JSON array and drills down into properties of that face, displaying those properties in a DIV on the page.
For example, in the attached site.css stylesheet, I’ve defined 2 classes - .Rectangle and .FaceLabel - that initially hide objects on the page via the display: none style. These classes also set the position to “absolute” allowing us to position them exactly where we want within a container. The z-order is incresed, so that these items will appear on top of the face in the picture. The relevant CSS is shown below:
Our page contains object with these classes to identify parts of the face identified. Initially, they will be invisible until we determine where to place them via the information returned by the Face API.
When the call to the Face API web service returns successfully, we drill down into the returned JSON to find out the outline of the face and the location of the eyes, nose, and mouth. Then, we make these objects visible (set the display style to “block”) and place them above the corresponding facial feature (set the “top” and “left” styles). In the case of the Rectangle image, we also resize it to cover the face detected. The rectangle’s “opacity” style is 0.3, making it translucent enough to see the face behind it. Here is the JavaScript to accomplish this:
$("#Rectangle").css("top", faceTop);
$("#Rectangle").css("left", faceLeft);
$("#Rectangle").css("height", faceHeight);
$("#Rectangle").css("width", faceHeight);
$("#Rectangle").css("display", "block");
$("#LeftEyeDiv").css("top", leftPupil.y);
$("#LeftEyeDiv").css("left", leftPupil.x);
$("#LeftEyeDiv").css("display", "block");
$("#RightEyeDiv").css("top", rightPupil.y);
$("#RightEyeDiv").css("left", rightPupil.x);
$("#RightEyeDiv").css("display", "block");
$("#NoseDiv").css("top", nose.y);
$("#NoseDiv").css("left", noseHorizontalCenter);
$("#NoseDiv").css("display", "block");
$("#MouthDiv").css("top", mouthVerticalCenter);
$("#MouthDiv").css("left", mouthTop.x);
$("#MouthDiv").css("display", "block");
As you can see, calling the Cognitive Services Face API, is a simple matter of making a call to a web service and reading the JSON data returned by that service.