gpt4 book ai didi

node.js - 使用 Node 将图像作为二进制数据上传到认知服务

转载 作者:太空宇宙 更新时间:2023-11-03 23:24:28 25 4
gpt4 key购买 nike

我正在尝试向 Microsoft 认知服务面部 API 传递用户上传的图像。该图像可在服务器上的上传文件夹中找到。

Microsoft 期望镜像为“application/octet-stream”并作为二进制数据传递。

我目前无法找到一种方法将图像传递到 API,使其能够令人满意地被接受并不断收到“解码错误,图像格式不受支持”。据我所知,图像必须以 blob 或文件格式上传,但对于 NodeJs 来说,我真的不确定如何实现这一点。

到目前为止,我已经有了这个,并且看了一些选项,但没有一个起作用,我尝试的其他选项返回了类似的错误,例如“文件太小或太大”,但是当我通过 Postman 手动测试相同的图像时,它工作正常。

image.mv('./uploads/' + req.files.image.name , function(err) {
if (err)
return res.status(500).send(err);
});

var encodedImage = new Buffer(req.files.image.data, 'binary').toString('hex');

let addAPersonFace = cognitive.addAPersonFace(personGroupId, personId, encodedImage);

addAPersonFace.then(function(data) {
res.render('pages/persons/face', { data: data, personGroupId : req.params.persongroupid, personId : req.params.personid} );
})

最佳答案

您正在使用的软件包,cognitive-services ,似乎不支持文件上传。您可以选择在 GitHub page 上提出问题.

不过,如果可以的话,确实存在替代的 NPM 软件包。与project-oxford ,您将执行如下操作:

var oxford = require('project-oxford'),
client = new oxford.Client(YOUR_FACE_API_KEY),
uuid = require('uuid');

var personGroupId = uuid.v4();
var personGroupName = 'my-person-group-name';
var personName = 'my-person-name';
var facePath = './images/face.jpg';

// Skip the person-group creation if you already have one
console.log(JSON.stringify({personGroupId: personGroupId}));
client.face.personGroup.create(personGroupId, personGroupName, '')
.then(function(createPersonGroupResponse) {
// Skip the person creation if you already have one
client.face.person.create(personGroupId, personName)
.then(function(createPersonResponse) {
console.log(JSON.stringify(createPersonResponse))
personId = createPersonResponse.personId;
// Associate an image to the person
client.face.person.addFace(personGroupId, personId, {path: facePath})
.then(function (addFaceResponse) {
console.log(JSON.stringify(addFaceResponse));
})
})
});

关于node.js - 使用 Node 将图像作为二进制数据上传到认知服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46025649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com