Skip to content

Instantly share code, notes, and snippets.

@conanak99
Created April 14, 2018 15:03
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/48cb1189a1d9cfdd0fbb20dd04c59001 to your computer and use it in GitHub Desktop.
Save conanak99/48cb1189a1d9cfdd0fbb20dd04c59001 to your computer and use it in GitHub Desktop.
const rp = require('request-promise');
const Jimp = require("jimp");
(async () => {
const imageUrl = 'https://pbs.twimg.com/media/DWr05hUXcAA9s8n.jpg';
const detectResult = await detectImage(imageUrl);
addMustache(imageUrl, detectResult, 'jav_rau.jpg');
})();
async function addMustache(url, detectResult, output) {
// Tính toán vị trí và khuôn mặt
const face = detectResult[0]; // Hình chỉ nhận được 1 khuôn mặt
const landmarks = face.faceLandmarks;
const mouthLeft = landmarks['mouthLeft'];
const mouthRight = landmarks['mouthRight'];
const x1 = mouthLeft.x, y1 = mouthLeft.y;
const x2 = mouthRight.x, y2= mouthRight.y;
const mustacheWidth = x2 - x1;
let source = await Jimp.read(url);
const mustache = await Jimp.read('mustache.png');
mustache.resize(mustacheWidth, Jimp.AUTO);
return source.composite(mustache, x1, y2).write(output);
}
async function detectImage(source) {
const subscriptionKey = "3845387fabbf4ba5bfe958a2409b8238";
const uri = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";
const options = {
uri,
qs: {
returnFaceId: true,
returnFaceLandmarks: true,
},
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey
},
body: {
url: source
},
json: true // Automatically parses the JSON string in the response
};
const result = await rp(options);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment