diff --git a/README.md b/README.md index 0cdf0c0a..c7aeba8f 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 613 +Total: 614 ### Utility: @@ -660,6 +660,7 @@ Total: 613 ### Image Manipulation: * **adorable:** Creates an adorable avatar based on the text you provide. +* **anime-eyes:** Draws anime eyes onto the faces in an image. * **approved:** Draws an "approved" stamp over an image or a user's avatar. * **blur:** Draws an image or a user's avatar but blurred. * **bob-ross:** Draws an image or a user's avatar over Bob Ross' canvas. @@ -1199,6 +1200,7 @@ here. - [Evil Mojo Games](https://www.evilmojogames.com/) * paladins ([Original "Paladins" Game](https://www.paladins.com/)) - [Face++ Cognitive Services](https://www.faceplusplus.com/) + * anime-eyes ([Face Detection API](https://www.faceplusplus.com/face-detection/)) * danny-devito ([Face Detection API](https://www.faceplusplus.com/face-detection/)) * eyes ([Face Detection API](https://www.faceplusplus.com/face-detection/)) * face ([Face Detection API](https://www.faceplusplus.com/face-detection/)) diff --git a/assets/images/anime-eyes/left.png b/assets/images/anime-eyes/left.png new file mode 100644 index 00000000..c91065d5 Binary files /dev/null and b/assets/images/anime-eyes/left.png differ diff --git a/assets/images/anime-eyes/right.png b/assets/images/anime-eyes/right.png new file mode 100644 index 00000000..06bfa306 Binary files /dev/null and b/assets/images/anime-eyes/right.png differ diff --git a/commands/edit-image/anime-eyes.js b/commands/edit-image/anime-eyes.js new file mode 100644 index 00000000..ab1f747c --- /dev/null +++ b/commands/edit-image/anime-eyes.js @@ -0,0 +1,94 @@ +const Command = require('../../structures/Command'); +const request = require('node-superfetch'); +const { createCanvas, loadImage } = require('canvas'); +const path = require('path'); +const { base64 } = require('../../util/Util'); +const { FACEPLUSPLUS_KEY, FACEPLUSPLUS_SECRET } = process.env; + +module.exports = class AnimeEyesCommand extends Command { + constructor(client) { + super(client, { + name: 'anime-eyes', + aliases: ['ani-eyes', 'manga-eyes'], + group: 'edit-image', + memberName: 'anime-eyes', + description: 'Draws anime eyes onto the faces in an image.', + throttling: { + usages: 1, + duration: 60 + }, + credit: [ + { + name: 'Face++ Cognitive Services', + url: 'https://www.faceplusplus.com/', + reason: 'Face Detection API', + reasonURL: 'https://www.faceplusplus.com/face-detection/' + } + ], + args: [ + { + key: 'image', + prompt: 'What face would you like to scan?', + type: 'image-or-avatar' + } + ] + }); + } + + async run(msg, { image }) { + const leftEye = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'anime-eyes', 'left.png')); + const rightEye = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'anime-eyes', 'right.png')); + const imgData = await request.get(image); + try { + const faces = await this.detect(imgData); + if (!faces) return msg.reply('There are no faces in this image.'); + if (faces === 'size') return msg.reply('This image is too large.'); + const base = await loadImage(imgData.body); + const canvas = createCanvas(base.width, base.height); + const ctx = canvas.getContext('2d'); + ctx.drawImage(base, 0, 0); + for (const face of faces) { + const landmarks = face.landmark; + const leftWidth = landmarks.left_eye_right_corner.x - landmarks.left_eye_left_corner.x; + const leftRatio = leftWidth / leftEye.width; + const leftHeight = leftEye.height * leftRatio; + ctx.drawImage( + leftEye, + landmarks.left_eye_left_corner.x - (leftWidth / 2), + landmarks.left_eye_left_corner.y - (leftHeight / 2) - (leftHeight / 2), + leftWidth * 2, + leftHeight * 2 + ); + const rightWidth = landmarks.right_eye_right_corner.x - landmarks.right_eye_left_corner.x; + const rightRatio = rightWidth / rightEye.width; + const rightHeight = rightEye.height * rightRatio; + ctx.drawImage( + rightEye, + landmarks.right_eye_left_corner.x - (rightWidth / 2), + landmarks.right_eye_left_corner.y - (rightHeight / 2) - (rightHeight / 2), + rightWidth * 2, + rightHeight * 2 + ); + } + return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'anime-eyes.png' }] }); + } catch (err) { + if (err.status === 400) return msg.reply('There are no faces in this image.'); + if (err.status === 403) return msg.reply('Hold your horses! The command is overloaded! Try again soon.'); + return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } + + async detect(imgData) { + if (Buffer.byteLength(imgData.body) >= 2e+6) return 'size'; + const { body } = await request + .post('https://api-us.faceplusplus.com/facepp/v3/detect') + .attach('image_base64', base64(imgData.body)) + .query({ + api_key: FACEPLUSPLUS_KEY, + api_secret: FACEPLUSPLUS_SECRET, + return_landmark: 1 + }); + if (!body.faces || !body.faces.length) return null; + return body.faces; + } +}; diff --git a/package.json b/package.json index c1a1d993..a82faa6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "131.7.0", + "version": "131.8.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {