Introduction to Salesforce Einstein Vision
Introduction
Artificial Intelligence (AI) is already
part of our lives. Whenever you pick up your smartphone, you’re already seeing
AI in action—from tailored recommendations to relevant search results. With
Einstein Vision, you can harness the power of image recognition to build
AI-powered apps fast. Without a data science degree!
Using the Einstein Vision API, you can
train deep learning models to recognize and classify images. A model is a
machine learning construct used to solve a classification problem. The model “learns”
from data instead of from explicit rules. In this topic, the terms model and
classifier mean the same thing.
You design a model by creating a dataset
and then defining labels and providing examples that belong to these labels.
When you train the dataset, the system determines the commonalities and
differences between the various labels and generalizes the characteristics that
define each label. When you send an image to the model, the model predicts
which class that image falls into based on the classes specified in your
training dataset.
You may find some of these terms unfamiliar, so if
you’re new to AI and deep learning, fear not! This topic is a great way to
get started, because I am using a model that’s already been built. Plus, this topic takes you through each step of the way to send in an image and get back
a prediction.
Einstein Vision lets you use pre-trained
classifiers or train your own custom classifiers to solve unique use cases.
Creating a custom classifier requires a lot of data. So in this blog article, you
access the pre-built general image classifier from your Salesforce org.
First,
you create Apex classes that call the API. Then you create a Visualforce page
to tie it all together. Finally, you test all your work by sending this image
of a tree frog into the classifier. The classifier returns a prediction of what
the image contains.
Pre-requisite :-
·
A GitHub account. If you don’t have an account, sign up here.
·
Git installed. See this
page for information
on installing and configuring Git.
You can install it for use from the
command line or you can install the desktop client. This blog article uses the Git
command line commands.
Get an Einstein Platform Account
Before you can access the Einstein Vision API, you
first create an account and download your key. You can skip this step if you
already have an account and key.
When you sign up for an account, you’re prompted to
log in to your Salesforce org. After you log in, you’re prompted to allow the
sign up page to access information in your Salesforce org. This lets the sign
up page retrieve some basic information such as your ID, profile, email,
address, and phone number. After your account is created, you can download your
key.
Note:- Be sure you’re logged out of Salesforce before you go
through the steps to get an account.
2. Click Sign Up Using
Salesforce.
3. On the Salesforce login page, type your
Trailhead Playground username and password and click Log In.
4. Click Allow so the page can access basic information, such as your
email address, and perform requests.
5. On the activation page click Download Key to save the key locally.
Upload Your Key
The .pem file you downloaded contains your key. You
upload your key to Salesforce Files so that the Apex controller class can
access it.
1. In Salesforce, click Files.
2. Click Upload File.
3. Navigate
to the directory where you saved the einstein_platform.pem file, select the file, and click Open.
You should see the key file in the list of files owned
by you.
Get the Code
All the code you need for this project is available on GitHub. After you
download the code, you just cut and paste.
These steps show
you how to download the code to your local machine using git commands from the
command line. You can also access the code from GitHub using your
browser.
1. Open a command-line
window and navigate to the directory where you want to download the code.
2. Issue this command
to download the Apex classes that handle the JWT token processing.
- git clone https://github.com/salesforceidentity/jwt
3. Issue this command
to download the code for the Apex classes and the Visualforce page. These code
elements call the Einstein Vision API.
|
Before your Apex code can call the Einstein Vision endpoint, you must
register the endpoint URL in the Remote Site Settings page.
1. Log in to
Salesforce.
2. From Setup, enter Remote Site in the Quick
Find box, then select Remote Site Settings.
3. Click New
Remote Site.
4. For Remote
Site Name, type PVS_API.
5. For Remote
Site URL, type https://api.metamind.io.
6. Click Save.
Your page should
look something like this.
Create the JWT Apex
Classes
Now you create the JWT classes that handle security and create the JWT
token.
In these steps, you
copy code from the jwt GitHub repo that you cloned and paste it into
Salesforce.
1. On your computer,
find the directory that contains the jwt repo.
2. In Salesforce, from
Setup, enter Apex Classes in the Quick Find box,
then select Apex Classes.
3. Click New.
4. To create the JWT Apex
class, copy all the code from JWT.apex to the Apex Class tab.
5. Click Save.
6. To return to the
class list, from Setup, click Apex Classes.
7. Click New.
8. To create the JWTBearerFlow Apex
class, copy all the code from JWTBearer.apex to the Apex
Class tab and click Save.
9. To return to the class
list, from Setup, click Apex Classes
Create the
Einstein Vision Apex Classes
In this step, you create the classes that create a
Web request and call the API endpoint.
You copy code from the apex-utils GitHub
repo that you cloned locally and paste it into Salesforce. You create three
classes:
·
HttpFormBuilder—Called
by the Vision class to build the REST API request.
·
Vision—Called
by the VisionController class and contains the code that builds the
request and calls the API.
·
VisionController—Custom
controller that initiates the API call and returns the results to the
Visualforce page.
1. Open a new window and navigate to the directory
that contains the apex-utils repo.
2. In Salesforce, click New.
3. To create the HttpFormBuilder Apex class,
copy all the code from HttpFormBuilder.apex to the Apex Class tab
and click Save.
4. To return to the class list, from Setup, click Apex
Classes.
5. Click New.
6. To create the Vision Apex class, copy all
the code from Vision.apex to the Apex Class tab and
click Save.
7. To return to the class list, from Setup, click Apex
Classes.
8. Click New.
9. To create the VisionController Apex
class, copy only the VisionController code from README.md to
the Apex Class tab. The class is all the code from public
class VisionController { to the closing brace }.
10. Update the jwt.sub placeholder text of yourname@example.com with your email address. Use your email address that's contained in the Salesforce org you logged in to when you created an account.
Your VisionController should now look
something like the following.
11. Click Save.
Create the
Visualforce Page
Now you create the Visualforce page that calls the classes that you just
created and displays the prediction results.
1. In Salesforce, from
Setup, enter Visualforce in the Quick Find box,
then select Visualforce Pages.
2. Click New.
3. For Label,
type Predict.
The Name field automatically populates based on this entry.
4. From README.md,
copy all of the code from to and
paste it in the Visualforce Markup tab.
5. Click Save.
Test the
Visualforce Page
The time has come to test out all your hard work.
To refresh your memory, you run the code you created, and that code calls the
Einstein Vision API. The code sends the image of the tree frog to the general
image classifier (by referencing the image URL), and returns a prediction.
1. From the Visualforce page you created, click Preview.
Your page shows the prediction results from the general image classifier, and
the classifier is pretty sure it’s a picture of a tree frog.
Congratulations! You just called the Einstein
Vision API, sent an image into an existing model, and received a prediction
back from the model.














Comments
Post a Comment