What is Microsoft Assure Cognitive service :
Microsoft Assure Cognitive services provide a set of precooked cloud services as API for the end user who is into machine learning portfolio. As an end user, we don't want to take the pain in managing a full stack machine learning infrastructure, we can avail it with subscription basis. This helps developers add Intelligent features to their products with very ease.
The cognitive services are classified into Vision, Speech, Language, Knowledge, Search. The Emotion API is under Vision.
How to get trial keys :
There are keys for each service provides by them, here we need an Emotion API key and that can be found at here
We will get one pair of keys and in that one is a spare one.
Java Example
Since all assure service is cloud-based, the client's program is accessing it through internet URL's, for that, we have to use HTTP client's such Apache HttpComponents that you can get it from here
Below is the sample java code to get emotional features from an image.
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class EmotionDetection {
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
// NOTE: You must use the same region in your REST call as you used
// to obtain your subscription keys.
// For example, if you obtained your subscription keys from
// westcentralus, replace "westus" in the
// URL below with "westcentralus".
URIBuilder uriBuilder = new URIBuilder("https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize");
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
// Request headers. Replace the example key below with your valid
// subscription key.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "your-emotion-api-key-here");
// Request body. Replace the example URL below with the URL of the
// image you want to analyze.
StringEntity reqEntity = new StringEntity("{ \"url\": \"
http://i2.cdn.cnn.com/cnnnext/dam/assets/160504160857-03-donald-trump-0504-exlarge-169.jpg\" }");
request.setEntity(reqEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
The result is like below
[
{
"faceRectangle" {
"height" 62,
"left" 357,
"top" 227,
"width" 62
},
"scores" {
"anger" 1.55757982E-06,
"contempt" 3.73622584E-06,
"disgust" 3.22815913E-06,
"fear" 3.00894448E-11,
"happiness" 0.999942839,
"neutral" 4.861337E-05,
"sadness" 3.223123E-08,
"surprise" 9.294127E-09
}
},
{
"faceRectangle" {
"height" 58,
"left" 649,
"top" 73,
"width" 58
},
"scores" {
"anger" 1.661109E-06,
"contempt" 8.71511747E-06,
"disgust" 6.735908E-06,
"fear" 4.32732925E-08,
"happiness" 0.999927759,
"neutral" 5.25107862E-05,
"sadness" 5.01160343E-07,
"surprise" 2.07105654E-06
}
},
{
"faceRectangle" {
"height" 58,
"left" 493,
"top" 82,
"width" 58
},
"scores" {
"anger" 8.13568647E-07,
"contempt" 1.41677892E-05,
"disgust" 3.982801E-06,
"fear" 1.98274577E-07,
"happiness" 0.9993439,
"neutral" 0.0006029792,
"sadness" 3.24607172E-05,
"surprise" 1.49778668E-06
}
},
{
"faceRectangle" {
"height" 56,
"left" 249,
"top" 63,
"width" 56
},
"scores" {
"anger" 1.51057766E-05,
"contempt" 2.41940452E-05,
"disgust" 0.000192243941,
"fear" 1.22523124E-06,
"happiness" 0.996882558,
"neutral" 0.00285888766,
"sadness" 9.816682E-06,
"surprise" 1.595975E-05
}
},
{
"faceRectangle" {
"height" 56,
"left" 578,
"top" 41,
"width" 56
},
"scores" {
"anger" 6.05733157E-06,
"contempt" 1.90798182E-05,
"disgust" 5.012195E-05,
"fear" 7.27382456E-08,
"happiness" 0.9987048,
"neutral" 0.00121335278,
"sadness" 5.16508351E-07,
"surprise" 5.979184E-06
}
},
{
"faceRectangle" {
"height" 55,
"left" 163,
"top" 81,
"width" 55
},
"scores" {
"anger" 2.84686159E-08,
"contempt" 1.811132E-05,
"disgust" 6.6194545E-08,
"fear" 5.259717E-10,
"happiness" 0.9976553,
"neutral" 0.00232596323,
"sadness" 4.00665755E-07,
"surprise" 1.62385817E-07
}
},
{
"faceRectangle" {
"height" 54,
"left" 66,
"top" 89,
"width" 54
},
"scores" {
"anger" 2.30844165E-07,
"contempt" 5.02633657E-06,
"disgust" 4.72447255E-06,
"fear" 2.59101629E-09,
"happiness" 0.9996127,
"neutral" 0.000376438344,
"sadness" 3.15855345E-07,
"surprise" 5.60826436E-07
}
},
{
"faceRectangle" {
"height" 54,
"left" 326,
"top" 42,
"width" 54
},
"scores" {
"anger" 2.84579E-07,
"contempt" 7.520778E-06,
"disgust" 2.72833768E-06,
"fear" 4.44046327E-10,
"happiness" 0.999589145,
"neutral" 0.00039979635,
"sadness" 3.79227174E-07,
"surprise" 1.34581569E-07
}
},
{
"faceRectangle" {
"height" 50,
"left" 402,
"top" 80,
"width" 50
},
"scores" {
"anger" 8.794061E-08,
"contempt" 9.987322E-07,
"disgust" 2.38704865E-06,
"fear" 4.87888258E-07,
"happiness" 0.9999112,
"neutral" 5.163266E-05,
"sadness" 3.127918E-05,
"surprise" 1.932678E-06
}
},
{
"faceRectangle" {
"height" 49,
"left" 184,
"top" 251,
"width" 49
},
"scores" {
"anger" 1.166769E-10,
"contempt" 3.764107E-10,
"disgust" 1.72476755E-10,
"fear" 2.19937824E-13,
"happiness" 1.0,
"neutral" 8.727345E-09,
"sadness" 1.53888638E-11,
"surprise" 1.5125054E-10
}
}
]
The response value spans between -1 and 1, where -1 represents lower and 1 represents a higher degree.
To further drill down to the results programmatically, we can use JSON API such as gson
The code is available in git
Microsoft Assure Cognitive services provide a set of precooked cloud services as API for the end user who is into machine learning portfolio. As an end user, we don't want to take the pain in managing a full stack machine learning infrastructure, we can avail it with subscription basis. This helps developers add Intelligent features to their products with very ease.
The cognitive services are classified into Vision, Speech, Language, Knowledge, Search. The Emotion API is under Vision.
How to get trial keys :
There are keys for each service provides by them, here we need an Emotion API key and that can be found at here
We will get one pair of keys and in that one is a spare one.
Java Example
Since all assure service is cloud-based, the client's program is accessing it through internet URL's, for that, we have to use HTTP client's such Apache HttpComponents that you can get it from here
Below is the sample java code to get emotional features from an image.
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class EmotionDetection {
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
// NOTE: You must use the same region in your REST call as you used
// to obtain your subscription keys.
// For example, if you obtained your subscription keys from
// westcentralus, replace "westus" in the
// URL below with "westcentralus".
URIBuilder uriBuilder = new URIBuilder("https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize");
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
// Request headers. Replace the example key below with your valid
// subscription key.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "your-emotion-api-key-here");
// Request body. Replace the example URL below with the URL of the
// image you want to analyze.
StringEntity reqEntity = new StringEntity("{ \"url\": \"
http://i2.cdn.cnn.com/cnnnext/dam/assets/160504160857-03-donald-trump-0504-exlarge-169.jpg\" }");
request.setEntity(reqEntity);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
The result is like below
[
{
"faceRectangle" {
"height" 62,
"left" 357,
"top" 227,
"width" 62
},
"scores" {
"anger" 1.55757982E-06,
"contempt" 3.73622584E-06,
"disgust" 3.22815913E-06,
"fear" 3.00894448E-11,
"happiness" 0.999942839,
"neutral" 4.861337E-05,
"sadness" 3.223123E-08,
"surprise" 9.294127E-09
}
},
{
"faceRectangle" {
"height" 58,
"left" 649,
"top" 73,
"width" 58
},
"scores" {
"anger" 1.661109E-06,
"contempt" 8.71511747E-06,
"disgust" 6.735908E-06,
"fear" 4.32732925E-08,
"happiness" 0.999927759,
"neutral" 5.25107862E-05,
"sadness" 5.01160343E-07,
"surprise" 2.07105654E-06
}
},
{
"faceRectangle" {
"height" 58,
"left" 493,
"top" 82,
"width" 58
},
"scores" {
"anger" 8.13568647E-07,
"contempt" 1.41677892E-05,
"disgust" 3.982801E-06,
"fear" 1.98274577E-07,
"happiness" 0.9993439,
"neutral" 0.0006029792,
"sadness" 3.24607172E-05,
"surprise" 1.49778668E-06
}
},
{
"faceRectangle" {
"height" 56,
"left" 249,
"top" 63,
"width" 56
},
"scores" {
"anger" 1.51057766E-05,
"contempt" 2.41940452E-05,
"disgust" 0.000192243941,
"fear" 1.22523124E-06,
"happiness" 0.996882558,
"neutral" 0.00285888766,
"sadness" 9.816682E-06,
"surprise" 1.595975E-05
}
},
{
"faceRectangle" {
"height" 56,
"left" 578,
"top" 41,
"width" 56
},
"scores" {
"anger" 6.05733157E-06,
"contempt" 1.90798182E-05,
"disgust" 5.012195E-05,
"fear" 7.27382456E-08,
"happiness" 0.9987048,
"neutral" 0.00121335278,
"sadness" 5.16508351E-07,
"surprise" 5.979184E-06
}
},
{
"faceRectangle" {
"height" 55,
"left" 163,
"top" 81,
"width" 55
},
"scores" {
"anger" 2.84686159E-08,
"contempt" 1.811132E-05,
"disgust" 6.6194545E-08,
"fear" 5.259717E-10,
"happiness" 0.9976553,
"neutral" 0.00232596323,
"sadness" 4.00665755E-07,
"surprise" 1.62385817E-07
}
},
{
"faceRectangle" {
"height" 54,
"left" 66,
"top" 89,
"width" 54
},
"scores" {
"anger" 2.30844165E-07,
"contempt" 5.02633657E-06,
"disgust" 4.72447255E-06,
"fear" 2.59101629E-09,
"happiness" 0.9996127,
"neutral" 0.000376438344,
"sadness" 3.15855345E-07,
"surprise" 5.60826436E-07
}
},
{
"faceRectangle" {
"height" 54,
"left" 326,
"top" 42,
"width" 54
},
"scores" {
"anger" 2.84579E-07,
"contempt" 7.520778E-06,
"disgust" 2.72833768E-06,
"fear" 4.44046327E-10,
"happiness" 0.999589145,
"neutral" 0.00039979635,
"sadness" 3.79227174E-07,
"surprise" 1.34581569E-07
}
},
{
"faceRectangle" {
"height" 50,
"left" 402,
"top" 80,
"width" 50
},
"scores" {
"anger" 8.794061E-08,
"contempt" 9.987322E-07,
"disgust" 2.38704865E-06,
"fear" 4.87888258E-07,
"happiness" 0.9999112,
"neutral" 5.163266E-05,
"sadness" 3.127918E-05,
"surprise" 1.932678E-06
}
},
{
"faceRectangle" {
"height" 49,
"left" 184,
"top" 251,
"width" 49
},
"scores" {
"anger" 1.166769E-10,
"contempt" 3.764107E-10,
"disgust" 1.72476755E-10,
"fear" 2.19937824E-13,
"happiness" 1.0,
"neutral" 8.727345E-09,
"sadness" 1.53888638E-11,
"surprise" 1.5125054E-10
}
}
]
The response value spans between -1 and 1, where -1 represents lower and 1 represents a higher degree.
To further drill down to the results programmatically, we can use JSON API such as gson
The code is available in git
Comments