How to Detect Objects — Including Types & Locations — In an Image using Java

The more we know about our images, the more easily we can search them in our own databases & make informed changes to them as needed. With our Object Detection API, you can automatically identify objects within your photo and return useful information about them including the object type (this determination is accompanies by a confidence score ranging between 0.0–1.0) & object location (this is expressed in terms of pixel coordinates & approximate measurements of the object). The response body for any given image will look something like the below, with one ObjectClassName string provided for every identifiable object:

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

This API is completely free to use — you just need to register a free account on our website, and supply your API key within the ready-to-run Java code examples provided below. In the remainder of this article, I’ll walk you through how to structure your API call using said Java code examples; all you need to do is copy & paste.

Our first step is to install the API client. Let’s add a reference to the repository in pom.xml:

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

And then let’s add one to the pom.xml dependency:

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

With installation complete, let’s turn our attention to the controller; first include the imports at the top of your file, and then call the function:

// 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)
//Apikey.setApiKeyPrefix("Token");
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.recognizeDetectObjects(imageFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling RecognizeApi#recognizeDetectObjects");
e.printStackTrace();
}

With that, you’re all finished — 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
Cloudmersive

Cloudmersive

436 Followers

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