How to Detect & Find Faces in an Image using Java

Cloudmersive
2 min readSep 15, 2022

--

We can make more intelligent choices when editing an image if we understand the location of human faces within it. Perhaps most importantly, we can find out where to begin & end cropping the image (when we are doing so programmatically rather than within some photo editing application) so that it fits more smoothly within a particular space on a web page or prints out to a certain size specification (without resizing & losing quality).

Thankfully, there’s an API for that. Our Face Location API will return the exact location of all identifiable faces within an image for you, returning exact coordinates for each face as well as a total face count. You can take advantage of this API for free by registering a free account on our website (free accounts come with 800 API calls per month and zero additional commitments — awesome for small projects/startups) & then structuring your API call with the ready-to-run Java code examples provided below in this article.

Let’s first install the API client. We can do so by adding a reference to the pom.xml repository:

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

And then adding one to the pom.xml dependency:

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

With installation complete, we can turn our attention to the controller & add our imports to the top of the 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;

Lastly, we can call the function. Enter your free-tier API key into the authorization portion of the snippet (where indicated by the comments) and include your file path where shown within the function:

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 imageFile = new File("/path/to/inputfile"); // File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
FaceLocateResponse result = apiInstance.faceLocate(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling FaceApi#faceLocate");
e.printStackTrace();
}

That’s all the code you’ll need. For your reference, I’ve included a JSON response model:

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

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet