1

I am trying to use the food categorization API from clarifai but the example uses a publically hosted image. Is there any way to use the API using an image from a local folder? I am using nodejs.

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

// example code from Clarifai Docs
app.models.predict("bd367be194cf45149e75f01d59f77ba7", "https://samples.clarifai.com/food.jpg").then(
    function(response) {
      // do something with response
    },
    function(err) {
      // there was an error
    }
  );

Link where I got sample API request: https://www.clarifai.com/models/food-image-recognition-model-bd367be194cf45149e75f01d59f77ba7

Andy Chen
  • 85
  • 1
  • 7
  • Did you try anything and faced any issue. It seems you didn't try anything – Jaswanth Karani Aug 31 '20 at 03:37
  • 1
    Actually I did -- I tried putting the path to the local image in place of the url but it didnt work. I was hoping someone with experience with Clarifai would know the correct syntax – Andy Chen Aug 31 '20 at 04:53

1 Answers1

0

You will need to give the bytestream instead of the URL to upload from a local file. For example:

// Insert here the initialization code as outlined on this page:
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

const fs = require("fs");
const imageBytes = fs.readFileSync("{YOUR_IMAGE_LOCATION}");

stub.PostInputs(
    {
        inputs: [{data: {image: {base64: imageBytes}}}]
    },
    metadata,
    (err, response) => {
        if (err) {
            throw new Error(err);
        }

        if (response.status.code !== 10000) {
            throw new Error("Post inputs failed, status: " + response.status.description);
        }
    }
);

This code will read in the base64 bytes of the local image and send that (replacing url with base64). This is using the grpc based nodejs API. Some additional information including this example can be found https://docs.clarifai.com/api-guide/data/create-get-update-delete.

syntheticgio
  • 588
  • 6
  • 25