| const axios = require('axios'); | |
| const upload = require('../lib/uploadImage'); | |
| async function faceSwap(img_url1, img_url2) { | |
| try { | |
| const response1 = await axios.get(img_url1, { responseType: 'arraybuffer' }); | |
| const base64_1 = Buffer.from(response1.data).toString('base64'); | |
| const response2 = await axios.get(img_url2, { responseType: 'arraybuffer' }); | |
| const base64_2 = Buffer.from(response2.data).toString('base64'); | |
| const swapResponse = await axios.post('https://api.faceswapper.ai/swap', { | |
| target: base64_1, | |
| source: base64_2, | |
| type: 'invisible', | |
| id: 'faceswapper' | |
| }, { | |
| headers: { | |
| 'Accept': 'application/json, text/plain, */*', | |
| 'Content-Type': 'application/json', | |
| 'type': 'rapid' | |
| } | |
| }); | |
| const base64Data = swapResponse.data.result.replace(/^data:image\/\w+;base64,/, ''); | |
| const buffer = Buffer.from(base64Data, 'base64'); | |
| const imageUrl = await upload(buffer); | |
| return { | |
| success: true, | |
| elapsedTime: swapResponse.data.elapsedTime, | |
| result: swapResponse.data.result, | |
| url: imageUrl | |
| }; | |
| } catch (error) { | |
| throw error; | |
| } | |
| } | |
| const handler = async (req, res) => { | |
| try { | |
| const { img, img2, key } = req.query; | |
| if (!img) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Missing required parameter: img' | |
| }); | |
| } | |
| if (!img2) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Missing required parameter: img2' | |
| }); | |
| } | |
| if (!key) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Missing required parameter: key' | |
| }); | |
| } | |
| const result = await faceSwap(img, img2); | |
| return res.json({ | |
| author: "Herza", | |
| success: true, | |
| data: { | |
| elapsedTime: result.elapsedTime, | |
| result: result.result, | |
| url: result.url | |
| } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message, | |
| timestamp: new Date().toISOString() | |
| }); | |
| } | |
| }; | |
| module.exports = { | |
| name: 'Face Swap', | |
| description: 'Swap faces between two images using AI', | |
| type: 'GET', | |
| routes: ['api/AI/faceswap'], | |
| tags: ['AI', 'tools'], | |
| parameters: ['img', 'img2', 'key'], | |
| limit: 3, | |
| enabled: true, | |
| main: ['AI', 'tools'], | |
| handler | |
| }; |