Create faceswap.js
Browse files- plugins/faceswap.js +87 -0
plugins/faceswap.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const axios = require('axios');
|
| 2 |
+
const upload = require('../lib/uploadImage');
|
| 3 |
+
|
| 4 |
+
async function faceSwap(img_url1, img_url2) {
|
| 5 |
+
try {
|
| 6 |
+
const response1 = await axios.get(img_url1, { responseType: 'arraybuffer' });
|
| 7 |
+
const base64_1 = Buffer.from(response1.data).toString('base64');
|
| 8 |
+
|
| 9 |
+
const response2 = await axios.get(img_url2, { responseType: 'arraybuffer' });
|
| 10 |
+
const base64_2 = Buffer.from(response2.data).toString('base64');
|
| 11 |
+
|
| 12 |
+
const swapResponse = await axios.post('https://api.faceswapper.ai/swap', {
|
| 13 |
+
target: base64_1,
|
| 14 |
+
source: base64_2,
|
| 15 |
+
type: 'invisible',
|
| 16 |
+
id: 'faceswapper'
|
| 17 |
+
}, {
|
| 18 |
+
headers: {
|
| 19 |
+
'Accept': 'application/json, text/plain, */*',
|
| 20 |
+
'Content-Type': 'application/json',
|
| 21 |
+
'type': 'rapid'
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
const base64Data = swapResponse.data.result.replace(/^data:image\/\w+;base64,/, '');
|
| 26 |
+
const buffer = Buffer.from(base64Data, 'base64');
|
| 27 |
+
|
| 28 |
+
const imageUrl = await upload(buffer);
|
| 29 |
+
|
| 30 |
+
return {
|
| 31 |
+
success: true,
|
| 32 |
+
elapsedTime: swapResponse.data.elapsedTime,
|
| 33 |
+
url: imageUrl
|
| 34 |
+
};
|
| 35 |
+
|
| 36 |
+
} catch (error) {
|
| 37 |
+
throw error;
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
const handler = async (req, res) => {
|
| 42 |
+
try {
|
| 43 |
+
const { img1, img2 } = req.query;
|
| 44 |
+
|
| 45 |
+
if (!img1) {
|
| 46 |
+
return res.status(400).json({
|
| 47 |
+
success: false,
|
| 48 |
+
error: 'Missing required parameter: img1'
|
| 49 |
+
});
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
if (!img2) {
|
| 53 |
+
return res.status(400).json({
|
| 54 |
+
success: false,
|
| 55 |
+
error: 'Missing required parameter: img2'
|
| 56 |
+
});
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
const result = await faceSwap(img1, img2);
|
| 60 |
+
|
| 61 |
+
return res.json({
|
| 62 |
+
author: "Herza",
|
| 63 |
+
success: true,
|
| 64 |
+
data: result.url
|
| 65 |
+
});
|
| 66 |
+
|
| 67 |
+
} catch (error) {
|
| 68 |
+
res.status(500).json({
|
| 69 |
+
success: false,
|
| 70 |
+
error: error.message,
|
| 71 |
+
timestamp: new Date().toISOString()
|
| 72 |
+
});
|
| 73 |
+
}
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
module.exports = {
|
| 77 |
+
name: 'Face Swap',
|
| 78 |
+
description: 'Swap faces between two images using AI',
|
| 79 |
+
type: 'GET',
|
| 80 |
+
routes: ['api/ai/faceswap'],
|
| 81 |
+
tags: ['AI', 'tools'],
|
| 82 |
+
parameters: ['img1', 'img2', 'key'],
|
| 83 |
+
limit: 3,
|
| 84 |
+
enabled: true,
|
| 85 |
+
main: ['ai', 'tools'],
|
| 86 |
+
handler
|
| 87 |
+
};
|