1

我想通過微軟認知服務面部API一個用戶上傳的圖像。該圖像位於上傳文件夾中的服務器上。上傳圖像作爲二進制數據認知服務與節點

微軟希望圖像是'application/octet-stream'並作爲二進制數據傳遞。

我目前無法找到將圖像傳遞給API的方式,即令人滿意的圖像被接受並繼續收到「解碼錯誤,圖像格式不受支持」。據im知道圖像必須以blob或文件格式上傳,但是對於NodeJs而言,我還不確定如何實現這一點。

到目前爲止,我看到了一些選項,但都沒有工作,其他選項我嘗試返回類似的錯誤,如'文件太小或大',但是當我通過郵遞員手動測試相同的圖像時,它工作正常。

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

回答

1

看起來像你正在使用的軟件包cognitive-services似乎不支持文件上傳。您可能會選擇在GitHub page上提出問題。

但是,如果這是一個選項,替代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)); 
      }) 
     }) 
    }); 
+0

謝謝!雖然它沒有直接回答我的問題,但我最終決定,您建議的軟件包更全面和更好的支持,並決定在我的項目中使用它。 如果我找到實際問題的答案,我會在這裏發佈! – user1286856

1

請更新到0.2.0版本,這應該現在的工作。

相關問題