mmdhx commited on
Commit
f25dce2
·
verified ·
1 Parent(s): d9ae3dc

Update egg-api.js

Browse files
Files changed (1) hide show
  1. egg-api.js +53 -6
egg-api.js CHANGED
@@ -70,15 +70,20 @@ async function videoToGif(inputVideoPath, outputGifPath) {
70
  .run();
71
  });
72
  }
 
 
 
 
 
73
 
74
  // 处理图片的 Express 接口
75
  app.get('/process-image', async (req, res) => {
76
  try {
77
- const { imageUrl } = req.query;
78
  const tempDir = os.tmpdir();
79
  const tempImagePath = path.join(tempDir, 'temp_image.jpg');
 
80
  const outputImagePath = path.join(tempDir, 'output_image.jpg');
81
- const replacementImagePath = path.join(__dirname, 'replacement_face.png');
82
  const classifiersFolder = path.join(__dirname, 'classifiers');
83
 
84
  console.log('接收到图片处理请求:', imageUrl);
@@ -95,6 +100,25 @@ app.get('/process-image', async (req, res) => {
95
  writer.on('error', reject);
96
  });
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  console.log('图片下载完成:', tempImagePath);
99
 
100
  // 从文件夹动态加载分类器
@@ -113,7 +137,9 @@ app.get('/process-image', async (req, res) => {
113
  faces.push(...allFaces);
114
  });
115
 
116
- const replacementFace = await Jimp.read(replacementImagePath);
 
 
117
 
118
  if (Array.isArray(faces) && faces.length > 0) {
119
  console.log(`检测到 ${faces.length} 张脸`);
@@ -147,6 +173,7 @@ app.get('/process-image', async (req, res) => {
147
  } else {
148
  // 删除临时文件
149
  fs.unlinkSync(tempImagePath);
 
150
  fs.unlinkSync(outputImagePath);
151
  }
152
  });
@@ -159,9 +186,10 @@ app.get('/process-image', async (req, res) => {
159
  // 处理 GIF 的 Express 接口
160
  app.get('/processGif', async (req, res) => {
161
  try {
162
- const { gifUrl } = req.query;
163
  const tempDir = os.tmpdir();
164
  const tempGifPath = path.join(tempDir, 'temp.gif');
 
165
  const tempVideoPath = path.join(tempDir, 'temp_video.mp4');
166
  const outputGifPath = path.join(tempDir, 'output.gif');
167
  const replacementImagePath = path.join(__dirname, 'replacement_face.png');
@@ -181,6 +209,25 @@ app.get('/processGif', async (req, res) => {
181
  writer.on('error', reject);
182
  });
183
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  console.log('GIF下载完成:', tempGifPath);
185
 
186
  // 从文件夹动态加载分类器
@@ -211,7 +258,7 @@ app.get('/processGif', async (req, res) => {
211
  faces.push(...allfaces);
212
  });
213
 
214
- const replacementFace = await Jimp.read(replacementImagePath);
215
 
216
  if (Array.isArray(faces) && faces.length > 0) {
217
  console.log(`检测到 ${faces.length} 张脸`);
@@ -261,7 +308,7 @@ app.get('/processGif', async (req, res) => {
261
  });
262
  app.use(serveStatic(__dirname)); // 添加这一行,设置静态文件服务器
263
  app.get('/', (req, res) => {
264
- res.sendFile(path.join(__dirname, 'index.html')); // 修��这一行,发送HTML文件
265
  });
266
  // 启动 Express 服务器
267
  app.listen(PORT, () => {
 
70
  .run();
71
  });
72
  }
73
+ // 下载并转换图片到内存的辅助函数
74
+ async function downloadAndConvertImage(url) {
75
+ const response = await axios.get(url, { responseType: 'arraybuffer' });
76
+ return await Jimp.read(response.data);
77
+ }
78
 
79
  // 处理图片的 Express 接口
80
  app.get('/process-image', async (req, res) => {
81
  try {
82
+ const { imageUrl, replacementImageUrl } = req.query;
83
  const tempDir = os.tmpdir();
84
  const tempImagePath = path.join(tempDir, 'temp_image.jpg');
85
+ const tempImagePaths = path.join(tempDir, 'temp_images.jpg');
86
  const outputImagePath = path.join(tempDir, 'output_image.jpg');
 
87
  const classifiersFolder = path.join(__dirname, 'classifiers');
88
 
89
  console.log('接收到图片处理请求:', imageUrl);
 
100
  writer.on('error', reject);
101
  });
102
 
103
+ let replacementImageBuffer;
104
+ if (replacementImageUrl === 'replace') {
105
+ // 使用默认的本地替换图像路径
106
+ const defaultReplacementImagePath = './replacement_face.png';
107
+ replacementImageBuffer = await Jimp.read(defaultReplacementImagePath);
108
+ } else {
109
+ // 下载并转换替换图片到内存中
110
+ const response = await axios.get(replacementImageUrl, { responseType: 'stream' });
111
+ const writer = fs.createWriteStream(tempImagePaths);
112
+ response.data.pipe(writer);
113
+
114
+ // 等待文件写入完成
115
+ await new Promise((resolve, reject) => {
116
+ writer.on('finish', resolve);
117
+ writer.on('error', reject);
118
+ });
119
+
120
+ replacementImageBuffer = tempImagePaths;
121
+ }
122
  console.log('图片下载完成:', tempImagePath);
123
 
124
  // 从文件夹动态加载分类器
 
137
  faces.push(...allFaces);
138
  });
139
 
140
+
141
+
142
+ const replacementFace = replacementImageBuffer;
143
 
144
  if (Array.isArray(faces) && faces.length > 0) {
145
  console.log(`检测到 ${faces.length} 张脸`);
 
173
  } else {
174
  // 删除临时文件
175
  fs.unlinkSync(tempImagePath);
176
+ fs.unlinkSync(tempImagePaths);
177
  fs.unlinkSync(outputImagePath);
178
  }
179
  });
 
186
  // 处理 GIF 的 Express 接口
187
  app.get('/processGif', async (req, res) => {
188
  try {
189
+ const { gifUrl , replacementImageUrl } = req.query;
190
  const tempDir = os.tmpdir();
191
  const tempGifPath = path.join(tempDir, 'temp.gif');
192
+ const tempGifPaths = path.join(tempDir, 'temps.gif');
193
  const tempVideoPath = path.join(tempDir, 'temp_video.mp4');
194
  const outputGifPath = path.join(tempDir, 'output.gif');
195
  const replacementImagePath = path.join(__dirname, 'replacement_face.png');
 
209
  writer.on('error', reject);
210
  });
211
 
212
+ let replacementImageBuffer;
213
+ if (replacementImageUrl === 'replace') {
214
+ // 使用默认的本地替换图像路径
215
+ const defaultReplacementImagePath = './replacement_face.png';
216
+ replacementImageBuffer = await Jimp.read(defaultReplacementImagePath);
217
+ } else {
218
+ // 下载并转换替换图片到内存中
219
+ const response = await axios.get(replacementImageUrl, { responseType: 'stream' });
220
+ const writer = fs.createWriteStream(tempGifPaths);
221
+ response.data.pipe(writer);
222
+
223
+ // 等待文件写入完成
224
+ await new Promise((resolve, reject) => {
225
+ writer.on('finish', resolve);
226
+ writer.on('error', reject);
227
+ });
228
+
229
+ replacementImageBuffer = tempGifPaths;
230
+ }
231
  console.log('GIF下载完成:', tempGifPath);
232
 
233
  // 从文件夹动态加载分类器
 
258
  faces.push(...allfaces);
259
  });
260
 
261
+ const replacementFace =replacementImageBuffer;
262
 
263
  if (Array.isArray(faces) && faces.length > 0) {
264
  console.log(`检测到 ${faces.length} 张脸`);
 
308
  });
309
  app.use(serveStatic(__dirname)); // 添加这一行,设置静态文件服务器
310
  app.get('/', (req, res) => {
311
+ res.sendFile(path.join(__dirname, 'index.html')); // 修改这一行,发送HTML文件
312
  });
313
  // 启动 Express 服务器
314
  app.listen(PORT, () => {