Home Salesforce Extract License Plate Number from Image In Salesforce

Extract License Plate Number from Image In Salesforce

by Dhanik Lal Sahni

Last week I got a request about extracting the vehicle license plate numbers. Requester wanted to extract the license plate number from the image. I was not sure that we have a specific API for this request. After analysis, I found License Plate Number APIs which can extract plate numbers from images.

To extract the license plate number from the image in Salesforce, we have to complete the below steps

  1. Get the test credential from RapidAPI
  2. Create Apex Class to extract Plate Number from the image
  3. Call Apex class from Flow
  4. Add button on page layout and call flow

1. Get the test credential from RapidAPI

Create an account in RapidAPI.com to test License Plate Recognition API. Once the account is created, go to https://rapidapi.com/macgyverapi/api/license-plate-recognition1 and get a test credential for this API.

2. Create Apex Class to extract Plate Number from image

Once test account and API credential are created, let us create apex class which will be extract license plate number from image.

API Url : https://macgyverapi-license-plate-recognition-v1.p.rapidapi.com/

Above API will need image url to process so we have to get url for our uploaded image in Salesforce record. Sample request for this API is below

{
    "id": "3X5D3d2k",
    "key": "free",
    "data": {
        "image_url": "https://storage.googleapis.com/marketing-files/program-markdown-assets/license-detection/license-plate.jpg",
        "country": "us",
        "numberCandidates": 2
    }
}

External Url for Image:

We can use ContentDistribution object for getting image url from uploaded image. We can create public image link when we upload file. Create trigger for creating public link.

trigger ContentVersionExternalLink on ContentVersion (after insert) {
	ContentTriggerHandler.createPublicLinkForFile(trigger.new);
}

ContentTriggerHandler class will help in creating public link for uploaded image. This class will be called from ContentVersionExternalLink trigger.

public class ContentTriggerHandler {
    public static void createPublicLinkForFile(List contentVersions){
        ContentDistribution[] distributionsToInsert = new List();
        Set contentDocId = new Set();
        for(ContentVersion objContentVersion : contentVersions){
            contentDocId.add(objContentVersion.ContentDocumentId);
        }
        for(ContentVersion objContentVersion : contentVersions){
           distributionsToInsert.add(createContentDistribution(objContentVersion.Id));
        }
        insert distributionsToInsert;
    }
    
    public static ContentDistribution createContentDistribution(Id contentVersionId){
        ContentDistribution newDist = new ContentDistribution();
        newDist.ContentVersionId = contentVersionId;
        newDist.Name = 'External Link';
        newDist.PreferencesNotifyOnVisit = false;
        newDist.PreferencesAllowViewInBrowser = true;
        return newDist;
    }
}

We also have to make uploaded file visible using ContentDocumentLink. Create trigger for this like below code.

trigger ContentDocumentLinkTrigger on ContentDocumentLink (before insert) {
    for(ContentDocumentLink l:Trigger.new) {
        l.Visibility='AllUsers';
    }
}

Now we will get public downloadable link of uploaded image. Let us create apex class which will extract plate number.

Complete Apex Code:

Above code will also update vehicle license plate number in case object. Create Plate Number field in case object to store extracted plate number.

Note: Add API url in remote site setting or you can use named credential to avoid this.

3. Call Apex class from Flow

After apex class created, we have to call this apex class from flow or lightning component. I have used flow to use as standard feature instead of custom code.

Here is complete flow process.

Variables :

Create two variables recordId and varRecordId.

This variable will hold value when record id passed from record page to flow.

Assignment Logic :

Create assignment logic to assign value from recordId to varRecordId. recordId is passed from record page.

Action Method :

Call apex class from action flow. Pass varRecordId to apex method. @InvocableMethod annotation is required for calling any apex method from flow.

4. Add button on page layout and call flow

Add a new action button in case object and then put this button on page layout. This button will call flow to update plate number in field.

Test Page;

References:

https://rapidapi.com/macgyverapi/api/license-plate-recognition1

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_what_is_apex.htm

You may also like

2 comments

Create OCR App using Salesforce Einstein OCR API | SalesforceCodex June 26, 2020 - 1:07 pm

[…] : This is image url. We can get downloadable url for our uploaded image. Refer Extract License Plate Number from Image In Salesforce for creating downloadable url for any uploaded […]

Reply
Dhanik Lal Sahni December 7, 2020 - 6:21 pm

Hey Smriti,

Looks like this API is down. Their website is also down, looks like they have stop their service.

I will write blog with some other API for this use case.

Thank You for letting me know.

Dhanik

Reply

Leave a Comment