apiRouter.js 3.05 KB
const express = require('express');
const fs = require('fs');
const ffmpeg = require('ffmpeg');
const request = require('request')

const router = express.Router();

router.post('/videoResult', function (req, res) {

    try {
        let preview = req.body[0].preview;

        str = preview.replace(/^data:(.*?);base64,/, "");
        str = str.replace(/ /g, '+');

        fs.writeFileSync(`./data/temp.mp4`, str, 'base64', function (err) {
            if (err) throw err;
            console.log("video saved");
            return;
        })

        let process = new ffmpeg(`./data/temp.mp4`);

        process.then(function (video) {
            video.fnExtractFrameToJPG(__dirname + "/data",
                {
                    every_n_seconds: 1,
                    file_name: 'frame_%s'
                }, function (error, files) {
                    if (!error)
                    for(var i=0;i<files.length-1;i++){
                        let base64str = base64_encode(files[i]);
                        request.post({
                            url: 'http://101.101.210.73/process',
                            form: {
                                'data': base64str,
                                'index': i
                            },
                            json: true
                        }, (err, response, body) => {
                            console.log(body)
                            var index=body['index'];
                            if(body['unknown_person'] == true){
                                save_Result_image((files[index]), 'unknown', index);
                            }
                            if(body['fire_broken'] == true){
                                save_Result_image((files[index]), 'fire', index);
                            }
                        })
                    }
                }
            )
        })

    } catch (err) {
        console.error(err);
    }

    return res.json({ data: true });
});

router.post('/saveImage', function (req, res) {
    try {
        let preview = req.body[0].preview;
        str = preview.replace(/^data:(.*?);base64,/, "");
        base64str = str.replace(/ /g, '+');
        request.post({
            url: 'http://101.101.210.73/save',
            form: {
                'data': base64str,
                'name': 'temp'
            },
            json: true
        }, (err, response, body) => {
            console.log(body);
        })
    } catch (err) {
        console.error(err);
    }
    return res.json({ data: true });
});

function base64_encode(file) {
    // read binary data
    var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string
    return new Buffer(bitmap).toString('base64');
}

function save_Result_image(file, type, index){
    var bitmap = fs.readFileSync(file);
    var filePath = './' + type + '/' + index + '.jpg';
    fs.writeFile(filePath, bitmap, function(err){
        console.log('save');
    });
}

module.exports = router;