Working with CLARIFAI's Public APIs:

CLARIFAI offers a suite of powerful APIs that can perform various computer vision tasks on images and videos - tagging and detecting objects, faces, and more. In this blog post, we will explore the CLARIFAI APIs and learn how to use them using JavaScript.

Getting started:

Before we can start using the CLARIFAI APIs, we will need to create an account on their developer portal and create an API key. Once the API key is generated, we can start using the APIs.

Installing the CLARIFAI JavaScript SDK:

To make it easier to interact with the CLARIFAI APIs, we can use the official CLARIFAI JavaScript SDK. To install it, run the following command using npm:

npm install clarifai --save

Using the CLARIFAI APIs:

The CLARIFAI API provides different models to perform various tasks. Let's take a look at some of the most commonly used models:

Tagging Images using the General Model:

The General Model is used to tag images with concepts that are present in the image. To use this model, we first need to load the model using the CLARIFAI JavaScript SDK, as shown below:

const Clarifai = require('clarifai');
const app = new Clarifai.App({apiKey: 'YOUR_API_KEY_HERE'});

// Load the General Model
app.models
  .initModel({id: Clarifai.GENERAL_MODEL})
  .then(generalModel => {
    console.log('General Model Loaded!');
  })
  .catch(err => {
    console.error(err);
  });

Once the General Model is loaded, we can use it to tag an image by providing the image URL, as shown below:

// Tag an Image
app.models
  .predict(Clarifai.GENERAL_MODEL, 'https://samples.clarifai.com/metro-north.jpg')
  .then(response => {
    const concepts = response.outputs[0].data.concepts;
    console.log(concepts);
  })
  .catch(err => {
    console.error(err);
  });

In the above example, concept is an array of objects that contains information about the concepts present in the image.

Detecting Faces using the Face Detection Model:

The Face Detection Model is used to detect faces in an image. Similar to the General Model, we first need to load the Face Detection Model, as shown below:

// Load the Face Detection Model
app.models
  .initModel({id: Clarifai.FACE_DETECT_MODEL})
  .then(faceDetectModel => {
    console.log('Face Detection Model Loaded!');
  })
  .catch(err => {
    console.error(err);
  });

Once the Face Detection Model is loaded, we can use it to detect faces in an image by providing the image URL, as shown below:

// Detect Faces in an Image
app.models
  .predict(Clarifai.FACE_DETECT_MODEL, 'https://samples.clarifai.com/face-det.jpg')
  .then(response => {
    const regions = response.outputs[0].data.regions;
    console.log(regions);
  })
  .catch(err => {
    console.error(err);
  });

In the above example, regions is an array of objects that contains information about the regions where faces were detected in the image.

Conclusion:

The CLARIFAI APIs provide powerful computer vision capabilities that can be leveraged to build intelligent applications. In this blog post, we explored how to use CLARIFAI APIs in JavaScript to tag images and detect faces, using the General Model and the Face Detection Model.

Related APIs