How to Compare and Match Faces from Different Images using Java

Cloudmersive
2 min readSep 15, 2022

There are a multitude of applications for face matching technology, from its easily imaginable uses in law enforcement to its considerably more mundane use in securing our laptops and phones. Whatever your use case may be, we have an API to help you incorporate facial recognition into your application. Our Face Matching API will reference an input image (containing one or more faces) against an existing image (containing exactly one face) and determine the degree to which those faces are a match. The API response will identify the exact location of the matching face within the input image and provide a MatchScore indicating the degree to which the faces were similar. MatchScore values range from 0.0–1.0 with values north of 0.7 indicating a match.

To use this API for free, all you need to do is register a free account on our website (this account comes with a limit of 800 API calls per month; intended for startups & small projects) and structure your API call using the Java code examples provided below.

We can begin API client installation by first adding a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then adding a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation finished, we can switch over to our controller and add the imports to the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.FaceApi;

Last but not least, we can now call the function. Our first step is to authenticate the API within the proper snippet (shown by the comments), and then configure our input & matchFace file paths:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
FaceApi apiInstance = new FaceApi();
File inputImage = new File("/path/to/inputfile"); // File | Image file to perform the operation on; this image can contain one or more faces which will be matched against face provided in the second image. Common file formats such as PNG, JPEG are supported.
File matchFace = new File("/path/to/inputfile"); // File | Image of a single face to compare and match against.
try {
FaceCompareResponse result = apiInstance.faceCompare(inputImage, matchFace);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FaceApi#faceCompare");
e.printStackTrace();
}

Now you’re all done & can test the API at will. I’ve included an example response model below for your reference:

{
"Successful": true,
"Faces": [
{
"LeftX": 0,
"TopY": 0,
"RightX": 0,
"BottomY": 0,
"HighConfidenceMatch": true,
"MatchScore": 0
}
],
"FaceCount": 0,
"ErrorDetails": "string"
}

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.