-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmit.html
73 lines (64 loc) · 2.57 KB
/
submit.html
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
<html>
<head>
<title>PyClassify</title>
</head>
<body>
<h1>PyClassify</h1>
<p>This is a python server for POSTing images and receiving classifications and weights</p>
<form action="/classify" method="post" enctype="multipart/form-data" id="image_form">
<input type="file" name="image" accept="image/jpeg">
<br/><br/>
<input type="submit" value="Upload">
</form>
<br />
result:
<div id="result"></div>
<script>
const convertBase64 = (file) => {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.readAsDataURL(file);
fileReader.onload = () => {
resolve(fileReader.result);
};
fileReader.onerror = (error) => {
reject(error);
};
});
};
const base64awaited = async (file) => {
let b64img = await convertBase64(file)
return b64img
}
// hang an event handler on the form submit event to intercept it and
// post the form data as JSON
document.getElementById('image_form').addEventListener('submit', function (e) {
e.preventDefault();
var form = e.target;
var formData = new FormData(form);
var reader = new FileReader();
reader.onload = function (e) {
var image = e.target.result;
console.log({ image });
var json = JSON.stringify({
'image': image
});
console.log({ json });
var xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');
xhr.onload = function () {
var res = JSON.parse(xhr.responseText);
console.log(res);
var result = document.getElementById('result');
result.innerHTML = '';
// for each "classifications" element, add a new paragraph to the result div
for (const[key,value] of Object.entries(res.classifications)) {
result.innerHTML += `<p style="margin:0.3rem; font-size: ${value*1.1+1}rem">${key}: ${(100*value).toFixed(2)}%</p>`;
}
};
xhr.send(json);
};
reader.readAsDataURL(formData.get('image'));
});
</script>