1

我正在尝试使用来自 clarifai 的食品分类 API,但该示例使用公共托管的图像。有没有办法使用本地文件夹中的图像使用 API?我正在使用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
    }
  );

我收到示例 API 请求的链接:https ://www.clarifai.com/models/food-image-recognition-model-bd367be194cf45149e75f01d59f77ba7

4

1 回答 1

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.

于 2020-11-02T05:25:10.920 回答