Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microsoft Cognitive Services: Starting With Face API

DZone's Guide to

Microsoft Cognitive Services: Starting With Face API

Learn how to get started with a new Microsoft Cognitive Services feature, the Face API service, now that Microsoft Cognitive Services has moved to Azure.

· AI Zone
Free Resource

Find out how AI-Fueled APIs from Neura can make interesting products more exciting and engaging. 

I started using Microsoft Cognitive Services a while back for the purpose of learning and using their features for my smart home scenarios. Now that they've moved to Azure, access to their old home is coming to an end, and old access keys are expiring. I couldn't find any way to properly migrate my old services data I've trained (like people's faces) to Azure, so I've decided to start from scratch, beginning with service creation. This blog post will deal with a new Cognitive Service creation — specifically, adding a new Face API service.

Microsoft Cognitive Services

Microsoft Cognitive Services is a set of APIs that employ powerful machine learning algorithms to provide application developers with intelligent features like image and voice recognition, face identification, and language understanding. Starting under the code name Project Oxford and with limited availability for testing, Cognitive Services evolved significantly over the time and was recently moved to the Microsoft Azure Portal.

Face API

Face API consists of two main group of APIs: face detection and face recognition. While face detection APIs can detect people faces in provided images, along with face's attributes (gender, age, emotion, makeup, etc.) and position on that image (face, mouth, eyes, nose, etc.), face recognition can actually recognize detected faces on an image if the detected face matches one of those in previously trained data.

Creating a New Face API Service in Azure

Head here and log into your account. Hit New (the big green plus) and search for Cognitive Services. You'll get a bunch of available apps in the results; select Face API.

Image title

In the above screen, enter the required data and click Create. Before that, consider selecting the right pricing tier for you. In the time of this writing, there are two tiers available:

  1. Free tier (F0) costs nothing but is limited to 20 calls per minute and overall 30,000 calls per month.
  2. Standard tier (S0) is limited to 10 calls per second and calls cost per the table below.

There is also a cost for stored images if you need face identification and matching capabilities.

Image title

Now that the Face API resource is created, the most important thing is to take note of the pair of access keys that are available through the Resource Management > Keys section. 

Image title

These keys are associated with your Azure account will allow you to access Face API endpoints. Let's try them live with API testing console!

Testing With API Keys

Cognitive Services APIs have excellent documentation with a ready-to-test API console; you can even choose which Azure you want to use (note that not every service is available in every region).

Image title

Here's the link to use West Central U.S. endpoints. Following it will land you on the API documentation page with an entry form used to supply required and optional parameters. Note that API keys only work for the same region you picked when creating API service on Azure.

Image title

Query Parameters lets you pick what you want the service to return. If any faces are detected, setting returnFaceId will get you IDs of those faces if you need them for subsequent calls. returnFaceLandmarks will, if set to true, return positions of various face landmarks if they were detected. For returnFaceAttributes, simply list what attributes you want to be returned. In the above example, I'm interested in age and gender of any detected face.

Don't forget to enter or paste your API key into the Ocp-Apim-Subscription-Key field.

Finally, use the request body to enter a valid image URL you want to test with and hit the Send button below.

Image title

If everything went OK, you should get an HTTP 200 response with a detailed JSON result (I've clipped some of the response below to shorten it):

[
  {
    "faceId": "***********************************",
    "faceRectangle": {
      "top": 79,
      "left": 57,
      "width": 43,
      "height": 43
    },
    "faceLandmarks": {
      "pupilLeft": {
        "x": 66.4,
        "y": 94.3
      },
      "pupilRight": {
        "x": 83.9,
        "y": 88.1
      },
      "noseTip": {
        "x": 79.4,
        "y": 102.0
      },
      "mouthLeft": {
        "x": 71.9,
        "y": 113.0
      },
      "mouthRight": {
        "x": 90.5,
        "y": 106.8
      },
      "eyebrowLeftOuter": {
        "x": 59.5,
        "y": 93.6
      },
      "eyebrowLeftInner": {
        "x": 70.2,
        "y": 90.1
      },
... ... ...
    "faceAttributes": {
      "gender": "male",
      "age": 64.8
    }
  },
  {
    "faceId": "***********************************",
    "faceRectangle": {
      "top": 50,
      "left": 88,
      "width": 39,
      "height": 39
    },
    "faceLandmarks": {
... ... ... 
      "noseRightAlarOutTip": {
        "x": 114.9,
        "y": 71.0
      },
      "upperLipTop": {
        "x": 110.3,
        "y": 77.4
      },
      "upperLipBottom": {
        "x": 110.7,
        "y": 78.2
      },
      "underLipTop": {
        "x": 112.3,
        "y": 81.7
      },
      "underLipBottom": {
        "x": 113.2,
        "y": 83.8
      }
    },
    "faceAttributes": {
      "gender": "female",
      "age": 60.0
    }
  }
]

Wrapping Up

Now that Face API Cognitive Service is up and running, you can do interesting things with it. This blog post was about setting the service up; follow-up posts will focus on use cases and other parts of Cognitive Services. 

To find out how AI-Fueled APIs can increase engagement and retention, download Six Ways to Boost Engagement for Your IoT Device or App with AI today.

Topics:
ai ,tutorial ,facial recognition ,face api ,microsoft cognitive services

Published at DZone with permission of Andrej Tozon, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}