Image Recognition in Java

Hello everyone, In this post I am sharing about, How to perform Image Recognition in Java using Clarifai API. This example were tested with environment and same has been shared in the post.

About Clarifai API

The Clarifai is a Image & Vdieo Recognition API. That can be called via the webservice provided, to do that you should have the Client ID and Client Secret Key. That can be generated after creating an account in Clarifai. For more details you can visit https://www.clarifai.com/

Project Structure

Image Recognition in Java using Clarifai API example

Dependencies(JAR) List
  • core-2.2.0.jar
  • okhttp-3.4.2.jar
  • okio-1.11.0.jar
  • gson-2.8.0.jar

You can always download above JAR`s or Dependencies from Maven Repository or you can Downalod the complete project here.

ImageRecognizer.java

package com.javatraineronline;

import java.util.ArrayList;
import java.util.List;

import clarifai2.api.ClarifaiBuilder;
import clarifai2.api.ClarifaiClient;
import clarifai2.dto.input.ClarifaiInput;
import clarifai2.dto.input.image.ClarifaiImage;
import clarifai2.dto.model.output.ClarifaiOutput;
import clarifai2.dto.prediction.Concept;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ImageRecognizer {
	
	// Provide your Client ID
	private final static String CLIENT_ID = "Your Client ID"; 
	
	// Provider Your Client Secret Key
	private final static String CLIENT_SECRET_KEY = "Your Client Secret Key";
	
	public static List<String> recognize(String imageUrl) {
		
		// Defining List Object
		List<String> resultList = new ArrayList<String>();
		
		if(imageUrl != null && !imageUrl.isEmpty()) {
			
			final ClarifaiClient client = new ClarifaiBuilder(CLIENT_ID, CLIENT_SECRET_KEY).buildSync();
			
			final List<ClarifaiOutput<Concept>> predictionResults =
			          client.getDefaultModels().generalModel() // You can also do client.getModelByID("id") to get custom models
			              	.predict()
			              	.withInputs(
			              				ClarifaiInput.forImage(ClarifaiImage.of(imageUrl))
			              			)
			              	.executeSync()
			              	.get();
			
			if (predictionResults != null && predictionResults.size() > 0) {

				// Prediction List Iteration
				for (int i = 0; i < predictionResults.size(); i++) {

					ClarifaiOutput<Concept> clarifaiOutput = predictionResults.get(i);
					
					List<Concept> concepts = clarifaiOutput.data();

					if(concepts != null && concepts.size() > 0) {
						for (int j = 0; j < concepts.size(); j++) {

							resultList.add(concepts.get(j).name());
						}
					}
				}
			}

		}	
		return resultList;
	}

	public static void main(String[] args) {

		// You can change the Image URL accordingly.
		String imageUrl = "http://www.dream-wallpaper.com/free-wallpaper/nature-wallpaper/dream-homes-1-wallpaper/1280x800/free-wallpaper-9.jpg";
	
		// List of Recognized Result from Image
		List<String> resultList = ImageRecognizer.recognize(imageUrl);
		
		// Iteration of Result
		for(String result : resultList) {
			
			System.out.println(result);
		}	
	}
}

Input Image

Image Recognition in Java using Clarifai API example

Predicted Text

no person
tree
water
landscape
nature
summer
outdoors
grass
sky
travel
wood
lake
lawn
scenic
park
house
flower
daylight
garden
river

Download Example Here

Download from GitHub

References

1. https://www.clarifai.com/
2. https://developer.clarifai.com/guide/
3. https://github.com/Clarifai/clarifai-java