How to Detect & Find Faces in an Image using Java

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:

And then adding one to the pom.xml dependency:

With installation complete, we can turn our attention to the controller & add our imports to the top of the file:

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:

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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

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