mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Add Face manipulation group
This commit is contained in:
@@ -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-face',
|
||||
memberName: 'anime-eyes',
|
||||
description: 'Draws anime eyes onto the faces in an image.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
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 * 0.25),
|
||||
landmarks.left_eye_left_corner.y - (leftHeight / 2) - (leftHeight * 0.25),
|
||||
leftWidth * 1.5,
|
||||
leftHeight * 1.5
|
||||
);
|
||||
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 * 0.25),
|
||||
landmarks.right_eye_left_corner.y - (rightHeight / 2) - (rightHeight * 0.25),
|
||||
rightWidth * 1.5,
|
||||
rightHeight * 1.5
|
||||
);
|
||||
}
|
||||
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;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,88 @@
|
||||
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 DannyDevitoCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'danny-devito',
|
||||
aliases: ['devito'],
|
||||
group: 'edit-face',
|
||||
memberName: 'danny-devito',
|
||||
description: 'Draws Danny Devito\'s face onto the faces in an image.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
duration: 60
|
||||
},
|
||||
credit: [
|
||||
{
|
||||
name: 'Danny DeVito',
|
||||
url: 'https://twitter.com/dannydevito',
|
||||
reason: 'Himself'
|
||||
},
|
||||
{
|
||||
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 danny = await loadImage(path.join(__dirname, '..', '..', 'assets', 'images', 'danny-devito.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 width = landmarks.contour_right1.x - landmarks.contour_left1.x;
|
||||
const ratio = width / danny.width;
|
||||
const height = danny.height * ratio;
|
||||
ctx.drawImage(
|
||||
danny,
|
||||
landmarks.contour_left1.x - (width * 0.25),
|
||||
landmarks.contour_left1.y - (height / 2) - (height * 0.25),
|
||||
width * 1.5,
|
||||
height * 1.5
|
||||
);
|
||||
}
|
||||
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'danny-devito.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;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,98 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { GuildEmoji } = require('discord.js');
|
||||
const request = require('node-superfetch');
|
||||
const { createCanvas, loadImage } = require('canvas');
|
||||
const twemoji = require('twemoji-parser');
|
||||
const { base64 } = require('../../util/Util');
|
||||
const { FACEPLUSPLUS_KEY, FACEPLUSPLUS_SECRET } = process.env;
|
||||
|
||||
module.exports = class EmojiFaceCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'emoji-face',
|
||||
aliases: ['emoji-f', 'e-face'],
|
||||
group: 'edit-face',
|
||||
memberName: 'emoji-face',
|
||||
description: 'Draws an emoji onto the faces in an image.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
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: 'emoji',
|
||||
prompt: 'What emoji do you want to draw?',
|
||||
type: 'default-emoji|custom-emoji'
|
||||
},
|
||||
{
|
||||
key: 'image',
|
||||
prompt: 'What face would you like to scan?',
|
||||
type: 'image-or-avatar'
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
async run(msg, { emoji, image }) {
|
||||
let emojiURL;
|
||||
if (emoji instanceof GuildEmoji) {
|
||||
emojiURL = emoji.url;
|
||||
} else {
|
||||
const parsed = twemoji.parse(emoji);
|
||||
if (!parsed.length || !parsed[0].url) return msg.reply('This emoji is not yet supported.');
|
||||
emojiURL = parsed[0].url;
|
||||
}
|
||||
const emojiData = await request.get(emojiURL);
|
||||
const emojiImg = await loadImage(emojiData.body);
|
||||
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 width = landmarks.contour_right1.x - landmarks.contour_left1.x;
|
||||
const ratio = width / emojiImg.width;
|
||||
const height = emojiImg.height * ratio;
|
||||
ctx.drawImage(
|
||||
emojiImg,
|
||||
landmarks.contour_left1.x - (width * 0.25),
|
||||
landmarks.contour_left1.y - (height / 2) - (height * 0.25),
|
||||
width * 1.5,
|
||||
height * 1.5
|
||||
);
|
||||
}
|
||||
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'emoji-face.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;
|
||||
}
|
||||
};
|
||||
@@ -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-face',
|
||||
memberName: 'eyes',
|
||||
description: 'Draws emoji eyes onto the faces in an image.',
|
||||
throttling: {
|
||||
usages: 2,
|
||||
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.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 / eyes.width;
|
||||
const leftHeight = eyes.height * leftRatio;
|
||||
ctx.drawImage(
|
||||
eyes,
|
||||
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 / eyes.width;
|
||||
const rightHeight = eyes.height * rightRatio;
|
||||
ctx.drawImage(
|
||||
eyes,
|
||||
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: '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;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user