node.js - 使用 Nodejs 面对 api

标签 node.js azure face-api

我正在使用 Node js 使用 azure Face api,下面是代码。然而,我想使用本地镜像并发布它,而不是托管一些图像。我尝试了不同的选项,但它无法识别图像格式或无效的图像网址

以下是我尝试过的事情

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

最佳答案

我对此进行了尝试,并让以下代码正常工作,实际上您已经非常接近让它运行了。主要是将 Buffer 对象传递给 POST 请求的 body 字段,并指定正确的 content-type header。

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

关于node.js - 使用 Nodejs 面对 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54574681/

相关文章:

c# - 在 C# 和 NodeJS 中生成 PBKDF2 key

Ubuntu 20.04 上的 Azure IoT Edge 安装中缺少 Azure IoT 中心

javascript - 使用 Face-api.js 检测面部时出错预期媒体为 HTMLImageElement 类型 | HTMLVideo 元素 | HTMLCanvas元素

javascript - 无法在face-api.js中从本地 Node js服务器加载图像

python - Face API,如何在Python中验证大量人脸

javascript - 如何将标签 "a"中数据库中的值插入到属性 href

javascript - 如何思考 Node.js 中的异步回调操作

node.js - 如何将 .then 方法制作为 async.waterfall Node js

java - 有没有办法利用表(azure-storage)客户端 API 通过 CosmosDb 设置项目级别 TTL(生存时间)?

azure - 如何使用 Microsoft Graph API 获取 Active Directory 用户的电话号码