Image recognition using Mulesoft and Salesforce
Tags: mulesofthackathon, salesforce, aws, twitter
Published: 2020-11-16 22:01:48

Mulesoft and Salesforce seem to be the right combination of technologies to be able to deliver projects robust and complex in short time. I would like to demonstrate how we can use both of them to recognize images produced from a mobile device and recognize a picture bringing more information and interesting data for kind of a real scenario.

So, how this is done? Well here are some of components I'm using for this project (I will go deep on each one):

1. Salesforce developer Org
2. Anypoint Platform (Sandbox) account
3. Mulesoft mule-aws-recognition-system-api.
4. Mulesoft mule-aws-recognition-process-api


## Salesforce developer Org

I have got a developer account for Salesforce from their developer site (developerforce.com). Salesforce in this case allows me to have:

* Custom Objects (tables)
* Custom Fields on each of those objects created
* A way to expose a mobile application (previously known as Salesforce 1) available to install on IOS or Android devices
* Visualforce pages, allowing to customize what we cant to show on a mobile app or browser
* Apex Classes, custom apex code to handle data from a page or allowing to expose REST services from a custom apex (java style) definition.

So here we have the design:

- Standard Object (Content Version and         ContentDocumentLink). Allows to store the actual binary file in Salesforce
- Custom Object (Hackathon Image). Allows to have a record to link the photo taken
- Custom Object (Image Label). Stores the image information labels and how accurate is the image with the label from AWS.

here comes the fun part..
- Visualforce page allowing to show an UI to take the picture:


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/17v83buqsp4tb1isij7v.png)

- Apex Controller. Allows to get all information from the picture and Create the Content Version and Content Link record related to the Hackathon image.

- Apex Controller REST . Exposes the mentions endpoint allowing to trigger a push notification in the mobile device.

Where I can get this code from?  https://github.com/emoran/sfdc-mulesoft-hackathon-2020.git

Now a basic flow:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vffdari364hl066tdh3r.png)



## Mulesoft mule-aws-recognition-system-api.

Initially this system api was for AWS only but because the time and resources I also included here one of the pieces I need to complete this exercise.

As I mentioned this system API allows to process a Base64 image and send it to Amazon Rekognition API, the result of this call is to be able to retrieve the labels generated from this call. 

This same application contains the logic to pull a few tweets using a parameter based on hashtags.



```
#%RAML 1.0
title: mule-aws-recognition-system-api

/image:
  post:
    body:
      application/json:
        
    responses:
      200:
        body:
          application/json:

/twitter:
  /tweets:
    get:
      queryParameters:
        q:
          description: "Parameters to filter by hashtag"
      responses:
        200:
          body:
            application/json:
```

To process the image basically I used the AWS Java SDK to use the API my flow looks like this:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wqo8o4yx314mklcwoan0.png)

In the other hand for the Tweets we have a different endpoint which receives only the GET request and we return all tweets based on the hashtags provided.

Here how the flow looks like:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k0et7hkipk2y1fk4jo8a.png)

As you can see this is just a pretty simple HTTP Request to the Twitter API, It's not included in the process API as we are not using a connector to extract the logic of this request.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/usrydm9n42zpit7ihdtx.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vmlzxwjcj6wbbb0muv6s.png)

You can get the code of the system API from here: https://github.com/emoran/mule-aws-recognition-system-api.git 



## Mulesoft mule-aws-recognition-process-api

At this point in the process api, now we are really doing more things and connecting the dots. I will try to explain step by step what happens.

The process API has this RAML:

```
#%RAML 1.0
title: mule-aws-recognition-process-api


/image:
  post:
    body:
      application/json:
    responses:
      200:
        body:
          application/json:
            example:

/sfdc:
  /images:
    get:
      responses:
        200:
          body:
            application/json:
  /contentVersion:
    get:
      queryParameters:
        id:
          description: imageId
          type: string
/tweets:
  get:
    responses:
      200:
        body:
          application/json:
            
                      
```

1. After the mobile application saves the picture  we took with our device, Salesforce calls the /images endpoint we exposed in Mulesoft, basically it passes three params imageRecordId (Hackathon Image), contentVersionId (Id of the actual file in Saleforce) and contentDocumentLinkId (Link document to the picture.)

2. Mulesoft gets the parameters, then using the Salesforce connector we make a query to Content Version and we download the file (actual image in Base64), then we call the system API to passing the image and wait for the bunch of labels that AWS recognized 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pj22olnpa5hbypfuikrv.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j98g5ifeyts44g0z363k.png)

3. Once AWS responded we also create the labels in Salesforce (Image Labels) for the uploaded image as records and lastly we call the REST Service we exposed in Salesforce in order to notify the person the image has been processed and now it has labels created.


![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2moeoualvk3isknffn9t.png)

It was really interesting to check how to call that REST service from the connector, since on older versions of the connector we were able to connect getting the session ID and use the REST endpoint directly. In Mule 4 we are not able to do so, in this case we use the connector capabilities to do it

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gru79gfgrh7ck43wlgvv.png)


4. Now in the last part as user you can use your device to see the labels created per record, but also I created a feature on this process API. I've created a page served on Mulesoft to show the information we saved!


How I did do it?, in the same process API I placed a new configuration file named "portal", a flow that contains a "Load Static Resource" that serves a page stored in a folder named "web" on src/main/resources 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06q1vlq2rcex1g5vclen.png)


Now the main page contains a script that uses JQuery in order to show the information of images and tweets.

This is how the render page looks like:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fp23s1divqsurqxtdiv3.png)

Basically the page tells you the labels we got from AWS, shows the image we took the picture and the tweets related to the generated labels.

you can get this code from here https://github.com/emoran/mule-aws-recognition-process-api.git

Watch the video:
http://www.youtube.com/watch?v=GWKP4U0o2Ng

[![](http://img.youtube.com/vi/GWKP4U0o2Ng/0.jpg)](http://www.youtube.com/watch?v=GWKP4U0o2Ng "Demo on video.")