The application demonstrates an IBM Cloud Functions (based on Apache OpenWhisk) that gets an image from the Cloudant database and classifies it through Watson Visual Recognition. The use case demonstrates how actions work with data services and execute logic in response to Cloudant events.
One function, or action, is triggered by changes (in this use case, an upload of a document) in a Cloudant database. These documents are piped to another action that submits the image to Watson Visual recognition and upload a new document in Cloudant with the classifiers produced by Watson.
When the reader has completed this Code Pattern, they will understand how to:
- Create and Deploy Cloud Functions
- Trigger Cloud Functions with Cloudant changes
- Use Watson Image Recognition with Cloud Functions
- User chooses a picture from the gallery.
- The image is stored in the Cloudant database.
- Cloud Function is triggered when there's a new image in the database.
- Cloud Function gets the image and uses Watson Visual Recognition to process the image.
- Cloud Function stores the results (classes with scores) from Visual Recognition in the database.
- The user can see the new tags or classes in the image they uploaded.
- IBM Cloud Functions (powered by Apache OpenWhisk): Execute code on demand in a highly scalable, serverless environment.
- Cloudant: A fully managed data layer designed for modern web and mobile applications that leverages a flexible JSON schema.
- Watson Visual Recognition: Visual Recognition understands the contents of images - visual concepts tag the image, find human faces, approximate age and gender, and find similar images in a collection.
- Serverless: An event-action platform that allows you to execute code in response to an event.
-
IBM Cloud Functions CLI to create cloud functions from the terminal. Make sure you do the test action
ibmcloud wsk action invoke /whisk.system/utils/echo -p message hello --result
so that your~/.wskprops
is pointing to the right account. -
Whisk Deploy (wskdeploy) is a utility to help you describe and deploy any part of the OpenWhisk programming model using a Manifest file written in YAML. You'll use it to deploy all the Cloud Function resources using a single command. You can download it from the releases page and select the appropriate file for your system.
-
Install Node.js if you want to use Electron.
Clone the serverless-image-recognition
locally. In a terminal, run:
$ git clone https://github.com/IBM/serverless-image-recognition
Create a Cloudant instance and choose Use both legacy credentials and IAM
for the Available authentication method option.
- Create credentials for this instance and copy the username and password in the
local.env
file in the value ofCLOUDANT_USERNAME
andCLOUDANT_PASSWORD
. - Launch the Cloudant web console and create a database named
images
andtags
. Create Cloudant credentials using the IBM Cloud dashboard and place them in thelocal.env
file.
Modify
local.env
as needed if you have plan to have different database names.
Create a Watson Visual Recognition instance.
- Copy the API Key in the Credentials section and paste it in the
local.env
file in the value ofWATSON_VISUAL_APIKEY
Make sure you have the right environment variables in the local.env
file. Export them in your terminal then deploy the Cloud Functions using wskdeploy
:
$ source local.env
$ wskdeploy
Configure web/scripts/upload.js
. Modify the lines for your Cloudant credentials.
let usernameCloudant = "YOUR_CLOUDANT_USERNAME"
let passwordCloudant = "YOUR_CLOUDANT_PASSWORD"
Run the Electron app or open the html file.
- Electron:
$ npm install
$ npm start
- (or) Double-click
web/index.html