Delete egg-api.js
Browse files- egg-api.js +0 -93
egg-api.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
const express = require('express');
|
| 2 |
-
const fs = require('fs');
|
| 3 |
-
const path = require('path');
|
| 4 |
-
const cv = require('opencv4nodejs');
|
| 5 |
-
const Jimp = require('jimp');
|
| 6 |
-
const axios = require('axios');
|
| 7 |
-
const sharp = require('sharp');
|
| 8 |
-
|
| 9 |
-
const app = express();
|
| 10 |
-
const port = 7860;
|
| 11 |
-
const outputDir = './output';
|
| 12 |
-
const classifiersFolder = './classifiers';
|
| 13 |
-
const replacementImagePath = './replacement_face.png';
|
| 14 |
-
|
| 15 |
-
// 确保输出目录存在
|
| 16 |
-
if (!fs.existsSync(outputDir)){
|
| 17 |
-
fs.mkdirSync(outputDir);
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
// 从文件夹加载所有分类器
|
| 21 |
-
async function loadClassifiersFromFolder(folderPath) {
|
| 22 |
-
const classifiers = [];
|
| 23 |
-
const files = fs.readdirSync(folderPath);
|
| 24 |
-
for (const file of files) {
|
| 25 |
-
const filePath = path.join(folderPath, file);
|
| 26 |
-
const classifier = new cv.CascadeClassifier(filePath);
|
| 27 |
-
classifiers.push(classifier);
|
| 28 |
-
}
|
| 29 |
-
return classifiers;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
// 下载图片并转换格式
|
| 33 |
-
async function downloadAndConvertImage(url, dest) {
|
| 34 |
-
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
| 35 |
-
await sharp(response.data).toFormat('jpeg').toFile(dest);
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
// 图像处理函数
|
| 39 |
-
async function processImage(inputImagePath, outputImagePath) {
|
| 40 |
-
const classifiers = await loadClassifiersFromFolder(classifiersFolder);
|
| 41 |
-
const img = cv.imread(inputImagePath);
|
| 42 |
-
const grayImg = img.bgrToGray();
|
| 43 |
-
const replacementFace = await Jimp.read(replacementImagePath);
|
| 44 |
-
|
| 45 |
-
classifiers.forEach(classifier => {
|
| 46 |
-
const faces = classifier.detectMultiScale(grayImg).objects;
|
| 47 |
-
faces.forEach(faceRect => {
|
| 48 |
-
const resizedReplacementFace = replacementFace.resize(faceRect.width, faceRect.height);
|
| 49 |
-
const faceRegion = img.getRegion(faceRect);
|
| 50 |
-
const replacementBuffer = resizedReplacementFace.bitmap.data;
|
| 51 |
-
for (let y = 0; y < faceRect.height; y++) {
|
| 52 |
-
for (let x = 0; x < faceRect.width; x++) {
|
| 53 |
-
const idx = (y * faceRect.width + x) << 2;
|
| 54 |
-
const [r, g, b, a] = replacementBuffer.slice(idx, idx + 4);
|
| 55 |
-
if (a > 0) {
|
| 56 |
-
faceRegion.set(y, x, new cv.Vec3(b, g, r));
|
| 57 |
-
}
|
| 58 |
-
}
|
| 59 |
-
}
|
| 60 |
-
});
|
| 61 |
-
});
|
| 62 |
-
|
| 63 |
-
cv.imwrite(outputImagePath, img);
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
// Express接口
|
| 67 |
-
app.get('/process-image', async (req, res) => {
|
| 68 |
-
try {
|
| 69 |
-
const { imageUrl } = req.query;
|
| 70 |
-
if (!imageUrl) {
|
| 71 |
-
return res.status(400).send('Image URL is required');
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
const inputImagePath = path.join(outputDir, 'test.jpg'); // 使用jpg格式保存
|
| 75 |
-
const outputImagePath = path.join(outputDir, 'image_with_replaced_faces.jpg');
|
| 76 |
-
|
| 77 |
-
// 下载并转换输入图片
|
| 78 |
-
await downloadAndConvertImage(imageUrl, inputImagePath);
|
| 79 |
-
|
| 80 |
-
// 处理图片
|
| 81 |
-
await processImage(inputImagePath, outputImagePath);
|
| 82 |
-
|
| 83 |
-
// 返回处理后的图片链接
|
| 84 |
-
res.sendFile(outputImagePath, { root: '.' });
|
| 85 |
-
} catch (error) {
|
| 86 |
-
console.error('Error processing image:', error);
|
| 87 |
-
res.status(500).send('Error processing image');
|
| 88 |
-
}
|
| 89 |
-
});
|
| 90 |
-
|
| 91 |
-
app.listen(port, () => {
|
| 92 |
-
console.log(`Server is running on http://localhost:${port}`);
|
| 93 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|