Skip to content

devtutorials/ibm-cloud-functions-refarch-serverless-image-recognition

Repository files navigation

Serverless Image Recognition with Cloud Functions

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

Flow

  1. User chooses a picture from the gallery.
  2. The image is stored in the Cloudant database.
  3. Cloud Function is triggered when there's a new image in the database.
  4. Cloud Function gets the image and uses Watson Visual Recognition to process the image.
  5. Cloud Function stores the results (classes with scores) from Visual Recognition in the database.
  6. The user can see the new tags or classes in the image they uploaded.

Included components

  • 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.

Featured technologies

  • Serverless: An event-action platform that allows you to execute code in response to an event.

Prerequisites

  • 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.

Steps

1. Clone the repo

Clone the serverless-image-recognition locally. In a terminal, run:

$ git clone https://github.com/IBM/serverless-image-recognition

2. Create IBM Cloud Services

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 of CLOUDANT_USERNAME and CLOUDANT_PASSWORD.
  • Launch the Cloudant web console and create a database named images and tags. Create Cloudant credentials using the IBM Cloud dashboard and place them in the local.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 of WATSON_VISUAL_APIKEY

3. Create Cloud Functions

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

4. Launch Application

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

Sample output

sample-output

Links

License

Apache 2.0

About

Classify images as soon as you upload them in a database with serverless functions

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 72.4%
  • CSS 18.3%
  • HTML 9.3%