-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (38 loc) · 1.61 KB
/
script.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
const fs = require('fs');
const request = require('request');
/**
* Sends an image to customvision.ai and returns analysis result
* @param {string} filepath - the path to the image to send
* @param {object} config - config object e.g. {"predictionEndpoint": "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Prediction/11111111-1111-1111-1111-111111111111/image", "predictionKey": "11110000111100001111000011110000"}
* @param {function} successCallback - method to be called in case of success (gets data as single parameter)
* @param {function} [errorCallback] - method to be called in case of error (gets error data as first and data as second parameter)
*/
function sendImage(filepath, config, successCallback, errorCallback = null) {
let formData = {
file: fs.createReadStream(filepath),
};
let options = {
url: config.predictionEndpoint,
headers: {
'Prediction-Key': config.predictionKey,
'Content-Type': 'multipart/form-data'
},
formData: formData
};
request.post(options, (err, httpResponse, body) => {
console.log();
if (err && errorCallback) {
errorCallback(err, body);
}
else if (httpResponse.statusCode != 200 && errorCallback) {
errorCallback(null, JSON.parse(body), httpResponse.statusCode)
}
else if (err || httpResponse.statusCode != 200) {
successCallback(null);
}
else {
successCallback(JSON.parse(body));
}
});
}
module.exports = { sendImage: sendImage };