request({ method: 'POST', url: 'https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true&returnFaceAttributes=age,gender', headers: { 'Content-Type': 'application/octet-stream', 'Ocp-Apim-Subscription-Key': 'your Face API key' }, body:data
}, function (error, response, body) { if (!error && response.statusCode == 200) { Fdata =JSON.parse(body); console.dir(Fdata, {depth: null, colors: true}); if (isEmpty(Fdata)) { console.log("No face detect!"); io.emit('message',{'id':'No Face'}); } else { console.log('Face Detect'); io.emit('message',{'id':Fdata[0].faceId,'gender':Fdata[0].faceAttributes.gender,'age':Fdata[0].faceAttributes.age}); //由此解析該臉孔的性別與年齡 } } }); //--------emotion API----------- request({ method: 'POST', url: 'https://api.projectoxford.ai/emotion/v1.0/recognize', headers: { 'Content-Type': 'application/octet-stream', 'Ocp-Apim-Subscription-Key': 'your Emotion API key' }, body: data }, function (error, response, body) { if (!error && response.statusCode == 200) { var object = JSON.parse(body); console.dir(object, {depth: null, colors: true}); } }); }); },3000) });
function isEmpty(obj) { // null and undefined are "empty" if (obj == null) return true;
// Assume if it has a length property with a non-zero value that that property is correct. if (obj.length > 0) return false; if (obj.length === 0) return true;
// Otherwise, does it have any properties of its own? // Note that this doesn't handle toString and valueOf enumeration bugs in IE < 9 for (var key in obj) { if (hasOwnProperty.call(obj, key)) return false; } return true; }