-
Notifications
You must be signed in to change notification settings - Fork 2
/
leo.js
276 lines (220 loc) · 9.12 KB
/
leo.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//Load Node Modules
var archiver = require('archiver');
var formidable = require('formidable');
var request = require('request');
var uuid = require('node-uuid');
var fs = require('fs');
var path = require('path');
//Load Local Modules
config = require('./config.json')
var __dirname = config.SmartShop.imgDir;
console.log(__dirname);
module.exports = {
UpdateVectorsBase :function (){
return UpdateVectorsBase();
},
GetSimilarItems: function(res,callback){
return GetSimilarItems(res, callback);
console.log('GetSimilarItems');
}
}
function UpdateVectorsBase(){
/*
This functions reads all images in the folder set in the configuration file,
creates a zip, calls SAP Leonardo Image Feature Extraction API and store the results
on config.Leonardo.VectorDir to be used later for Image Comparision
*/
console.log ('Updating Item Image Vectors Database')
var zipFile = uuid.v4()+'.zip';
// create a file to stream archive data to.
var output = fs.createWriteStream(config.SmartShop.imgDir + zipFile);
var archive = archiver('zip', {zlib: { level: 9 }}); // Sets the compression level.
// listen for all archive data to be written
output.on('close', function() {
extractVectors(config.SmartShop.imgDir + zipFile, function (vectors){
//Creates a New Zip File with the vectors of each image
vectors = JSON.parse(vectors);
if (vectors.feature_vector_list.length <= 0){
console.error('Could not retrieve vectors from Leonardo');
console.error(vectors);
return;
}
//zipFile = config.Leonardo.VectorZip;
output = fs.createWriteStream(config.SmartShop.imgDir + zipFile);
for(var i = 0; i < vectors.feature_vector_list.length; i++ ){
//Change file extension
var fileName = vectors.feature_vector_list[i].name
fileName = fileName.substr(0, fileName.indexOf('.'))+'.txt'
var newTxt = fs.createWriteStream(path.join(config.Leonardo.VectorDir,fileName));
var content = JSON.stringify(vectors.feature_vector_list[i].feature_vector);
newTxt.write(content);
newTxt.end()
console.log('Creating file '+ fileName);
}
});
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
fs.readdirSync(__dirname).forEach(file => {
// append img files from stream
if(file.indexOf('.png') !== -1 ||file.indexOf('.jpg') !== -1 || file.indexOf('.jpeg') !== -1){
var file1 = __dirname + '/'+file;
archive.append(fs.createReadStream(file1), { name: file });
console.log(file);
}
})
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
}
function extractVectors(file, callback){
// More info on
// https://help.sap.com/viewer/product/SAP_LEONARDO_MACHINE_LEARNING_FOUNDATION/1.0/en-US
var options = {
url: 'https://sandbox.api.sap.com/ml/featureextraction/inference_sync',
headers: {
'APIKey': config.Leonardo.Credentials.APIKey,
'Accept': 'application/json'
},
formData :{
files: fs.createReadStream(file)},
}
request.post(options, function (err, res, body) {
if (err) {
return console.error('extractVectors failed:', err);
throw err;
}
else{
return callback(body);
}
});
}
function GetSimilarItems(req, callback){
/* this function uploads a image file to the config upload folder,
* then it creates a copy of the Vectors zip (created by UpdateVectorsBase())
* adds the uploaded image to that copy so it can be compared by SAP Leonardo in
* order to find the top X similar items */
//Upload File to Server
uploadFile(req, function(file, err){
if (!err){
//Extract Vector of Image
extractVectors(file,function(vector,err){
if (!err){
// Compare this image with the ones stored in the server
getSimilatiryScoring(vector, function(base, similars,err){
if (!err){
var resp = similars;
for (var i = 0; i < resp.similarityScoring.length; i++){
if (resp.similarityScoring[i].id == base){
resp.similarityScoring = resp.similarityScoring[i].similarVectors
for (var j =0; j < resp.similarityScoring.length; j++){
var fileName = resp.similarityScoring[j].id
fileName = fileName.substr(0, fileName.indexOf('.'))+'.jpg'
resp.similarityScoring[j].id = fileName
}
callback(resp);
}
}
//callback(resp);
}
})
}
})
}
})
}
function uploadFile(req,callback){
// create an incoming form object
var form = new formidable.IncomingForm();
// specify that we want to allow the user to upload multiple files in a single request
form.multiples = false;
// store all uploads in the /uploads directory
form.uploadDir = config.SmartShop.uploadDir;
// File uploaded successfuly.
form.on('file', function(field, file) {
fs.rename(file.path, file.path+'.jpg');
//Callback with the route to the file in the server
callback(file.path+'.jpg');
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured uploaiding the file: \n' + err);
callback(null, err);
});
form.on('end', function() {
});
// parse the incoming request containing the form data
form.parse(req, function(err, fields, files){
console.log(files)
});
}
function getSimilatiryScoring(vectors,callback){
vectors = JSON.parse(vectors);
// Create e zip file of vectors to be used by the Similarity scoring service
var zipFile = uuid.v4()+'.zip';
// create a file to stream archive data to the zip
var output = fs.createWriteStream(config.SmartShop.imgDir + zipFile);
var archive = archiver('zip', {zlib: { level: 9 }}); // Sets the compression level.
// listen for all archive data to be written
output.on('close', function() {
var options = {
url: 'https://sandbox.api.sap.com/ml/similarityscoring/inference_sync',
headers: {
'APIKey': config.Leonardo.Credentials.APIKey,
'Accept': 'application/json',
},
formData :{
files: fs.createReadStream(config.SmartShop.imgDir + zipFile),
options: "{\"numSimilarVectors\":3}"
}
}
request.post(options, function (err, res, body) {
if (err) {
return console.error('Similarity Scoring failed:', err);
throw err;
}
else{
return callback(fileName, JSON.parse(body));
}
});
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
var buff = Buffer.from(JSON.stringify(vectors.feature_vector_list[0].feature_vector), config.SmartShop.encoding);
var fileName = vectors.feature_vector_list[0].name
fileName = fileName.substr(0, fileName.indexOf('.'))+'.txt'
archive.append(buff,{ name: fileName});
fs.readdirSync(config.Leonardo.VectorDir).forEach(file => {
// append txt vector files from stream to the zip
if(file.indexOf('.txt') !== -1){
archive.append(fs.createReadStream(path.join(config.Leonardo.VectorDir,file)), { name: file });
}
})
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
}