I need to compare 2 face images in java and confidence value of them. I'm trying to learn reading the official documentation provided by face++, but it's a little bit poor in my opinion. I need to use the face compare API here: https://console.faceplusplus.com/documents/5679308. I don't understand how to build the url to send the request. There is these piece of code at the end of the page:
curl -X POST "https://api-us.faceplusplus.com/facepp/v3/compare" \
-F "api_key=<api_key>" \
-F "api_secret=<api_secret>" \
-F "face_token1=c2fc0ad7c8da3af5a34b9c70ff764da0" \
-F "face_token2=ad248a809408b6320485ab4de13fe6a9"
Now, the only code I see in their documentation is this:
https://console.faceplusplus.com/documents/7078069
But it doesn't work, or at least, this is not for the "face compare".
I need to create and to get the request for the face compare API(first link at the begin). I can't find any java example on how to do that. I'm doing it in java
Using the same sample code provided in the documentation, all you need to do is to change the main method like so:
public static void main(String[] args) throws Exception{
// Create a new file object for the first file and get bytes from file
File file = new File("C:\\Users\\ihene\\Desktop\\my-photo.jpg");
byte[] buff1 = getBytesFromFile(file);
// Create a new file object for the second file and get bytes from file
File file2 = new File("C:\\Users\\ihene\\Desktop\\esan-caleb.jpg");
byte[] buff2 = getBytesFromFile(file2);
// Data needed to use the Face++ Compare API
String url = "https://api-us.faceplusplus.com/facepp/v3/compare";
HashMap<String, String> map = new HashMap<>();
HashMap<String, byte[]> byteMap = new HashMap<>();
map.put("api_key", "dam4ZdTkSsZOUAiR4oQpP3DRnjEz1fcD");
map.put("api_secret", "0MOCfpum1Lec06EMOzuJPOEa_EhM4Ttg");
byteMap.put("image_file1", buff1);
byteMap.put("image_file2", buff2);
try {
// Connecting and retrieving the JSON results
byte[] bacd = post(url, map, byteMap);
String jsonStr = new String(bacd);
// Parse the JSON and get the confidence value
JSONObject obj = new JSONObject(jsonStr);
double confidence = obj.getDouble("confidence");
System.out.println(confidence);
} catch (Exception e) {
e.printStackTrace();
}
}
Simply replace PATH_TO_FILE_1
, PATH_TO_FILE_2
, YOUR_API_KEY
, and YOUR_API_SECRET
with the appropriate values. It should work.
I have tested the API using two images with the face belonging to the same person and two other images with different faces. Here's the output I got for the former:
{"faces1": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "1ee9de6d362b0c8c1bf240a70fbf3eac"}], "faces2": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "0a7c539f3603aa744ee18c65acc224a8"}], "time_used": 531, "thresholds": {"1e-3": 62.327, "1e-5": 73.975, "1e-4": 69.101}, "confidence": 97.389, "image_id2": "Fc64vrBtETVmP2cS+BoW/Q==", "image_id1": "Fc64vrBtETVmP2cS+BoW/Q==", "request_id": "1569184566,0cc04f1a-1495-4316-928c-1efe7d6836dc"}
Notice the confidence here is 97.389. The documentation says that "higher confidence indicates higher possibility that two faces belong to same person." So, this in line with what we would expect. For the latter images with different faces, here's the output:
{"faces1": [{"face_rectangle": {"width": 156, "top": 210, "left": 142, "height": 156}, "face_token": "c7d8561f0235a99d9b060750c7a9c3c7"}], "faces2": [{"face_rectangle": {"width": 75, "top": 44, "left": 53, "height": 75}, "face_token": "ad87092cb593128c015c2e2221b962f2"}], "time_used": 533, "thresholds": {"1e-3": 62.327, "1e-5": 73.975, "1e-4": 69.101}, "confidence": 53.993, "image_id2": "MiZOf00hrq7OmAxc3+n7sg==", "image_id1": "Fc64vrBtETVmP2cS+BoW/Q==", "request_id": "1569185082,0bfd0a0f-a376-4dac-92bd-924c34ef76ed"}
Here, confidence is 53.933.