-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClarifaiAPI.js
46 lines (41 loc) · 1.16 KB
/
ClarifaiAPI.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
const Clarifai = require('clarifai');
const key = require('./config/config.js');
const app = new Clarifai.App({
apiKey: key
});
var predictBase64 = (b) => {
return app.models.predict(Clarifai.GENERAL_MODEL, {base64: b})
.then(response => {
var concepts = response['outputs'][0]['data']['concepts'];
// console.log(concepts);
return concepts;
})
.catch(err => console.error(err))
};
var processData = (data) => {
// console.log('data', data);
return data.reduce((acc, cur) => {
if (cur.value > .9) acc.push(cur);
return acc;
}, []);
};
var getBase64 = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
let encoded = reader.result.replace(/^data:(.*;base64,)?/, '');
if ((encoded.length % 4) > 0) {
encoded += '='.repeat(4 - (encoded.length % 4));
}
resolve(encoded);
};
reader.onerror = error => reject(error);
})
.then(str => predictBase64(str))
.then(data => processData(data))
.catch(err => console.error(err))
};
module.exports = {
getBase64: getBase64
};