-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimageanalysisfunction.js
54 lines (48 loc) · 1.77 KB
/
imageanalysisfunction.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
var request = require('request');
module.exports = function (context, req) {
context.log('Node.js HTTP trigger function processed a request. RequestUri=%s', req.originalUrl);
// Make a call out to Cognitive Services
if (req.body.url)
{
var queryString = 'visualFeatures=Categories,Tags,Description,Faces,ImageType,Color,Adult&details=Celebrities&language=en';
request.post({
url: 'https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?'+ queryString,
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": "[Cogntive Services Key]"
},
json: { "url": req.body.url }
},
function (err, res, body) {
context.log("Response from Cog API (err, res, body)");
context.log(JSON.stringify(err, null, " "));
context.log(JSON.stringify(res, null, " "));
context.log(JSON.stringify(body, null, " "));
// Check to see if we succeeded.
if (err || res.statusCode != 200) {
context.log(err);
context.res = {
status: 500,
body: err
}
context.done();
return
}
context.res = {
// status: 200, /* Defaults to 200 */
// Send back the score we got from the Cog API
body: { caption: body.description.captions[0].text, raw: body }
};
context.done();
});
}
else {
// Bad request - Missing property on body
context.res = {
status: 400,
body: "Expected 'text' property on query string"
};
context.done();
}
};