【问题标题】:Clarifai API error while decoding base64 (nodejs)解码base64(nodejs)时出现Clarifai API错误
【发布时间】:2016-09-22 01:29:36
【问题描述】:

我正在使用 npm 包 clarifai 来访问 Clarifai API。我需要使用getTagsByImageBytes 函数,但我得到了这个答案:

{"status_code":"ALL_ERROR","status_msg":"请求中的所有图像均失败。请查看每个图像的错误消息。","meta":{},"results":[{"docid" :2.0371991595148082e+38,"status_code":"CLIENT_ERROR","status_msg":"数据加载失败,详见结果。","local_id":"","re​​sult":{"error":"data 0 is无效,因为:数据已损坏或数据类型不受支持.."},"docid_str":"99430754f1cd37d149b992bc635f685f"}]}

我的图像编码有问题。到目前为止,这是我尝试获取它的方式(path 是我的图像的本地路径,bytes 是我要发送给 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);
});

我也尝试发送JSON.stringify.toString 版本,但未成功。我想我在对预期字节数组的理解中遗漏了一些东西,有人可以帮助我吗?

谢谢!

更新:我更改了上面的代码和错误消息以更新我的最后一次尝试。

【问题讨论】:

  • 我不是专家,但您似乎没有提供足够的信息来重现您的问题。如果是这样,请尽可能“修复”这个问题。
  • 感谢您的建议。我已经编辑了代码,只需将其粘贴到 .js 文件中并使用节点启动它。 :)

标签: node.js api base64 clarifai


【解决方案1】:

好的,我有一个解决方案。当我使用此答案中的代码时,它实际上正在工作:NodeJS base64 image encoding/decoding not quite working

我的功能变成了:

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);
   }
});

我希望它可以帮助其他人。玩得开心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2016-01-25
    • 1970-01-01
    • 2021-06-20
    • 2016-04-15
    • 1970-01-01
    • 2020-03-15
    相关资源
    最近更新 更多