-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
112 lines (99 loc) · 3.09 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html>
<head>
<title>mnist-cnn</title>
<script>
let data = Array(28 * 28).fill(0)
/** result bars
* @type{HTMLDivElement[]}*/
let bars = []
let hold = false
document.addEventListener("mousedown", (ev) => { ev.preventDefault(); hold = true })
document.addEventListener("mouseup", () => { hold = false })
function draw(pixel, x, y) {
if (!hold) return
data[y * 28 + x] = 1
pixel.dataset.on = true
}
function init() {
let canvas = document.querySelector('#main-canvas')
for (let y = 0; y < 28; ++y) for (let x = 0; x < 28; ++x) {
data[y * 28 + x] = 0
let pixel = document.createElement('div')
pixel.onmouseenter = () => draw(pixel, x, y)
canvas.appendChild(pixel)
}
let results = document.querySelector('#results')
for (let x = 0; x < 10; ++x) {
let bar = document.createElement('div')
bar.innerText = x
bars[x] = bar
results.appendChild(bar)
}
}
async function predict() {
let res = await fetch('./predict', {
body: JSON.stringify(data),
headers: { 'content-type': 'application/json' },
method: 'POST'
})
let pred = await res.json()
pred = pred.pred
console.log(pred)
for (let i = 0; i < 10; ++i) {
bars[i].style.height = 100-Math.floor(pred[i] * 100) + '%';
}
}
function cleardraw() {
data.fill(0)
for (let pixel of document.querySelectorAll('[data-on]'))
delete pixel.dataset.on
}
</script>
<style>
* {
box-sizing: border-box
}
#main-canvas {
width: 280px;
height: 280px;
border: 1px solid black;
}
.mnist-grid {
display: grid;
grid-template-columns: repeat(28,1fr);
grid-template-rows: repeat(28,1fr)
}
.mnist-grid > div {
width: 100%;
height: 100%;
}
[data-on] {
background-color: black;
}
#results {
display: grid;
height: 50px;
width: 280px;
grid-template-columns: repeat(10,1fr);
grid-column-gap: 1px;
border: 1px solid black;
background-color: black;
}
#results > div {
background-color: white;
vertical-align: bottom;
}
</style>
</head>
<body onload="init()">
<div class="mnist-grid" id="main-canvas"></div>
<button onclick="predict()" style="margin:10px 10px">predict</button>
<button onclick="cleardraw()" style="margin:10px 10px">clear</button>
<div id="results"></div>
<footer>
<hr>
contact: [email protected]
</footer>
</body>
</html>