user1286856
user1286856

Reputation: 75

Uploading image as Binary Data to cognitive Services with Node

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

Upvotes: 1

Views: 941

Answers (2)

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17496

Please update to version 0.2.0, this should work now.

Upvotes: 1

cthrash
cthrash

Reputation: 2973

The package it looks like you're using, cognitive-services, does not appear to support file uploads. You might choose to raise an issue on the GitHub page.

Alternative NPM packages do exist, though, if that's an option. With project-oxford, you would do something like the following:

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

Upvotes: 1

Related Questions