-
Notifications
You must be signed in to change notification settings - Fork 3
/
get-image-analysis.js
60 lines (50 loc) · 1.28 KB
/
get-image-analysis.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
var base64 = require('node-base64-image');
var request = require('request');
var config = require('./config');
var sb = require('standard-bail')();
var apiURL =
'https://vision.googleapis.com/v1/images:annotate?key=' +
config.googleVisionAPIKey;
var encooderOpts = {
string: true
};
// This function fetches and encodes the image, assembles the request JSON,
// then basically does this:
// curl -v -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1/images:annotate?key=<key> --data-binary @vision-request.json
function getImageAnalysis(opts, done) {
var imageURL;
if (opts) {
imageURL = opts.imageURL;
}
base64.base64encoder(imageURL, encooderOpts, sb(analyzeImage, done));
}
function analyzeImage(image, done) {
var requestOpts = {
url: apiURL,
method: 'POST',
json: true,
body: createPostBody(image)
};
request(requestOpts, sb(passBody, done));
}
function passBody(response, body, done) {
done(null, body);
}
function createPostBody(base64encodedImage) {
return {
requests: [
{
image: {
content: base64encodedImage
},
features: [
{
type: 'LABEL_DETECTION',
maxResults: 5
}
]
}
]
};
}
module.exports = getImageAnalysis;