Scroll Top

Consuming Microsoft Azure Cognitive Services in Xamarin.Android Application

Overview

Customer service rating plays an important role in determining user satisfaction with the company’s service. There are several techniques are already in place in the market to determine the same. In this article, we will explore how to capture customer’s emotion and which will help in calculating the service rating.

Microsoft provides many different cognitive services which we can consume in applications. In this write-up, we’ll use One such cognitive service i.e., Emotion. Using Emotion API we can analyze faces and detect a range of feelings of the user to help rate the service. The app uses the camera to get a photo of the user’s face and calculates a rating for the service based on happiness. The more you smile, the better you rate the service.

Emotions are understood to be cross-culturally and universally communicated with particular facial expressions. The emotions detected by Microsoft Azure Cognitive Service emotion API are anger, contempt, disgust, fear, happiness, neutral, sadness, and surprise.

In this article, you will develop a Xamarin.Android application and integrate it with One of the Cognitive Services API i.e., Emotion API.

Here are the steps you need to follow in order to make this happen.

Steps

This article is inspired by the mini-hacks of Microsoft cognitive services, written by Mike James. In this article, we play around with this code and see how it works in reality.

  • Generating Emotion API Key
  • Implementation

Generating Emotion API Key

The first step here is to subscribe to Emotion API and get an API key. The same API key will be used in Xamarin.Android application. Please follow the below steps to get the API key.

                     

  • After login, you will be redirected to the Azure portal and click on + icon on left sidebar and click on Intelligence + analytics and then on Cognitive Service APIs as shown below.

                     

  • Click on the create button as shown below.

                     

  • Fill in the required fields data as shown in below picture to create Emotion API , and click on create.

                     

  • Now you have successfully generated Emotion API keys in Azure portal and you can find those values by clicking on the All Resource icon on the left sidebar and select the API (with the name you have given) and then click on the Keys as shown below. Copy the anyone Key value, we will make use of it later.

                      

Implementation

  • Add the following Nuget packages to project in the specified order.
    • Microsoft.Bcl.Build – This package provides build infrastructure components so that projects referencing specific Microsoft packages can successfully build.
    • Microsoft.ProjectOxford.Emotion – This package contains Microsoft Cognitive Services Emotion Recognition Client Library.
  • Open MainActivity.cs you can see the click event for RateAppButton in OnCreate() replace that event with the following line of code.

  • Create a new layout file inside Resource? layout with name rateus_layout.axml and paste the below code.

  • Create a class with name BitmapHelper.cs and replace its source code with the below code snippet:

Credit: https://github.com/xamarin/mini-hacks/blob/master/microsoft-cognitive-services/Android.md

  • Above class ensures that images will be sent to emotion API in portrait mode.
  • Create a new activity with the name as RatingUs_Activity.cs and paste the following code to Integrating camera with your application for the button click.

Credit:https://developer.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

  • Send the captured image as a stream to Emotion API to get the happiness result. To achieve the same implement the OnActivityResult() inside the RatingUs_Activity.cs

Credit:https://github.com/xamarin/mini-hacks/blob/master/microsoft-cognitive-services/Android.md

NOTE:  We are resizing image size to below 4MB because Emotion API will not process images > 4MB and treats them as Invalid Image.

  • Create a class with the name Emotion_Helper.cs and replace with the following code.         

Credit:https://github.com/xamarin/mini-hacks/blob/master/microsoft-cognitive-services/Android.md

  • Make sure you assign the value of Emotion API Key generated in the first step to AzureKey property as highlighted in the above code.
  • Right click on the Android Project and select Properties to grant the following  Permissions:
  • CAMERA

      

  • INTERNET

  • WRITE_EXTERNAL_STORAGE    

That’s it! Now build and run the application by clicking on F5.

Summary

This article explained how to consume Microsoft Azure Cognitive Services in Xamarin.Android application. We also discussed specific cognitive service i.e., consumption emotion API and how we can use this for rating service in B2C applications.

References

  • For more information on “Getting Started with Xamarin and Azure Webinar by Mike James” refer to this link and this link
  • For more information on Android Mini Hacks using Azure – refer to this link
  • For more information on – Integrating Camera with Xamarin.Android refer to this link
  • For more information on – Microsoft Cognitive Services refer to this link.

Related Posts

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.