mmdhx commited on
Commit
f1ef6c1
·
verified ·
1 Parent(s): de6f5ec

Update egg-api.js

Browse files
Files changed (1) hide show
  1. egg-api.js +36 -20
egg-api.js CHANGED
@@ -7,7 +7,8 @@ const axios = require('axios');
7
  const sharp = require('sharp');
8
 
9
  const app = express();
10
- const port = 7860; // 将端口改为 7860
 
11
  // 从文件夹加载所有分类器
12
  async function loadClassifiersFromFolder(folderPath) {
13
  const classifiers = [];
@@ -42,7 +43,7 @@ async function downloadAndConvertImage(url) {
42
  }
43
 
44
  // 图像处理函数
45
- async function processImage(imageBuffer, classifiersFolder, replacementImagePath) {
46
  try {
47
  const classifiers = await loadClassifiersFromFolder(classifiersFolder);
48
 
@@ -64,22 +65,29 @@ async function processImage(imageBuffer, classifiersFolder, replacementImagePath
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) {
@@ -90,20 +98,28 @@ async function processImage(imageBuffer, classifiersFolder, replacementImagePath
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');
@@ -116,4 +132,4 @@ app.get('/process-image', async (req, res) => {
116
 
117
  app.listen(port, () => {
118
  console.log(`Server is running on http://localhost:${port}`);
119
- });
 
7
  const sharp = require('sharp');
8
 
9
  const app = express();
10
+ const port = 7860;
11
+
12
  // 从文件夹加载所有分类器
13
  async function loadClassifiersFromFolder(folderPath) {
14
  const classifiers = [];
 
43
  }
44
 
45
  // 图像处理函数
46
+ async function processImage(imageBuffer, classifiersFolder, replacementImageBuffer) {
47
  try {
48
  const classifiers = await loadClassifiersFromFolder(classifiersFolder);
49
 
 
65
  allFaces.push(...faces);
66
  });
67
 
68
+ const replacementFace = await Jimp.read(replacementImageBuffer);
69
  allFaces.forEach((faceRect, i) => {
70
+ const resizedReplacementFace = replacementFace.resize(faceRect.width, faceRect.height)
71
+ const faceRegion = img.getRegion(faceRect)
72
+
73
+ const replacementBuffer = resizedReplacementFace.bitmap.data
74
+ const centerX = faceRect.width / 2
75
+ const centerY = faceRect.height / 2
76
+ const maxRadius = Math.min(centerX, centerY)
77
  for (let y = 0; y < faceRect.height; y++) {
78
  for (let x = 0; x < faceRect.width; x++) {
79
+ const distance = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2)
80
+ if (distance <= maxRadius) {
81
+ const idx = (y * faceRect.width + x) << 2
82
+ const [r, g, b, a] = replacementBuffer.slice(idx, idx + 4)
83
+
84
+ if (a > 0) {
85
+ faceRegion.set(y, x, new cv.Vec3(b, g, r))
86
+ }
87
  }
88
  }
89
  }
90
+ })
 
91
  const outputBuffer = cv.imencode('.jpg', img);
92
  return outputBuffer;
93
  } catch (error) {
 
98
 
99
  // Express接口
100
  app.get('/process-image', async (req, res) => {
101
+ const { imageUrl, replacementImageUrl } = req.query;
102
 
103
+ if (!imageUrl || !replacementImageUrl) {
104
+ return res.status(400).send('Both image URL and replacement image URL are required');
105
  }
106
 
107
  const classifiersFolder = './classifiers';
108
+ let replacementImageBuffer;
 
109
  try {
110
+ // 下载并转换输入图片和替换图片到内存中
111
  const imageBuffer = await downloadAndConvertImage(imageUrl);
112
+ if (replacementImageUrl === 'replace') {
113
+ // 使用默认的本地替换图像路径
114
+ const defaultReplacementImagePath = './replacement_face.png';
115
+ replacementImageBuffer = await Jimp.read(defaultReplacementImagePath);
116
+ } else {
117
+ // 下载并转换替换图片到内存中
118
+ replacementImageBuffer = await downloadAndConvertImage(replacementImageUrl);
119
+ }
120
+
121
  // 处理图片
122
+ const outputBuffer = await processImage(imageBuffer, classifiersFolder, replacementImageBuffer);
123
 
124
  // 返回处理后的图片
125
  res.set('Content-Type', 'image/jpeg');
 
132
 
133
  app.listen(port, () => {
134
  console.log(`Server is running on http://localhost:${port}`);
135
+ });