Using Kairos API for detecting various Face landmarks like nose,eyes,lips etc

In previous article I had shown how to access Kairos’s APIs for face detection. In the example shown in the article you will only be able to fetch 3 Facial landmarks – Left eye, right eye and chin.
You can read the article here : Kairos Face detection

But if you are interested in more landmarks we need to hit their emotion API. Here is the sample code :

package images;
 
import java.io.StringReader;
import java.net.URI;
import java.util.Map.Entry;
 
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonValue;
 
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
 
public class KairosFaceDetection {
	public static void main(String[] args) throws Exception {
 
		HttpClient httpclient = new DefaultHttpClient();
 
		try {
			String url = "http://api.kairos.com/v2/media?source=http://media.kairos.com/test1.jpg&landmarks=1";
			URIBuilder builder = new URIBuilder(url);
 
			URI uri = builder.build();
			HttpPost request = new HttpPost(uri);
 
			// Request headers - replace this example key with your valid
			// subscription key.
			request.setHeader("Content-Type", "application/json");
			request.setHeader("app_id", "APP_ID");
			request.setHeader("app_key", "APP_KEY");
 
 
 
			HttpResponse response = null;
			HttpEntity entity = null;
			response = httpclient.execute(request);
			entity = response.getEntity();
 
			if (entity != null || true) {
				String id = "";
				String resp = "";
				resp = EntityUtils.toString(entity);
				id = Json.createReader(new StringReader(resp)).readObject()
						.getString("id");
 
				url = "http://api.kairos.com/v2/media/" + id;
				builder = new URIBuilder(url);
 
				uri = builder.build();
				HttpGet grequest = new HttpGet(uri);
				grequest.setHeader("Content-Type", "application/json");
				grequest.setHeader("app_id", "APP_ID");
				grequest.setHeader("app_key",
						"APP_KEY");
				response = httpclient.execute(grequest);
				entity = response.getEntity();
 
				if (entity != null) {
					resp = EntityUtils.toString(entity);
 
					JsonObject face = Json.createReader(new StringReader(resp))
							.readObject().getJsonArray("frames")
							.getJsonObject(0).getJsonArray("people")
							.getJsonObject(0).getJsonObject("face");
					JsonArray landmarksjson = Json
							.createReader(new StringReader(resp)).readObject()
							.getJsonArray("frames").getJsonObject(0)
							.getJsonArray("people").getJsonObject(0)
							.getJsonArray("landmarks");
					String[] landmarks = { "noseTipTop", "lowerLipTopCenter",
							"leftEyeBrowOuterLeft", "leftEyeBrowOuterRight",
							"righteEyeBrowOuterLeft", "rightEyeBrowOuterRight",
							"leftEyeCornerLeft", "rightEyeCornerLeft" };
					StringBuilder leftDim = new StringBuilder();
					for (String facet : landmarks) {
						for(int f=0;f<landmarksjson.size();f++){
							JsonObject ff = landmarksjson.getJsonObject(f);
							for(Entry<String, JsonValue> e : ff.entrySet()){
								if(e.getKey().equals(facet)){
									leftDim.append(((JsonObject)e.getValue()).getInt("x") + "," +((JsonObject)e.getValue()).getInt("y") + " ");
								}
							}
						}
					}
					System.out.println(leftDim);
 
					int facex = face.getInt("x");
					int facey = face.getInt("y");
					int facewid = face.getInt("width");
					int facehei = face.getInt("height");
 
				}
 
				// System.out.println(text);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
 
	private static void addFaceDim(javax.json.JsonObject faceLandmark,
			String facet, StringBuilder sb) {
		int xa = faceLandmark.getJsonObject(facet).getInt("x");
		int ya = faceLandmark.getJsonObject(facet).getInt("y");
		sb.append(xa).append(",").append(ya).append(" ");
	}
}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *