SHASHANGKA SHEKHAR'S BLOG

Hi, I am Shashangka Shekhar, Working with Microsoft Technologies. This is my personal blog where I share some of my experiences.

Face API Using ASP.Net MVC

In this article, we will learn about Face API and create a simple ASP.Net MVC application to detect faces in an image by using Face API.

Contents to be focused:

  • What is Cognitive Services?
  • What is Face API?
  • Sign Up for Face API
  • Create ASP.Net MVC Sample Application
    • Add AngularJS
    • Install & Configure the Face API
  • Upload images to detect faces
  • Mark faces in the image
  • List detected faces with face information
  • Summary

 

Cognitive Services (Project Oxford) :

Microsoft Cognitive Services, formerly known as Project Oxford are a set of machine-learning application programming interfaces (REST APIs), SDKs and services that helps developers to make smarter application by add intelligent features – such as emotion and video detection; facial, speech and vision recognition; and speech and language understanding.

Get more details from website: https://staging.www.projectoxford.ai

 

Face API: 

Microsoft Cognitive Services, there are four main components

  1. Face recognition: recognizes faces in photos, groups faces that look alike and verifies whether two faces are the same,
  2. Speech processing: recognize speech and translate it into text, and vice versa,
  3. Visual tools: analyze visual content to look for things like inappropriate content or a dominant color scheme, and
  4. Language Understanding Intelligent Service (LUIS): understand what users mean when they say or type something using natural, everyday language.

Get more details from Microsoft blog: http://blogs.microsoft.com/next/2015/05/01/microsofts-project-oxford-helps-developers-build-more-intelligent-apps/#sm.00000qcvfxlefheczxed59b9u8jna

We will implement Face recognition API in our sample application. So what is Face API?

Face API, is a cloud-based service that provides the most advanced face algorithms to detect and recognize human faces in images.

Face API has:

  1. Face Detection
  2. Face Verification
  3. Similar Face Searching
  4. Face Grouping
  5. Face Identification

Get detailed overview here: https://www.microsoft.com/cognitive-services/en-us/face-api/documentation/overview

Face Detection: In this post we are focusing on detecting faces so before we deal with the sample application let’s take a closer look to API Reference (Face API – V1.0). To enable the services we need to get an authorization key (API Key) by signing up with the service for free. Go to the link for signup: https://www.microsoft.com/cognitive-services/en-us/sign-up

 

Sign Up for Face API:

Sign up using any one by clicking on it,

  • Microsoft account
  • GitHub
  • LinkedIn

after successfully joining it will redirect to subscriptionsmpage. Request new trials for any of the product by selecting the checkbox.

Process: Click on Request new trials > Face – Preview > Agree Term > Subscribe

Here you can see I have attached a screenshot of my subscription. In the Keys column from Key 1 click on “Show” to preview the API Key, click “Copy” to copy the key for further use.

Key can be regenerate by clicking on “Regenerate”.

So fur we are done with the subscription process, now let’s get started with the ASP.Net MVC sample application.

Create Sample Application: Before going to start the experiment let’s make sure Visual Studio 2015 is installed on development machine. let’s open Visual Studio 2015, From the File menu, click on New > Project.

Select ASP.Net Web Application, name it as you like, I just named it “FaceAPI_MVC” click Ok button to proceed on for next step.

Choose empty template for the sample application, select “MVC” check box then click Ok.

In our empty template let’s now create MVC controller and generate views by scaffolding.

Add AngularJS: 

We need to add packages in our sample application. To do that go to Solution Explorer Right Click on Project > Manage NuGet Package

In Package Manager Search by typing “angularjs”, select package then click Install.

After installing “angularjs” package we need to reference it in our layout page, also we need to define app root using “ng-app” directive.

If you are new to angularjs, please get a basic overview on angularjs with MVC application from here: http://shashangka.com/2016/01/17/asp-net-mvc-5-with-angularjs-part-1

 

Install & Configure the Face API:

We need to add “Microsoft.ProjectOxford.Face” library in our sample application. Type and search like below screen then select and Install.

Web.Config: 

In application Web.Config add a new configuration setting in appSettings section with our previously generated API Key.

Finallly appSettings:

 

MVC Controller: This is where we are performing our main operation. First of all Get FaceServiceKey Value from web.config by ConfigurationManager.AppSettings.

Here in MVC Controller we have two main method to performing the face detection operation. One is HttpPost method, which is using for uploading the image file to folder and the other one is HttpGet method is using to get uploaded image and detecting faces by calling API Service.

Both methods are getting called from client script while uploading image to detect faces. Let’s get explained in steps.

Image Upload: This method is responsible for uploading images.

Image Detect: This method is responsible for detecting the faces from uploaded images.

This is the code snippet where the Face API is getting called to detect the face from uploaded image.

Create & Save Cropped Detected Face Images.

Method that Cropping Images according to face values.

Finally Full MVC Controller:

UI Helper:

MVC View:

Angular Controller:

Upload images to detect faces: Browse Image from local folder to upload and detect faces.

Mark faces in the image: Detected faces will mark with white rectangle.

List detected faces with face information: List and Separate the faces with detailed face information.

Summary: You have just seen how to call Face API to detect faces in an Image. Hope this will help to make application more smart and intelligent.

Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help to make it more understandable 🙂.

References:

 

2 Comments

Post a comment