【发布时间】: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":"","result":{"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