Last active
July 20, 2020 14:25
-
-
Save devsnek/a40f267f4d0747ba447b64a8a2665223 to your computer and use it in GitHub Desktop.
another example command for the sexy command system. It puts eyes on images.
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
"use strict"; | |
var oxford = require('project-oxford'); | |
var client = new oxford.Client('TOKEN'); | |
var gm = require('gm'); | |
var http = require('http'); | |
var fs = require('fs'); | |
function getFilesize(filename) { | |
var stats = fs.statSync(filename) | |
var fileSizeInBytes = stats["size"] | |
return fileSizeInBytes / 1000000.0; | |
} | |
module.exports = { | |
main: function(bot, msg) { | |
var args = msg; | |
console.log("URL", args); | |
var stream = request(args).pipe(fs.createWriteStream('temp.jpg')); | |
stream.on('finish', function() { | |
if (getFilesize('temp.jpg') < 1) { | |
try { | |
bot.startTyping(msg.channel, function() { | |
client.face.detect({ | |
path: 'temp.jpg', | |
analyzesFaceLandmarks: true, | |
}).then(function(responses) { | |
if (responses[0]) { | |
var image = gm('temp.jpg'); | |
for (let response of responses) { | |
var l = response.faceLandmarks.pupilLeft; | |
var r = response.faceLandmarks.pupilRight; | |
var eyeHeight = (r.x - l.x) / 1.4; | |
var eyeWidth = eyeHeight * 0.849122807; | |
image.draw(['image over ' + (l.x - (eyeWidth / 2)) + ',' + (l.y - (eyeHeight / 2)) + ' ' + eyeWidth + ',' + eyeHeight + ' /path/to/eye.png']) | |
.draw(['image over ' + (r.x - (eyeWidth / 2)) + ',' + (r.y - (eyeHeight / 2)) + ' ' + eyeWidth + ',' + eyeHeight + ' /path/to/eye.png']) | |
} | |
image.write('temp_out.png', function(err) { | |
if (!err) { | |
console.log(' hooray! '); | |
bot.sendFile(msg.channel, 'temp_out.png', 'eyes.png', ':eyes:'); | |
} else { | |
console.log(err); | |
} | |
}); | |
} else { | |
bot.sendMessage(msg.channel, "`Could not find face!`"); | |
} | |
}); | |
}); | |
} catch (err) { | |
bot.sendMessage(msg.channel, "`Something went wrong!`"); | |
} | |
} else { | |
bot.sendMessage(msg.channel, "`Image too large!`"); | |
} | |
}); | |
}, | |
args: '<url>', | |
help: "put eyes on face" | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment