1

I am trying to pass the Microsoft Cognitive services facial API an image which the user has uploaded. The image is available on the server in the uploads folder.

Microsoft is expecting the image to be 'application/octet-stream' and passed as binary data.

I am currently unable to find a way to pass the image to the API that is satisfactory for it to be accepted and keep receiving "decoding error, image format unsupported". As far as im aware the image must be uploaded in blob or file format but being new to NodeJs im really unsure on how to achieve this.

So far i have this and have looked a few options but none have worked, the other options i tried returned simmilar errors such as 'file too small or large' but when ive manually tested the same image via Postman it works fine.

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

2 回答 2

1

请更新到版本0.2.0,现在应该可以使用了。

于 2017-09-10T10:23:23.707 回答
1

看起来您正在使用的包,cognitive-services,似乎不支持文件上传。您可以选择在 GitHub页面上提出问题。

但是,如果可以的话,确实存在替代的 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));
          })
      })
  });
于 2017-09-03T23:58:27.443 回答