Azure Face API - Node JS Cannot sent local file in form of binary

359 Views Asked by At

I have tried to send the base64 form image into the Azure Face-API, with a config like this

var config = {
    method: 'post',
    url: endpoint,
    params: {
        returnFaceId: true,
        returnFaceLandmarks: false,
        returnFaceAttributes: 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
    },
    body:Buffer(facesBase64, 'base64'),
    headers: {
        'Ocp-Apim-Subscription-Key': subscriptionKey,
        'Content-Type': 'application/octet-stream'

    }
};

but it always gets error 400. Is the binary form I sent wrong? facesBase64 is already in Base64 form.

EDIT facesBase64 is full of base64 like this value

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ......
1

There are 1 best solutions below

0
On

I think you can't send the base64 image into the Azure Face-API.

UPDATE

Send local image to faceapi.

'use strict';
const request = require('request');
const fs = require("fs");

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = "eb8d5ea******8877c0" ;

const uriBase = 'https://pan***api.cognitiveservices.azure.com/face/v1.0/detect';

const imageBuffer = fs.readFileSync('jason.jpg');

// Request parameters.
const params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
        'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
};

const options = {
    uri: uriBase,
    qs: params,
    body: imageBuffer,
    headers: {
        'Content-Type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key' : subscriptionKey
    }
};

request.post(options, (error, response, body) => {
    if (error) {
        console.log('Error: ', error);
        return;
    }
    let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
    console.log('JSON Response\n');
    console.log(jsonResponse);
});

enter image description here

I tried the method you mentioned for testing and got the following results.

{
  "error": {
    "code": "InvalidImage",
    "message": "Decoding error, image format unsupported."
   }
}

Below is my test code.

'use strict';

//const request = require('request');
const fs = require("fs");
const axios = require("axios");
const request = require('request').defaults({ encoding: null });

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = "eb8d5ea0********8877c0" ;

const uriBase = "https://pans***api.cognitiveservices.azure.com"+ '/face/v1.0/detect'
const imgUrl="https://pan***torage.blob.core.windows.net/ja**b/ja**.jpg";


var imageBuffer;
request.get(imgUrl, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var data;
        data = "data:" + response.headers["content-type"] + ";base64," + Buffer.from(body).toString('base64');
        imageBuffer=data;
        aa();
    }
});

function aa(){
// Request parameters.
const params = {
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'false',
    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,' +
        'emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'
};

const options = {
    uri: uriBase,
    qs: params,
    body: imageBuffer,
    headers: {
        'Content-Type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key' : subscriptionKey
    }
};

request.post(options, (error, response, body) => {
    if (error) {
        console.log('Error: ', error);
        return;
    }
    let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
    console.log('JSON Response\n');
    console.log(jsonResponse);
});

}

Suggest.

If you really want to implement your function in this way, it is recommended to raise a support on the portal. Consult the official answer, which image formats currently supported by faceapi.

You can also submit suggestions to the official product group.