лицо API с Nodejs - PullRequest
       41

лицо API с Nodejs

0 голосов
/ 07 февраля 2019

Я использую Azure Face API, используя узел JS, ниже приведен код.Однако вместо того, чтобы разместить изображение, я хочу использовать свое локальное изображение и опубликовать его.я пробовал разные варианты, но он не распознает формат изображения или неверный URL-адрес изображения

ниже приведены вещи, которые я пробовал

1) var stream = fs.createReadStream('local image url');
2) var imageAsBase64 = fs.readFileSync('image.jpg','base64');

ниже указан код

'use strict';

const request = require('request');

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = '<Subscription Key>';

// You must use the same location in your REST call as you used to get your
// subscription keys. For example, if you got your subscription keys from
// westus, replace "westcentralus" in the URL below with "westus".
const uriBase = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect';

const imageUrl =
    'https://upload.wikimedia.org/wikipedia/commons/3/37/Dagestani_man_and_woman.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: '{"url": ' + '"' + imageUrl + '"}',
   //body: stream,
   body:imageAsBase64,
    headers: {
        'Content-Type': 'application/json',
        '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);
});

1 Ответ

0 голосов
/ 07 февраля 2019

Я поэкспериментировал с этим и получил следующий код, работающий на самом деле, вы были очень близки к началу.Главное - передать объект Buffer в поле body запроса POST и указать правильный заголовок типа содержимого.

'use strict';

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

// Replace <Subscription Key> with your valid subscription key.
const subscriptionKey = <Subscription Key> ;

const uriBase = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect';

const imageBuffer = fs.readFileSync('image.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);
});
...