|
|
@ -28,26 +28,19 @@ var upload = multer({ dest: 'public/uploads/' }); |
|
|
|
var progress = {}; |
|
|
|
var relationfile = {}; |
|
|
|
|
|
|
|
server.post('/generate', upload.fields([{ name: 'content_image', maxCount: 1 }, { name: 'style_image', maxCount: 1 }]), function (req, res) { |
|
|
|
let max_epoch = req.body.max_epoch; |
|
|
|
let md5 = crypto.createHash('md5'); |
|
|
|
|
|
|
|
console.log(req.files); |
|
|
|
console.log("Epoch = " + max_epoch); |
|
|
|
|
|
|
|
if (!req.files.content_image || !req.files.style_image) { |
|
|
|
progress[taskID] = {percentage: 0, max_epoch: 0, status: -2}; |
|
|
|
} |
|
|
|
var isIdle = true; |
|
|
|
var queue = []; |
|
|
|
var totalTask = 0; |
|
|
|
|
|
|
|
let taskID = Date.now().toString(16) + md5.update(req.files.content_image[0].filename + req.files.style_image[0].filename).digest('hex'); |
|
|
|
function run_child(taskID) { |
|
|
|
let child = exec(`python run_main.py --content ./public/uploads/${relationfile[taskID].content} --style ./public/uploads/${relationfile[taskID].style} --output ./public/uploads/${taskID}.jpg --num_iter ${progress[taskID].max_epoch}`); |
|
|
|
|
|
|
|
console.log(taskID + " start!"); |
|
|
|
console.log(`content: ${relationfile[taskID].content}`); |
|
|
|
console.log(`style: ${relationfile[taskID].style}`) |
|
|
|
console.log(`output: ${taskID}.jpg`); |
|
|
|
console.log(`num_iter = ${progress[taskID].max_epoch}`); |
|
|
|
|
|
|
|
relationfile[taskID] = {content: req.files.content_image[0].filename, style: req.files.style_image[0].filename}; |
|
|
|
|
|
|
|
let child = exec(`python run_main.py --content ./public/uploads/${req.files.content_image[0].filename} --style ./public/uploads/${req.files.style_image[0].filename} --output ./public/uploads/${taskID}.jpg --num_iter ${max_epoch}`); |
|
|
|
//console.log(`python run_main.py --content ./uploads/${req.files.content_image[0].filename} --style ./uploads/${req.files.style_image[0].filename} --output ./uploads/${taskID}.jpg --num_iter ${max_epoch}`);
|
|
|
|
|
|
|
|
progress[taskID] = {percentage: 0, max_epoch: parseInt(max_epoch), status: 0}; |
|
|
|
child.stdout.on("data", function (data) { |
|
|
|
let args = data.split(' '); |
|
|
|
let signal = args[0]; |
|
|
@ -69,8 +62,6 @@ server.post('/generate', upload.fields([{ name: 'content_image', maxCount: 1 }, |
|
|
|
progress[taskID].totalLoss = totalLoss; |
|
|
|
progress[taskID].ContentLoss = ContentLoss; |
|
|
|
progress[taskID].StyleLoss = StyleLoss; |
|
|
|
|
|
|
|
//console.log(taskID + ": " + args);
|
|
|
|
}); |
|
|
|
|
|
|
|
child.on("close", (code) => { |
|
|
@ -80,11 +71,45 @@ server.post('/generate', upload.fields([{ name: 'content_image', maxCount: 1 }, |
|
|
|
progress[taskID].status = -1; |
|
|
|
} |
|
|
|
console.log(taskID + " done!"); |
|
|
|
totalTask += 1; |
|
|
|
|
|
|
|
if (queue.length == 0) { |
|
|
|
isIdle = true; |
|
|
|
} else { |
|
|
|
let nextTaskID = queue.shift(); |
|
|
|
run_child(nextTaskID); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
server.post('/generate', upload.fields([{ name: 'content_image', maxCount: 1 }, { name: 'style_image', maxCount: 1 }]), function (req, res) { |
|
|
|
let max_epoch = req.body.max_epoch; |
|
|
|
let md5 = crypto.createHash('md5'); |
|
|
|
|
|
|
|
console.log(req.files); |
|
|
|
console.log("Epoch = " + max_epoch); |
|
|
|
|
|
|
|
if (!req.files.content_image || !req.files.style_image) { |
|
|
|
progress[taskID] = {percentage: 0, max_epoch: 0, status: -2}; |
|
|
|
} |
|
|
|
|
|
|
|
let taskID = Date.now().toString(16) + md5.update(req.files.content_image[0].filename + req.files.style_image[0].filename).digest('hex'); |
|
|
|
|
|
|
|
relationfile[taskID] = {content: req.files.content_image[0].filename, style: req.files.style_image[0].filename}; |
|
|
|
progress[taskID] = {percentage: 0, max_epoch: parseInt(max_epoch), status: 0, wait: totalTask + queue.length + 1}; |
|
|
|
|
|
|
|
if (isIdle) { |
|
|
|
isIdle = false; |
|
|
|
progress[taskID].wait -= 1; |
|
|
|
run_child(taskID); |
|
|
|
} else { |
|
|
|
queue.push(taskID); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let $ = cheerio.load(fs.readFileSync("./html/generate.html")); |
|
|
|
|
|
|
|
$('script[name="go"]').html(`setInterval(function() {getProgress("${taskID}");}, 1000);`); |
|
|
|
$('script[name="go"]').html(`getProgress("${taskID}"); setInterval(function() {getProgress("${taskID}");}, 1000);`); |
|
|
|
|
|
|
|
res.writeHead(200, { 'Content-type': 'text/html' }); |
|
|
|
res.end($.html()); |
|
|
@ -108,12 +133,17 @@ server.get('/success', function (req, res) { |
|
|
|
}); |
|
|
|
|
|
|
|
server.get('/fail', function (req, res) { |
|
|
|
|
|
|
|
res.writeHead(200, { 'Content-type': 'text/html' }); |
|
|
|
res.end(fs.readFileSync("./html/fail.html")); |
|
|
|
}); |
|
|
|
|
|
|
|
server.get('/progress', function (req, res) { |
|
|
|
if(req.query.taskID) { |
|
|
|
res.json(progress[req.query.taskID]); |
|
|
|
if(req.query.taskID && progress[req.query.taskID]) { |
|
|
|
let result_obj = {}; |
|
|
|
result_obj.percentage = progress[req.query.taskID].percentage; |
|
|
|
result_obj.wait = progress[req.query.taskID].wait - totalTask; |
|
|
|
result_obj.status = progress[req.query.taskID].status; |
|
|
|
res.json(result_obj); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|