Using Faceplusplus APIs for face and emotion detection in Java

I have been using Face detection APIs a lot recently. I have already blogged about Using Kairos API for face detection and Microsoft Face APIs.

Microsoft APIs had a limit of 30k calls under free plan. So I decided to explore more options available in the market as 30k was a bit low for me. Recently I came across Face plus plus. It seems they are some chinese company. Good thing is under free plan they give unlimited usage of APIs. I have started using their APIs though I will admit for small images it is not able to detect the faces (microsoft API was able to detect).

Here is the sample code to use their API. You can get the access keys by signing up in their website

package images;
 
import java.io.StringReader;
import java.net.URI;
import java.util.Map.Entry;
 
import javax.json.Json;
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.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
 
public class FaceplusImageDetection {
	public static void main(String[] args) throws Exception {
 
		HttpClient httpclient = new DefaultHttpClient();
 
		try {
			URIBuilder builder = new URIBuilder(
					"https://api-us.faceplusplus.com/facepp/v3/detect");
 
			builder.setParameter("api_key", "YOURAPIKEY");
			builder.setParameter("api_secret", "YOURAPISECRET");
			builder.setParameter("image_url", "http://CreateUrApp.com/createurapp/outputs/img1/313079019113132celeb-face-mergecrop1.jpg");
			builder.setParameter("return_landmark", "1");
 
			URI uri = builder.build();
			HttpPost request = new HttpPost(uri);
 
 
			HttpResponse response = httpclient.execute(request);
			HttpEntity entity = response.getEntity();
 
			if (entity != null) {
				String resp = EntityUtils.toString(entity);
				JsonObject text = Json.createReader(
						new StringReader(resp)).readObject().getJsonArray("faces").getJsonObject(0).getJsonObject("face_rectangle");
				String[] facets = { "mouth_upper_lip_left_contour2",
						"mouth_upper_lip_top",
						"mouth_upper_lip_left_contour1",
						"left_eye_upper_left_quarter",
						"left_eyebrow_lower_middle",
						"mouth_upper_lip_left_contour3",
						"left_eyebrow_lower_left_quarter",
						"right_eyebrow_lower_left_quarter",
						"right_eye_pupil",
						"mouth_lower_lip_right_contour1",
						"mouth_lower_lip_left_contour2",
						"mouth_lower_lip_right_contour3",
						"mouth_lower_lip_right_contour2", "contour_chin",
						"contour_left9", "left_eye_lower_right_quarter",
						"mouth_lower_lip_top",
						"right_eyebrow_upper_middle",
						"right_eyebrow_left_corner",
						"right_eye_lower_right_quarter",
						"right_eye_bottom", "contour_left7",
						"contour_left6", "contour_left5", "contour_left4",
						"contour_left3", "contour_left2", "contour_left1",
						"left_eye_lower_left_quarter", "contour_right1",
						"contour_right3", "contour_right2",
						"mouth_left_corner", "contour_right4",
						"contour_right7", "left_eyebrow_left_corner",
						"nose_right", "nose_tip", "contour_right5",
						"nose_contour_lower_middle", "right_eye_top",
						"mouth_lower_lip_left_contour3",
						"right_eye_right_corner",
						"mouth_upper_lip_right_contour1",
						"mouth_upper_lip_right_contour2",
						"right_eyebrow_lower_right_quarter",
						"left_eye_left_corner", "mouth_right_corner",
						"right_eye_lower_left_quarter",
						"left_eyebrow_right_corner",
						"left_eyebrow_lower_right_quarter",
						"right_eye_center", "left_eye_pupil", "nose_left",
						"mouth_lower_lip_left_contour1",
						"left_eye_upper_right_quarter",
						"right_eyebrow_lower_middle", "left_eye_center",
						"contour_left8", "contour_right9",
						"right_eye_left_corner",
						"left_eyebrow_upper_left_quarter",
						"left_eye_bottom", "left_eye_right_corner",
						"right_eyebrow_upper_left_quarter",
						"contour_right8", "right_eyebrow_right_corner",
						"right_eye_upper_left_quarter",
						"left_eyebrow_upper_middle",
						"right_eyebrow_upper_right_quarter",
						"nose_contour_left1", "nose_contour_left2",
						"nose_contour_left3", "nose_contour_right1",
						"nose_contour_right2", "mouth_lower_lip_bottom",
						"contour_right6", "nose_contour_right3",
						"left_eye_top", "mouth_upper_lip_right_contour3",
						"left_eyebrow_upper_right_quarter",
						"right_eye_upper_right_quarter",
						"mouth_upper_lip_bottom", };
				StringBuilder leftDim = new StringBuilder();
				javax.json.JsonObject faceLandmark = Json
						.createReader(new StringReader(resp)).readObject()
						.getJsonArray("faces").getJsonObject(0)
						.getJsonObject("landmark");
				for (String facet : facets) {
					addFaceDim(faceLandmark, facet, leftDim);
				}
				System.out.println(leftDim);
			}
		} 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 *