index.js 1.4 KB
var express = require('express');
var router = express.Router();
var multer = require("multer");
var path = require("path");
var PythonShell = require('python-shell');
var spawn = require("child_process").spawn;


var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, "../deep_sort_yolov4/")
    },
    filename: function(req, file, callback) {
        var extension = path.extname(file.originalname);
        var basename = path.basename(file.originalname, extension);
        callback(null, basename + extension);
    }
})

// 1. multer 미들웨어 등록
var upload = multer({
    storage: storage
})

// 뷰 페이지 경로
router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
});

// 2. 파일 업로드 처리
router.post('/create', upload.single("File"), function(req, res) {
    // 3. 파일 객체
    var file = req.file;
    var options = {
        mode: 'text',
        pythonPath: __dirname + '/../../venv/Scripts/python.exe',
        pythonOptions: ['-u'],
        scriptPath: __dirname + '/../../deep_sort_yolov4/',
        args: [file.originalname]
    };
    var shell1 = new PythonShell.PythonShell('main.py', options);
    shell1.end(function(err) {
        if (err) throw err;
        else {
            res.render('result', { file_name: file.originalname.split('.')[0] });
        }
    });
});
module.exports = router;