1

I am using the npm package clarifai to access the Clarifai API. I need to use the getTagsByImageBytes function, but I get this answer:

{"status_code":"ALL_ERROR","status_msg":"All images in request have failed. Please review the error messages per image.","meta":{},"results":[{"docid":2.0371991595148082e+38,"status_code":"CLIENT_ERROR","status_msg":"Data loading failed, see results for details. ","local_id":"","result":{"error":"data 0 is not a valid because: Data is corrupt or data type is not supported.."},"docid_str":"99430754f1cd37d149b992bc635f685f"}]}

I have a problem with my image encoding. Here is how I attempted to get it so far (path is the local path to my image and bytes the variable I want to send to Clarifai):

const fs = require('fs');
let path = 'path/to/any/image';
let buffers = [];
let bytes;
// creating a reading stream
const readable = fs.createReadStream(path);

// the read content is added to the buffer array
readable.on('data', (chunk) => {
    buffers.push(chunk);
});

// all the data read are joined in a single buffer
readable.on('end', () => {
    bytes = Buffer.from(buffers.join(), 'binary').toString('base64');
    // here the goal is to have a valid base64 encoded image
    console.log(bytes);
});

I have also unsuccessfully tried to send the JSON.stringify and the .toString version. I think I miss something in my nderstanding of the expected bytes array, is there anyone to help me?

thanks!

UPDATE: I changed the code and the error message above to update my last attempts.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
nakurai
  • 155
  • 1
  • 9
  • I'm not an expert, but it looks like you haven't provided enough info to *reproduce* your issue. If so, please "fix" this if possible. – YakovL Sep 22 '16 at 02:05
  • Thank you for your advice. I have edited the code, simply paste it in a .js file and use node to launch it. :) – nakurai Sep 22 '16 at 02:12

1 Answers1

0

Ok, so I have a solution. It is actually working when I use the code from this answer: NodeJS base64 image encoding/decoding not quite working

My function has become:

let path = 'my/path/to/image';
fs.readFile(path, function(err, original_data){
   if(err){
       return;
   }
   else{
       // this variable contains the correctly encoded image. Just use it as it is in the Clarifai API
       var base64Image = original_data.toString('base64');
       console.log(base64Image);
   }
});

I hope it can help other people. Have fun!

Community
  • 1
  • 1
nakurai
  • 155
  • 1
  • 9