Update egg-api.js
Browse files- egg-api.js +91 -100
egg-api.js
CHANGED
|
@@ -8,121 +8,112 @@ const sharp = require('sharp');
|
|
| 8 |
|
| 9 |
const app = express();
|
| 10 |
const port = 7860; // 将端口改为 7860
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
-
//
|
| 19 |
-
function
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
-
//
|
| 38 |
-
function
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
| 55 |
});
|
| 56 |
-
}
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
if (err) {
|
| 67 |
-
callback(err);
|
| 68 |
-
return;
|
| 69 |
-
}
|
| 70 |
-
Jimp.read(replacementImagePath, (err, replacementFace) => {
|
| 71 |
-
if (err) {
|
| 72 |
-
callback(err);
|
| 73 |
-
return;
|
| 74 |
-
}
|
| 75 |
-
const grayImg = img.bgrToGray();
|
| 76 |
-
const allFaces = [];
|
| 77 |
-
classifiers.forEach(classifier => {
|
| 78 |
-
const faces = classifier.detectMultiScale(grayImg).objects;
|
| 79 |
-
allFaces.push(...faces);
|
| 80 |
-
});
|
| 81 |
-
|
| 82 |
-
allFaces.forEach(faceRect => {
|
| 83 |
-
const resizedReplacementFace = replacementFace.resize(faceRect.width, faceRect.height);
|
| 84 |
-
const faceRegion = img.getRegion(faceRect);
|
| 85 |
-
const replacementBuffer = resizedReplacementFace.bitmap.data;
|
| 86 |
-
for (let y = 0; y < faceRect.height; y++) {
|
| 87 |
-
for (let x = 0; x < faceRect.width; x++) {
|
| 88 |
-
const idx = (y * faceRect.width + x) << 2;
|
| 89 |
-
const [r, g, b, a] = replacementBuffer.slice(idx, idx + 4);
|
| 90 |
-
if (a > 0) {
|
| 91 |
-
faceRegion.set(y, x, new cv.Vec3(b, g, r));
|
| 92 |
-
}
|
| 93 |
-
}
|
| 94 |
-
}
|
| 95 |
-
});
|
| 96 |
-
|
| 97 |
-
cv.imwrite(outputImagePath, img);
|
| 98 |
-
callback(null);
|
| 99 |
-
});
|
| 100 |
-
});
|
| 101 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
// Express接口
|
| 105 |
-
app.get('/process-image', (req, res) => {
|
| 106 |
-
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
const replacementImagePath = './replacement_face.png';
|
| 115 |
-
const classifiersFolder = './classifiers';
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
});
|
| 125 |
|
| 126 |
app.listen(port, () => {
|
| 127 |
-
|
| 128 |
});
|
|
|
|
| 8 |
|
| 9 |
const app = express();
|
| 10 |
const port = 7860; // 将端口改为 7860
|
| 11 |
+
// 从文件夹加载所有分类器
|
| 12 |
+
async function loadClassifiersFromFolder(folderPath) {
|
| 13 |
+
const classifiers = [];
|
| 14 |
+
const files = fs.readdirSync(folderPath);
|
| 15 |
+
for (const file of files) {
|
| 16 |
+
const filePath = path.join(folderPath, file);
|
| 17 |
+
const classifier = new cv.CascadeClassifier(filePath);
|
| 18 |
+
classifiers.push(classifier);
|
| 19 |
+
}
|
| 20 |
+
return classifiers;
|
| 21 |
}
|
| 22 |
|
| 23 |
+
// 下载并转换图像到内存中
|
| 24 |
+
async function downloadAndConvertImage(url) {
|
| 25 |
+
try {
|
| 26 |
+
const response = await axios({
|
| 27 |
+
method: 'get',
|
| 28 |
+
url: url,
|
| 29 |
+
responseType: 'arraybuffer'
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
const imageBuffer = await sharp(response.data)
|
| 33 |
+
.toFormat('jpeg')
|
| 34 |
+
.toBuffer();
|
| 35 |
+
|
| 36 |
+
console.log('Image downloaded and converted successfully.');
|
| 37 |
+
return imageBuffer;
|
| 38 |
+
} catch (error) {
|
| 39 |
+
console.error('Error downloading or converting image:', error);
|
| 40 |
+
throw error;
|
| 41 |
+
}
|
| 42 |
}
|
| 43 |
|
| 44 |
+
// 图像处理函数
|
| 45 |
+
async function processImage(imageBuffer, classifiersFolder, replacementImagePath) {
|
| 46 |
+
try {
|
| 47 |
+
const classifiers = await loadClassifiersFromFolder(classifiersFolder);
|
| 48 |
+
|
| 49 |
+
// 使用 Jimp 验证图片文件是否有效
|
| 50 |
+
const jimpImage = await Jimp.read(imageBuffer);
|
| 51 |
+
if (!jimpImage) {
|
| 52 |
+
throw new Error('Failed to read the image with Jimp. The image file might be corrupted or not supported.');
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
const img = cv.imdecode(imageBuffer);
|
| 56 |
+
if (img.empty) {
|
| 57 |
+
throw new Error('Failed to read the image with OpenCV. The image file might be corrupted or not supported.');
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
const grayImg = img.bgrToGray();
|
| 61 |
+
const allFaces = [];
|
| 62 |
+
classifiers.forEach(classifier => {
|
| 63 |
+
const faces = classifier.detectMultiScale(grayImg).objects;
|
| 64 |
+
allFaces.push(...faces);
|
| 65 |
});
|
|
|
|
| 66 |
|
| 67 |
+
const replacementFace = await Jimp.read(replacementImagePath);
|
| 68 |
+
allFaces.forEach((faceRect, i) => {
|
| 69 |
+
const resizedReplacementFace = replacementFace.resize(faceRect.width, faceRect.height);
|
| 70 |
+
const faceRegion = img.getRegion(faceRect);
|
| 71 |
+
const replacementBuffer = resizedReplacementFace.bitmap.data;
|
| 72 |
+
for (let y = 0; y < faceRect.height; y++) {
|
| 73 |
+
for (let x = 0; x < faceRect.width; x++) {
|
| 74 |
+
const idx = (y * faceRect.width + x) << 2;
|
| 75 |
+
const [r, g, b, a] = replacementBuffer.slice(idx, idx + 4);
|
| 76 |
+
if (a > 0) {
|
| 77 |
+
faceRegion.set(y, x, new cv.Vec3(b, g, r));
|
| 78 |
+
}
|
| 79 |
}
|
| 80 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
});
|
| 82 |
+
|
| 83 |
+
const outputBuffer = cv.imencode('.jpg', img);
|
| 84 |
+
return outputBuffer;
|
| 85 |
+
} catch (error) {
|
| 86 |
+
console.error('Error during image processing:', error);
|
| 87 |
+
throw error;
|
| 88 |
+
}
|
| 89 |
}
|
| 90 |
|
| 91 |
// Express接口
|
| 92 |
+
app.get('/process-image', async (req, res) => {
|
| 93 |
+
const { imageUrl } = req.query;
|
| 94 |
|
| 95 |
+
if (!imageUrl) {
|
| 96 |
+
return res.status(400).send('Image URL is required');
|
| 97 |
+
}
|
| 98 |
|
| 99 |
+
const classifiersFolder = './classifiers';
|
| 100 |
+
const replacementImagePath = './replacement_face.png';
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
try {
|
| 103 |
+
// 下载并转换输入图片到内存中
|
| 104 |
+
const imageBuffer = await downloadAndConvertImage(imageUrl);
|
| 105 |
+
// 处理图片
|
| 106 |
+
const outputBuffer = await processImage(imageBuffer, classifiersFolder, replacementImagePath);
|
| 107 |
+
|
| 108 |
+
// 返回处理后的图片
|
| 109 |
+
res.set('Content-Type', 'image/jpeg');
|
| 110 |
+
res.send(outputBuffer);
|
| 111 |
+
} catch (error) {
|
| 112 |
+
console.error('Error processing image:', error);
|
| 113 |
+
res.status(500).send('Error processing image');
|
| 114 |
+
}
|
| 115 |
});
|
| 116 |
|
| 117 |
app.listen(port, () => {
|
| 118 |
+
console.log(`Server is running on http://localhost:${port}`);
|
| 119 |
});
|