-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageTreatment - Copy.js
109 lines (93 loc) · 2.72 KB
/
imageTreatment - Copy.js
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
99
100
101
102
103
104
105
106
107
108
109
/* * * * * * * * * * * * *\
* MODULES *
\* * * * * * * * * * * * */
var fs = require('fs');
var config = require('./config/config');
var request = require('request');
var FormData = require('form-data');
/* * * * * * * * * * * * *\
* VARIABLE GLOBAL *
\* * * * * * * * * * * * */
var path = './app/public/pictures/out.jpg';
var stream = fs.createReadStream(path);
const vpn = require('cisco-vpn')({
server: 'vpn.example.org',
username: process.env.USER,
password: process.env.PASSWORD
})
/* * * * * * * * * * * * *\
* FUNCTIONS *
\* * * * * * * * * * * * */
/* -- Save the picture taken from the video feed -- */
function savePicture (data) {
return new Promise(function(resolve, reject) {
data = data.replace(/^data:image\/png;base64,/, "");
fs.writeFile(path, data, 'base64', function(err) {
if(err){
console.log(err);
} else {
console.log("Saved");
resolve();
}
});
});
}
function classifyImage() {
return new Promise(function(resolve, reject) {
var options = {
url: process.env.POWERAI_URL,
method: 'POST',
rejectUnauthorized: false,
headers: {
"Content-Type": "multipart/form-data"
},
formData : {
"files" : fs.createReadStream(path),
"filename": "./app/public/pictures/out.jpg"
}
};
var data = [{
"alerte": false,
"xmin": "",
"xmax": "",
"ymin": "",
"ymax": "",
}];
request(options, function (error, response, body) {
if(error) console.error(error)
try {
var result = JSON.parse(response.body);
console.log(response.body+" LENGTH"+result.classified.length)
if (result.result == "success") {
if (result.classified.length > 0) {
for (var i = result.classified.length - 1; i >= 0; i--) {
if (result.classified[i].label == 'head') {
data[i].alerte = true;
console.log("XMIN" +result.classified[i].xmin)
data[i].xmin = result.classified[i].xmin;
data[i].xmax = result.classified[i].xmax;
data[i].ymin = result.classified[i].ymin;
data[i].ymax = result.classified[i].ymax;
} else {
console.log(data)
}
resolve(data)
}
} else {
resolve(data)
}
}
} catch(error) {
console.error(error);
data.alerte = "error";
resolve(data)
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
}
})
});
}
module.exports = {
classifyImage: classifyImage,
savePicture: savePicture
};