apiRouter.js
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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;