-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
45 lines (37 loc) · 1.47 KB
/
process.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
var elements = document.getElementsByTagName("img")
for (var i = 0; i < elements.length; i++) {
processImage(elements[i].src, elements[i]);
}
function processImage(source, element) {
var subscriptionKey = "20613855066847539021532e06e9ce11";
var uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze";
var params = {
"visualFeatures": "Categories,Description,Color",
"details": "",
"language": "en",
};
var sourceImageUrl = source;
$.ajax({
url: uriBase + "?" + $.param(params),
beforeSend: function(xhrObj){
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
},
type: "POST",
data: '{"url": ' + '"' + sourceImageUrl + '"}',
})
.done(function(data) {
var caption = data.description.captions[0].text;
if (element.alt != "") {
element.alt ="Original caption: " + element.alt + "; Generated caption: " + caption;
} else {
element.alt = "Generated caption: " + caption;
}
})
.fail(function(jqXHR, textStatus, errorThrown) {
// Display error message.
var errorString = (errorThrown === "") ? "Error. " : errorThrown + " (" + jqXHR.status + "): ";
errorString += (jqXHR.responseText === "") ? "" : jQuery.parseJSON(jqXHR.responseText).message;
console.log(errorString);
});
};