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;
}
};