Repository navigation
Expand file tree
/
Copy pathmicrosoft.js
More file actions
95 lines (86 loc) · 2.91 KB
/
Copy pathmicrosoft.js
File metadata and controls
95 lines (86 loc) · 2.91 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var request = require("request");
let face = imageUrl => {
console.log(`Checking for faces in ${imageUrl}`);
return new Promise((resolve, reject) => {
if (!process.env.microsoft_key_face) {
reject("No Microsoft Key detected");
} else if (!imageUrl) {
reject("No image specified");
}
var options = {
url:
"https://southcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise",
method: "POST",
body: '{"url": "' + imageUrl + '"}',
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": process.env.microsoft_key_face,
},
timeout: 2000,
};
console.log("Making request to Microsoft");
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
var results = JSON.parse(body); // response from Emotion API
console.log(`${results.length} face(s) detected`);
if (results.length > 0) {
resolve(results);
} else {
reject("No faces found");
}
} else {
// there was an error
reject(`Error: ${error}`);
} // end processing response
}); // end request
}); // end Promise
}; // end microsoftEmotion
exports.emotion = imageUrl => {
return new Promise((resolve, reject) => {
face(imageUrl)
.then(result => {
let data = result[0].faceAttributes.emotion;
resolve(data);
})
.catch(error => {
reject(error);
});
});
};
exports.prediction = imageUrl => {
return new Promise((resolve, reject) => {
var options = {
url:
"https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/7c9d932a-0956-434b-b939-842abe5797e9/url?iterationId=d063039e-6f5b-452c-a306-95e8ba22eb61",
method: "POST",
body: '{"url": "' + imageUrl + '"}',
headers: {
"Content-Type": "application/json",
"Prediction-Key": "24a36be869414e18b39596255d276a98"
},
timeout: 2000,
};
console.log("Making request to Microsoft");
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
var results = JSON.parse(body); // response from Prediction API
if (results.predictions.length > 0) {
resolve(formatPredictions(results.predictions));
} else {
reject("Error: no predictions available");
}
} else {
// there was an error
reject(`Error: ${error}`);
} // end processing response
}); // end request
});
}
const formatPredictions = (predictions) => {
let formatted = {};
predictions.forEach((prediction) => {
formatted[prediction.tagName] = prediction.probability
})
console.log(formatted);
return formatted;
}