Created
January 16, 2017 10:38
-
-
Save conanak99/0e9f7c19b14e7a266f1637e68e43cd07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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