This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (38 loc) · 1.51 KB
/
index.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
// noinspection JSUnresolvedVariable
exports.owHandler = function (params) {
let url;
// search for image url in request
if (params.hasOwnProperty("body") && params.body.hasOwnProperty("url")) {
url = params.body.url;
} else {
throw new Error("Missing argument in image recognition");
}
// execute request and perform image analysis
return new Promise(function (resolve, _) {
detectObjectsAndScenes(url, function (response) {
resolve({
value: response,
body: {
"url": url
}
});
});
});
}
function detectObjectsAndScenes(image, callback) {
const ComputerVisionClient = require('@azure/cognitiveservices-computervision').ComputerVisionClient;
const ApiKeyCredentials = require('@azure/ms-rest-js').ApiKeyCredentials;
const azure = require('./azureconfig');
// prepare and perform request
// noinspection SpellCheckingInspection
const client = new ComputerVisionClient(new ApiKeyCredentials(
{ inHeader: { 'Ocp-Apim-Subscription-Key': azure.key } }), azure.endpoint);
client.analyzeImage(image, { visualFeatures: ['Tags'] }).then((result) => {
// analyze result
let tags = result.tags;
let resultString = tags.map(tag => (`${tag.name}`)).join(', ');
let value = resultString.includes("person");
// response creation and return
return callback(value);
}).catch((err) => {throw new Error(err)});
}