|
@@ -41,10 +41,8 @@ async function morphFaces(faces: cv.Rect[], data: Buffer) {
|
|
for (const rect of faces) {
|
|
for (const rect of faces) {
|
|
let dx = rect.x + rect.width / 2;
|
|
let dx = rect.x + rect.width / 2;
|
|
let dy = rect.y + rect.height / 2;
|
|
let dy = rect.y + rect.height / 2;
|
|
-
|
|
|
|
let emojiKey = emojiKeys[Math.floor(Math.random() * emojiKeys.length)];
|
|
let emojiKey = emojiKeys[Math.floor(Math.random() * emojiKeys.length)];
|
|
let emoji = client.emojis.get(emojiKey);
|
|
let emoji = client.emojis.get(emojiKey);
|
|
-
|
|
|
|
let emojiImage = await Jimp.read(emoji.url);
|
|
let emojiImage = await Jimp.read(emoji.url);
|
|
let ew = emojiImage.getWidth();
|
|
let ew = emojiImage.getWidth();
|
|
let eh = emojiImage.getHeight();
|
|
let eh = emojiImage.getHeight();
|
|
@@ -66,7 +64,6 @@ const CAPTION_OFFSET = 5;
|
|
|
|
|
|
async function getRandomCaption(type: FaceCaptionType) {
|
|
async function getRandomCaption(type: FaceCaptionType) {
|
|
let repo = getRepository(FaceCaptionMessage);
|
|
let repo = getRepository(FaceCaptionMessage);
|
|
-
|
|
|
|
let caption = await repo.query(`select message
|
|
let caption = await repo.query(`select message
|
|
from face_caption_message
|
|
from face_caption_message
|
|
where type = $1
|
|
where type = $1
|
|
@@ -81,9 +78,7 @@ async function captionFace(faces: cv.Rect[], data: Buffer) {
|
|
let padoru = Math.random() <= getPadoruChance();
|
|
let padoru = Math.random() <= getPadoruChance();
|
|
let face = faces[Math.floor(Math.random() * faces.length)];
|
|
let face = faces[Math.floor(Math.random() * faces.length)];
|
|
let squaredFace = await face.toSquareAsync();
|
|
let squaredFace = await face.toSquareAsync();
|
|
-
|
|
|
|
let targetSize = CAPTION_IMG_SIZE;
|
|
let targetSize = CAPTION_IMG_SIZE;
|
|
-
|
|
|
|
let img = await Jimp.read(data);
|
|
let img = await Jimp.read(data);
|
|
|
|
|
|
let tempImg = await Jimp.create(squaredFace.width, squaredFace.height);
|
|
let tempImg = await Jimp.create(squaredFace.width, squaredFace.height);
|
|
@@ -99,11 +94,8 @@ async function captionFace(faces: cv.Rect[], data: Buffer) {
|
|
tempImg = await tempImg.scale(targetSize / squaredFace.width);
|
|
tempImg = await tempImg.scale(targetSize / squaredFace.width);
|
|
|
|
|
|
let font = await Jimp.loadFont(padoru ? Jimp.FONT_SANS_16_WHITE : Jimp.FONT_SANS_16_BLACK);
|
|
let font = await Jimp.loadFont(padoru ? Jimp.FONT_SANS_16_WHITE : Jimp.FONT_SANS_16_BLACK);
|
|
-
|
|
|
|
let text = padoru ? "PADORU PADORU" : `${(await getRandomCaption(FaceCaptionType.PREFIX)).message} ${(await getRandomCaption(FaceCaptionType.POSTFIX)).message}`;
|
|
let text = padoru ? "PADORU PADORU" : `${(await getRandomCaption(FaceCaptionType.PREFIX)).message} ${(await getRandomCaption(FaceCaptionType.POSTFIX)).message}`;
|
|
-
|
|
|
|
let h = Jimp.measureTextHeight(font, text, targetSize - CAPTION_OFFSET * 2);
|
|
let h = Jimp.measureTextHeight(font, text, targetSize - CAPTION_OFFSET * 2);
|
|
-
|
|
|
|
let finalImage = await Jimp.create(targetSize, targetSize + h + CAPTION_OFFSET * 2, padoru ? "#FD2027" : "#FFFFFF");
|
|
let finalImage = await Jimp.create(targetSize, targetSize + h + CAPTION_OFFSET * 2, padoru ? "#FD2027" : "#FFFFFF");
|
|
|
|
|
|
finalImage = await finalImage.print(
|
|
finalImage = await finalImage.print(
|
|
@@ -130,12 +122,9 @@ function getPadoruChance() {
|
|
|
|
|
|
async function processFaceSwap(message: Message, attachmentUrl: string, processor?: ImageProcessor, failMessage?: string, successMessage?: string) {
|
|
async function processFaceSwap(message: Message, attachmentUrl: string, processor?: ImageProcessor, failMessage?: string, successMessage?: string) {
|
|
let data = await request(attachmentUrl, { encoding: null }) as Buffer;
|
|
let data = await request(attachmentUrl, { encoding: null }) as Buffer;
|
|
-
|
|
|
|
let im = await cv.imdecodeAsync(data, cv.IMREAD_COLOR);
|
|
let im = await cv.imdecodeAsync(data, cv.IMREAD_COLOR);
|
|
-
|
|
|
|
let gray = await im.cvtColorAsync(cv.COLOR_BGR2GRAY);
|
|
let gray = await im.cvtColorAsync(cv.COLOR_BGR2GRAY);
|
|
let normGray = await gray.equalizeHistAsync();
|
|
let normGray = await gray.equalizeHistAsync();
|
|
-
|
|
|
|
let animeFaces = await animeCascade.detectMultiScaleAsync(
|
|
let animeFaces = await animeCascade.detectMultiScaleAsync(
|
|
normGray,
|
|
normGray,
|
|
1.1,
|
|
1.1,
|
|
@@ -193,7 +182,6 @@ async function processFaceSwap(message: Message, attachmentUrl: string, processo
|
|
|
|
|
|
jimpImage.quality(90);
|
|
jimpImage.quality(90);
|
|
let buffer = await jimpImage.getBufferAsync(Jimp.MIME_JPEG);
|
|
let buffer = await jimpImage.getBufferAsync(Jimp.MIME_JPEG);
|
|
-
|
|
|
|
let messageContents =
|
|
let messageContents =
|
|
successMessage ||
|
|
successMessage ||
|
|
`I noticed a face in the image. I think this looks better ${client.emojis.get("505076258753740810").toString()}`;
|
|
`I noticed a face in the image. I think this looks better ${client.emojis.get("505076258753740810").toString()}`;
|