Image Classification using Einstein Platform API

on

|

views

and

comments

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);
        parseResponse(result);
    }
    
    public static void identifyImageBase64() {
        List<ContentDocumentLink> contentLink = [SELECT ContentDocumentId, LinkedEntityId  
                                                 FROM ContentDocumentLink where LinkedEntityId ='0010o00002jzULuAAM'];
        if(!contentLink.isEmpty()){
            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);
            parseResponse(result);
        }
    }
    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
Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential