Add back eyes command

This commit is contained in:
Dragon Fire
2024-03-23 21:27:59 -04:00
parent 1b56cf4ff2
commit df0e1cf622
5 changed files with 76 additions and 19 deletions
+1 -18
View File
@@ -1,9 +1,6 @@
const Command = require('../../framework/Command');
const request = require('node-superfetch');
const { createCanvas, loadImage } = require('canvas');
const tfnode = require('@tensorflow/tfjs-node');
const faceDetection = require('@tensorflow-models/face-detection');
const model = faceDetection.SupportedModels.MediaPipeFaceDetector;
const path = require('path');
module.exports = class AnimeEyesCommand extends Command {
@@ -26,16 +23,13 @@ module.exports = class AnimeEyesCommand extends Command {
}
]
});
this.detector = null;
}
async run(msg, { image }) {
if (!this.detector) this.detector = await faceDetection.createDetector(model, { runtime: 'tfjs', maxFaces: 10 });
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);
const faces = await this.detect(imgData.body);
const faces = await this.client.detectFaces(imgData.body);
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);
@@ -56,15 +50,4 @@ module.exports = class AnimeEyesCommand extends Command {
}
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'anime-eyes.png' }] });
}
async detect(imgData) {
if (Buffer.byteLength(imgData) >= 4e+6) return 'size';
tfnode.setBackend('tensorflow');
const image = tfnode.node.decodeImage(imgData);
tfnode.setBackend('cpu');
const faces = await this.detector.estimateFaces(image);
tfnode.setBackend('tensorflow');
if (!faces || !faces.length) return null;
return faces;
}
};
+51
View File
@@ -0,0 +1,51 @@
const Command = require('../../framework/Command');
const request = require('node-superfetch');
const { createCanvas, loadImage } = require('canvas');
const path = require('path');
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: 1,
duration: 60
},
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);
const faces = await this.client.detectFaces(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 eyeWidth = face.box.width / 4;
const eyeHeight = face.box.height / 4;
const leftEyeData = face.keypoints.find(landmark => landmark.name === 'leftEye');
const rightEyeData = face.keypoints.find(landmark => landmark.name === 'rightEye');
const leftEyeX = leftEyeData.x - (eyeWidth / 2);
const leftEyeY = leftEyeData.y - (eyeHeight / 2);
const rightEyeX = rightEyeData.x - (eyeWidth / 2);
const rightEyeY = rightEyeData.y - (eyeHeight / 2);
ctx.drawImage(eyes, leftEyeX, leftEyeY, eyeWidth, eyeHeight);
ctx.drawImage(eyes, rightEyeX, rightEyeY, eyeWidth, eyeHeight);
}
return msg.say({ files: [{ attachment: canvas.toBuffer(), name: 'eyes.png' }] });
}
};