-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
80 lines (51 loc) · 2.43 KB
/
script.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
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
window.addEventListener("load",function(){
document.getElementById('capture').onchange = function (evt) {
var tgt = evt.target || window.event.srcElement,
files = tgt.files;
// FileReader support
if (FileReader && files && files.length) {
var fr = new FileReader();
fr.onload = function () {
document.getElementById('PredictedPicture').src = fr.result;
}
fr.readAsDataURL(files[0]);
}
// Not supported
else {
// fallback -- perhaps submit the input to an iframe and temporarily store
// them on the server until the user's session ends.
}
}
button.addEventListener("click", function(){
const file = document.getElementById('capture').files[0];
console.log(file);
//HTTP Post Request
var URL = "https://centralindia.api.cognitive.microsoft.com/customvision/v3.0/Prediction/1b33e038-ed35-489d-8e9b-bc651e05dbf5/classify/iterations/Iteration1/image";
var xhr = new XMLHttpRequest();
xhr.open('POST', URL, true);
xhr.setRequestHeader('Prediction-Key','9f786ebaf15c4dfea557b5fb2412195e');
xhr.setRequestHeader('Content-Type','application/octet-stream')
xhr.send(file);
xhr.onreadystatechange = processRequest;
function processRequest(e){
if(xhr.readyState == 4 && xhr.status == 200){
//alert(xhr.responseText);
console.log(typeof(xhr.responseText));
var json = JSON.parse(xhr.responseText);
console.log(json);
console.log(json.predictions[0]['probability']);
console.log(typeof(json));
var table = document.getElementById("myTable");
for(var i = json.predictions.length -1 ; i >= 0 ; i--){
var row = table.insertRow(1);
// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element:
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
// Add some text to the new cells:
cell1.innerHTML = json.predictions[i]['tagName'];
cell2.innerHTML = json.predictions[i]['probability'] * 100 + '%';
}
}
}
},false);
},false);