Hey Everyone,

Welcome again, in this blog post we are going to learn how we can leverage the Einstein Platform API to predict the image. OR we can also say Einstein prediction/classification API to predict the image.

In the previous post, we learned how we can extract the text from the image. If you have missed that please refer to this link to read about that.

We will be reusing some of the classes from the previous post which are given below so that you can quickly get to know

  1. JWT
  2. JWTBearerFlow
  3. HttpFormBuilder
  4. EinsteinAPIService

Let’s Create the Class which will be having 2 methods like the OCR class. The class is having 2 method and description is given below

identifyImageThis method is used to classify the image using the image URL
identifyImageBase64This method is used to classify the image using the image content in base64 form
parseResponseThis method is used to parse the response that we are getting from the Prediction API.

Below is the complete code the apex class

public class EinsteinPredictionService {

    public static String VISION_API = 'https://api.einstein.ai/v2/vision';
    public static String PREDICT = VISION_API + '/predict';
    public static String MODEL = 'GeneralImageClassifier';
    public static String LANGUAGE_API = 'https://api.einstein.ai/v2/language';
    public static String SENTIMENT = LANGUAGE_API + '/sentiment';
    public static String INTENT = LANGUAGE_API + '/intent';
    public static void identifyImage() {
        String sample = 'https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png';
        String result = EinsteinAPIService.predictImage(PREDICT, sample, MODEL, false);
    public static void identifyImageBase64() {
        List<ContentDocumentLink> contentLink = [SELECT ContentDocumentId, LinkedEntityId  
                                                 FROM ContentDocumentLink where LinkedEntityId ='0010o00002jzULuAAM'];
            ContentVersion content = [SELECT Title,VersionData FROM ContentVersion 
                                      where ContentDocumentId =: contentLink.get(0).ContentDocumentId 
                                      LIMIT 1];
            String sample = EncodingUtil.base64Encode(content.VersionData);
            String result = EinsteinAPIService.predictImage(PREDICT, sample, MODEL, true);
    private static void parseResponse(String result){
        EinsteinPredictionResponse response = (EinsteinPredictionResponse)
            				System.JSON.deserialize(result, EinsteinPredictionResponse.class);
        for(EinsteinPredictionResponse.Probabilities prob : response.probabilities){
            System.debug(System.LoggingLevel.DEBUG, prob.label+' '+prob.probability);

We are also using one helper class which is used to store the response from the einstein prediction API

public class EinsteinPredictionResponse {
    public Probabilities[] probabilities;
    public class Probabilities {
        public Double probability;
        public String label;

Here is the debug statement output

Below is the video output

Thanks for reading πŸ™‚ sharing is caring πŸ˜‰

If you have any questions, please feel free to connect with me or do a comment post.

Resources: –

  1. Blog Post
  2. Complete Code
  3. Einstein API
  4. JWT

Leave a Reply