-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurfaceDetection.js
47 lines (42 loc) · 1.66 KB
/
surfaceDetection.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
function readUrlInput(e) {
const element = document.getElementById('input-url');
imageUrl = element.value;
const imageContainerElem = document.getElementById("image-container");
imageElem = document.createElement("IMG");
imageElem.src = imageUrl;
imageElem.alt = "Unknown Image";
imageContainerElem.removeChild(imageContainerElem.childNodes[0]);
if (imageContainerElem.childNodes.length > 0) {
imageContainerElem.removeChild(imageContainerElem.childNodes[0]);
imageContainerElem.appendChild(imageElem);
}
else {
imageContainerElem.appendChild(imageElem);
}
watsonServiceUrl = "http://think-fyre-covid-hub-watson-service.us-east.mybluemix.net/visual/recognizer"
payload = {"image_url": imageUrl};
postData(watsonServiceUrl, payload)
.then(response => {
response.json().then(data => {
dataJSON = data;
document.getElementById('surface-name').innerHTML = capitalizeFirstLetter(dataJSON.surface_name);
document.getElementById('life-time').innerHTML = "Duration: " + dataJSON.life_time;
document.getElementById('action-details').innerHTML = "Take Action: " + dataJSON.advice;
});
});
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
return response;
}
document.getElementById('analyze-button')
.addEventListener('click', readUrlInput, false);