Update plugins/faceswap.js
Browse files- plugins/faceswap.js +50 -20
plugins/faceswap.js
CHANGED
|
@@ -1,8 +1,38 @@
|
|
| 1 |
const axios = require('axios');
|
| 2 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -10,33 +40,29 @@ async function faceSwap(img_url1, img_url2) {
|
|
| 10 |
const base64_2 = Buffer.from(response2.data).toString('base64');
|
| 11 |
|
| 12 |
const swapResponse = await axios.post('https://api.faceswapper.ai/swap', {
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
}, {
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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
|
| 29 |
|
| 30 |
return {
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
};
|
| 36 |
-
|
| 37 |
-
} catch (error) {
|
| 38 |
-
throw error;
|
| 39 |
-
}
|
| 40 |
}
|
| 41 |
|
| 42 |
const handler = async (req, res) => {
|
|
@@ -45,6 +71,7 @@ const handler = async (req, res) => {
|
|
| 45 |
|
| 46 |
if (!img) {
|
| 47 |
return res.status(400).json({
|
|
|
|
| 48 |
success: false,
|
| 49 |
error: 'Missing required parameter: img'
|
| 50 |
});
|
|
@@ -52,6 +79,7 @@ const handler = async (req, res) => {
|
|
| 52 |
|
| 53 |
if (!img2) {
|
| 54 |
return res.status(400).json({
|
|
|
|
| 55 |
success: false,
|
| 56 |
error: 'Missing required parameter: img2'
|
| 57 |
});
|
|
@@ -59,6 +87,7 @@ const handler = async (req, res) => {
|
|
| 59 |
|
| 60 |
if (!key) {
|
| 61 |
return res.status(400).json({
|
|
|
|
| 62 |
success: false,
|
| 63 |
error: 'Missing required parameter: key'
|
| 64 |
});
|
|
@@ -78,6 +107,7 @@ const handler = async (req, res) => {
|
|
| 78 |
|
| 79 |
} catch (error) {
|
| 80 |
res.status(500).json({
|
|
|
|
| 81 |
success: false,
|
| 82 |
error: error.message,
|
| 83 |
timestamp: new Date().toISOString()
|
|
|
|
| 1 |
const axios = require('axios');
|
| 2 |
+
const FormData = require('form-data');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
|
| 6 |
+
async function uploadToTmpFiles(buffer, filename) {
|
| 7 |
+
const tempDir = path.join(__dirname, '..', 'temp');
|
| 8 |
+
|
| 9 |
+
if (!fs.existsSync(tempDir)) {
|
| 10 |
+
fs.mkdirSync(tempDir, { recursive: true });
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
const tempFile = path.join(tempDir, filename);
|
| 14 |
+
fs.writeFileSync(tempFile, buffer);
|
| 15 |
+
|
| 16 |
+
const form = new FormData();
|
| 17 |
+
form.append('file', fs.createReadStream(tempFile));
|
| 18 |
+
|
| 19 |
+
const response = await axios.post('https://tmpfiles.org/api/v1/upload', form, {
|
| 20 |
+
headers: {
|
| 21 |
+
...form.getHeaders()
|
| 22 |
+
}
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
if (fs.existsSync(tempFile)) {
|
| 26 |
+
fs.unlinkSync(tempFile);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const uploadUrl = response.data.data.url;
|
| 30 |
+
const directUrl = uploadUrl.replace('tmpfiles.org/', 'tmpfiles.org/dl/');
|
| 31 |
+
|
| 32 |
+
return directUrl;
|
| 33 |
+
}
|
| 34 |
|
| 35 |
async function faceSwap(img_url1, img_url2) {
|
|
|
|
| 36 |
const response1 = await axios.get(img_url1, { responseType: 'arraybuffer' });
|
| 37 |
const base64_1 = Buffer.from(response1.data).toString('base64');
|
| 38 |
|
|
|
|
| 40 |
const base64_2 = Buffer.from(response2.data).toString('base64');
|
| 41 |
|
| 42 |
const swapResponse = await axios.post('https://api.faceswapper.ai/swap', {
|
| 43 |
+
target: base64_1,
|
| 44 |
+
source: base64_2,
|
| 45 |
+
type: 'invisible',
|
| 46 |
+
id: 'faceswapper'
|
| 47 |
}, {
|
| 48 |
+
headers: {
|
| 49 |
+
'Accept': 'application/json, text/plain, */*',
|
| 50 |
+
'Content-Type': 'application/json',
|
| 51 |
+
'type': 'rapid'
|
| 52 |
+
}
|
| 53 |
});
|
| 54 |
|
| 55 |
const base64Data = swapResponse.data.result.replace(/^data:image\/\w+;base64,/, '');
|
| 56 |
const buffer = Buffer.from(base64Data, 'base64');
|
| 57 |
|
| 58 |
+
const imageUrl = await uploadToTmpFiles(buffer, `faceswap_${Date.now()}.png`);
|
| 59 |
|
| 60 |
return {
|
| 61 |
+
success: true,
|
| 62 |
+
elapsedTime: swapResponse.data.elapsedTime,
|
| 63 |
+
result: swapResponse.data.result,
|
| 64 |
+
url: imageUrl
|
| 65 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
const handler = async (req, res) => {
|
|
|
|
| 71 |
|
| 72 |
if (!img) {
|
| 73 |
return res.status(400).json({
|
| 74 |
+
author: "Herza",
|
| 75 |
success: false,
|
| 76 |
error: 'Missing required parameter: img'
|
| 77 |
});
|
|
|
|
| 79 |
|
| 80 |
if (!img2) {
|
| 81 |
return res.status(400).json({
|
| 82 |
+
author: "Herza",
|
| 83 |
success: false,
|
| 84 |
error: 'Missing required parameter: img2'
|
| 85 |
});
|
|
|
|
| 87 |
|
| 88 |
if (!key) {
|
| 89 |
return res.status(400).json({
|
| 90 |
+
author: "Herza",
|
| 91 |
success: false,
|
| 92 |
error: 'Missing required parameter: key'
|
| 93 |
});
|
|
|
|
| 107 |
|
| 108 |
} catch (error) {
|
| 109 |
res.status(500).json({
|
| 110 |
+
author: "Herza",
|
| 111 |
success: false,
|
| 112 |
error: error.message,
|
| 113 |
timestamp: new Date().toISOString()
|