Skip to content

Instantly share code, notes, and snippets.

@conanak99
Created January 16, 2017 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conanak99/0e9f7c19b14e7a266f1637e68e43cd07 to your computer and use it in GitHub Desktop.
Save conanak99/0e9f7c19b14e7a266f1637e68e43cd07 to your computer and use it in GitHub Desktop.
function mapResultToIdol(result, faces) {
var allIdols = result.map(result => {
// Lấy vị trí khuôn mặt trong ảnh để hiển thị
result.face = faces.filter(face => face.faceId == result.faceId)[0].faceRectangle;
// Tìm idol đã được nhận diện từ DB
if (result.candidates.length > 0) {
// Kết quả chỉ trả về ID, dựa vào ID này ta tìm tên của idol
var idolId = result.candidates[0].personId;
var idol = idolPerson.filter(person => person.personId == idolId)[0];
result.idol = {
id: idol.userData,
name: idol.name
};
} else {
result.idol = {
id: 0,
name: 'Unknown'
}
}
return result;
});
console.log(`Finish recognize image.`);
return allIdols;
}
// Nhận diện vị trí khuôn mặt và tên idol từ URL ảnh
// Dùng promise nên cách viết hơi khác một
function recognize(imageUrl) {
console.log(`Begin to recognize image: ${imageUrl}`);
let faces = [];
return detect(imageUrl)
.then(result => {
faces = result;
console.log(faces);
return faces.map(face => face.faceId);
})
.then(identify)
.then(identifiedResult => {
return mapResultToIdol(identifiedResult, faces);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment