How to Detect People (Including Locations) In an Image using Java

Photos often contain one, two, or even a half-dozen human subjects at a time. In a perfect world, we would remember exactly who and how many people were in all the photos we took — but that’s unrealistic, especially when there might be hundreds of photos in any given album. Thankfully, there are other ways to get useful metadata about an image’s subjects: for one, you can use our Detect People API, which will identify the position & size of human people within an image and provide exact pixel coordinates (along with a confidence score indicating the degree to which the operation feels it was successful). This will include people who may be facing away or turned at odd angles, too. Including this API in your image processing operation is a great way to get more data out of your photos and — in some cases — help prevent blindly cropping people out of your field of view.

For your reference, I’ve included a sneak-peek at an example response body for this API. Each identified person will receive a full ObjectClassName string along with the locational data & confidence score:

"Successful": true,
"Objects": [
"ObjectClassName": "string",
"Height": 0,
"Width": 0,
"Score": 0,
"X": 0,
"Y": 0
"ObjectCount": 0

Below, I’ll demonstrate how you can structure your API call using ready-to-run Java code examples from our API Console. Just copy & paste, and make sure you have a Cloudmersive API key to authenticate the service (you can get one for free easily by visiting our website and registering a free account).

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


And then adding one to the dependency in pom.xml:


Now we can call the function. First include the imports at the top of your file, and then supply your API key & image file path:

// 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.RecognizeApi;
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)
RecognizeApi apiInstance = new RecognizeApi();
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 {
ObjectDetectionResult result = apiInstance.recognizeDetectPeople(imageFile);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeDetectPeople");

Just like that, you’re all done — no more code required.



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



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