-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFace1.0.java
More file actions
91 lines (77 loc) · 3.32 KB
/
Face1.0.java
File metadata and controls
91 lines (77 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
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.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Include these library jars (or newer +), add to a lib folder:
* commons-logging-4.0.6+
* httpclient-4.5.3+
* httpcore-4.4.13+
* json-20190722+
*
* To compile and run from command line:
* javac Main.java -cp .;lib\*
* java -cp .;lib\* Main
*/
public class Main
{
// Add your Face subscription key and endpoint to your environment variables.
private static String subscriptionKey = System.getenv("FACE_SUBSCRIPTION_KEY");
private static String baseUrl = System.getenv("FACE_ENDPOINT");
public static final String endpoint= baseUrl + "/face/v1.0/detect";
private static String image = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/test-image-person-group.jpg";
public static void main(String[] args)
{
HttpClient httpclient = new DefaultHttpClient();
try
{
URIBuilder builder = new URIBuilder(endpoint);
// Request parameters. All of them are optional.
builder.setParameter("returnFaceId", "true");
builder.setParameter("returnFaceLandmarks", "false");
builder.setParameter("returnFaceAttributes", "age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise");
// Prepare the URI for the REST API call.
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request body.
StringEntity reqEntity = new StringEntity("{\"url\":\"" + image + "\"}");
request.setEntity(reqEntity);
// Execute the REST API call and get the response entity.
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
// Format and display the JSON response.
System.out.println("REST Response:\n");
String jsonString = EntityUtils.toString(entity).trim();
if (jsonString.charAt(0) == '[') {
JSONArray jsonArray = new JSONArray(jsonString);
System.out.println(jsonArray.toString(2));
}
else if (jsonString.charAt(0) == '{') {
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject.toString(2));
} else {
System.out.println(jsonString);
}
}
}
catch (Exception e)
{
// Display error message.
System.out.println(e.getMessage());
}
}
}