mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 15:07:42 +02:00
Eyes Command
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
@@ -0,0 +1,92 @@
|
||||
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 EyesCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'eyes',
|
||||
group: 'edit-image',
|
||||
memberName: 'eyes',
|
||||
description: 'Draws emoji 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 eyes = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'eyes.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);
|
||||
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 = eyes.width / leftWidth;
|
||||
const leftHeight = eyes.height * leftRatio;
|
||||
ctx.drawImage(
|
||||
eyes,
|
||||
landmarks.left_eye_left_corner.x,
|
||||
landmarks.left_eye_left_corner.y - (leftHeight / 2),
|
||||
leftWidth,
|
||||
leftHeight
|
||||
);
|
||||
const rightWidth = landmarks.right_eye_right_corner.x - landmarks.right_eye_left_corner.x;
|
||||
const rightRatio = eyes.width / rightWidth;
|
||||
const rightHeight = eyes.height * rightRatio;
|
||||
ctx.drawImage(
|
||||
eyes,
|
||||
landmarks.right_eye_left_corner.x,
|
||||
landmarks.right_eye_left_corner.y - (rightHeight / 2),
|
||||
rightWidth,
|
||||
rightHeight
|
||||
);
|
||||
}
|
||||
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: '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;
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "131.3.2",
|
||||
"version": "131.4.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user