How to detect people in a photo in Java

Cloudmersive
2 min readJan 21, 2020

--

Any good photo app needs to be able to locate people. Setting this functionality up from scratch is quite a process. Luckily, there’s a very quick and easy way around it, which is the subject of today’s post. We will be using an API to get set up in just a couple minutes.

First we need to set some references in our Maven POM file. This will allow Jitpack to dynamically compile our API library.

Repository reference:

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

Dependency reference:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v2.75</version>
</dependency>
</dependencies>

Now we will use the following code to call recognizeDetectPeople and provide it with an image to process:

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

That’s it! It’s just that easy. Below is an example response for this photo:

{
"Successful": true,
"Objects": [
{
"ObjectClassName": "person",
"Height": 300,
"Width": 173,
"Score": 0.8973171710968018,
"X": 3,
"Y": 94
},
{
"ObjectClassName": "person",
"Height": 305,
"Width": 477,
"Score": 0.8843736052513123,
"X": 356,
"Y": 119
},
{
"ObjectClassName": "person",
"Height": 319,
"Width": 408,
"Score": 0.8814287185668945,
"X": 231,
"Y": 105
},
{
"ObjectClassName": "person",
"Height": 311,
"Width": 251,
"Score": 0.799172580242157,
"X": 141,
"Y": 107
}
],
"ObjectCount": 4
}

For each person, we are given a location, dimensions, and a score of how likely it is in fact a person. Note that this function works best when each the faces of each person are visible and undistorted.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet