12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- const Clarifai = require("clarifai");
- const ClarifaiTOKEN = require("../clarifai_keys.js");
- const db = require("../db.js");
- const util = require("../util.js");
- const Jimp = require("jimp");
- const client = require("../client.js");
- const EMOTE_GUILD = "505333548694241281";
- const clarifaiApp = new Clarifai.App({
- apiKey: ClarifaiTOKEN
- });
- async function processFaceSwap(message, attachmentUrl) {
- let result = await clarifaiApp.models.predict(Clarifai.FACE_DETECT_MODEL, attachmentUrl);
- if(result.outputs[0].data.regions === undefined || result.outputs[0].data.regions.length == 0)
- return;
-
- let image = await Jimp.read(attachmentUrl);
- let w = image.getWidth();
- let h = image.getHeight();
- let emojiKeys = [...client.guilds.get(EMOTE_GUILD).emojis.filter(e => !e.animated).keys()];
- for(let region of result.outputs[0].data.regions) {
- let bb = region.region_info.bounding_box;
- let bw = (bb.right_col - bb.left_col) * w;
- let bh = (bb.bottom_row - bb.top_row) * h;
-
- let dx = (bb.right_col + bb.left_col) * w / 2;
- let dy = (bb.bottom_row + bb.top_row) * h / 2;
- let emojiKey = emojiKeys[Math.floor(Math.random() * emojiKeys.length)];
- let emoji = client.emojis.get(emojiKey);
-
- let emojiImage = await Jimp.read(emoji.url);
- let ew = emojiImage.getWidth();
- let eh = emojiImage.getHeight();
- const CONSTANT_SCALE = 1.5;
- let scaleFactor = Math.max(bw, bh) / Math.min(ew, eh) * CONSTANT_SCALE;
- ew *= scaleFactor;
- eh *= scaleFactor;
- emojiImage = emojiImage.scale(scaleFactor);
- image = image.composite(emojiImage, dx - ew / 2, dy - eh / 2);
- }
- image.quality(90);
- let buffer = await image.getBufferAsync(Jimp.MIME_JPEG);
- message.channel.send(`I noticed a face in the image. I think this looks better ${client.emojis.get("505076258753740810").toString()}`, {
- files: [ buffer ]
- });
- }
- const onMessage = (msg, contents, actionsDone) => {
- if (actionsDone)
- return false;
- let imagesCount = msg.attachments.filter(v => util.isValidImage(v.filename)).size;
- if (imagesCount > 0) {
- let probValue = db.get("faceEditChannels").get(msg.channel.id);
- if (probValue.isUndefined().value() || probValue.isNull().value())
- return false;
- if (Math.random() > probValue.value())
- return false;
- let imageAttachment = msg.attachments.find(v => util.isValidImage(v.filename));
- processFaceSwap(msg, imageAttachment.url).catch(err => console.log(`Failed to run faceapp because ${err}`));
- return true;
- }
- return false;
- };
- module.exports = {
- onMessage: onMessage
- };
|