|
const express = require('express'); |
|
const https = require('https'); |
|
const fs = require('fs'); |
|
const { exec } = require('child_process'); |
|
const path = require('path'); |
|
|
|
const app = express(); |
|
app.use(express.json({ limit: '250mb' })); |
|
|
|
app.post('/', (req, res) => { |
|
const { image } = req.body; |
|
|
|
if (!image) { |
|
return res.status(400).json({ error: 'No image provided' }); |
|
} |
|
|
|
|
|
const imageBuffer = Buffer.from(image, 'base64'); |
|
const tempImagePath = path.join(__dirname, 'temp_image.png'); |
|
fs.writeFileSync(tempImagePath, imageBuffer); |
|
|
|
const command = `python process.py --image "${tempImagePath}" --cuda`; |
|
exec(command, (error, stdout, stderr) => { |
|
if (error) { |
|
console.error(`exec error: ${error}`); |
|
return res.status(500).json({ error: stderr }); |
|
} |
|
|
|
const outputDir = '/work/huggingface-cloth-segmentation/output'; |
|
const outputData = {}; |
|
|
|
|
|
const alphaDir = path.join(outputDir, 'alpha'); |
|
const alphaFiles = fs.readdirSync(alphaDir); |
|
alphaFiles.forEach(file => { |
|
const filePath = path.join(alphaDir, file); |
|
const fileBuffer = fs.readFileSync(filePath); |
|
const base64String = fileBuffer.toString('base64'); |
|
outputData[file] = base64String; |
|
}); |
|
|
|
const clothSegDir = path.join(outputDir, 'cloth_seg'); |
|
const finalSegPath = path.join(clothSegDir, 'final_seg.png'); |
|
const finalSegBuffer = fs.readFileSync(finalSegPath); |
|
const finalSegBase64 = finalSegBuffer.toString('base64'); |
|
outputData['final_seg.png'] = finalSegBase64; |
|
|
|
res.status(200).json(outputData); |
|
}); |
|
}); |
|
|
|
const sslOptions = { |
|
key: fs.readFileSync('/etc/letsencrypt/live/aceadoorn.smartpickai.com/privkey.pem'), |
|
cert: fs.readFileSync('/etc/letsencrypt/live/aceadoorn.smartpickai.com/fullchain.pem') |
|
}; |
|
|
|
|
|
https.createServer(sslOptions, app).listen(8188, () => { |
|
console.log('HTTPS Server running on port 8188'); |
|
}); |
|
|