-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageProcessing.js
141 lines (120 loc) · 4.39 KB
/
imageProcessing.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* * * * * * * * * * * * *\
* MODULES *
\* * * * * * * * * * * * */
var fs = require('fs');
var config = require('./config/config');
var request = require('request');
var FormData = require('form-data');
/************************\
* IBM i Db2 for i *
\************************/
const {dbconn, dbstmt} = require('idb-connector');
var latestInsert=0;
/* * * * * * * * * * * * *\
* 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("New Image received : saved in the IFS");
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 = [];
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--) {
tempData={
"alerte": false,
"xmin": "",
"xmax": "",
"ymin": "",
"ymax": "",
}
if (result.classified[i].label == 'head') {
tempData.alerte = true;
//console.log("XMIN" +result.classified[i].xmin)
tempData.xmin = result.classified[i].xmin;
tempData.xmax = result.classified[i].xmax;
tempData.ymin = result.classified[i].ymin;
tempData.ymax = result.classified[i].ymax;
data.push(tempData);
//sSql = 'INSERT INTO AIVISION.DETECTIONS';
sqlInsert="INSERT INTO AIVISION.DETECTIONS(detectedLabel, confidence,ymax,xmax, xmin,ymin, attr,webAPI,imageUrl) VALUES ('"+result.classified[i].label+"',"+result.classified[i].confidence+","+tempData.ymax+","+tempData.xmax+","+tempData.xmin+","+tempData.ymin+",'empty','"+result.webAPIId+"','"+ result.imageUrl+"')";
detectionTS= new Date().getTime();
//console.log(detectionTS, latestInsert);
if (detectionTS - latestInsert > 10000) {
console.log("Object Detected!!( 10 second sampling) : Insert in Db2 for i AIVISION Database "+sqlInsert );
connection = new dbconn();
connection.conn('*LOCAL');
//#statement = new dbstmt(connection);
//#res1=statement.execSync('select count(*) from aivision.detections');
//#console.log(`Result Set: ${JSON.stringify(res1)}`);
//#statement.close();
statement = new dbstmt(connection);
res2=statement.execSync(sqlInsert);
statement.close();
delete statement;
connection.disconn();
connection.close();
latestInsert=detectionTS;
}
} 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
};