Spaces:
Sleeping
Sleeping
Update index.html
Browse files- index.html +88 -1
index.html
CHANGED
|
@@ -20,7 +20,7 @@
|
|
| 20 |
|
| 21 |
<h2 id="result">Prediction: </h2>
|
| 22 |
|
| 23 |
-
<script>
|
| 24 |
// =====================================================
|
| 25 |
// CANVAS SETUP (desktop + mobile)
|
| 26 |
// =====================================================
|
|
@@ -98,6 +98,93 @@ async function predict() {
|
|
| 98 |
let result = await response.json();
|
| 99 |
document.getElementById("result").innerText = "Prediction: " + result.prediction;
|
| 100 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
</script>
|
| 102 |
|
| 103 |
</body>
|
|
|
|
| 20 |
|
| 21 |
<h2 id="result">Prediction: </h2>
|
| 22 |
|
| 23 |
+
<!-- <script>
|
| 24 |
// =====================================================
|
| 25 |
// CANVAS SETUP (desktop + mobile)
|
| 26 |
// =====================================================
|
|
|
|
| 98 |
let result = await response.json();
|
| 99 |
document.getElementById("result").innerText = "Prediction: " + result.prediction;
|
| 100 |
}
|
| 101 |
+
</script>
|
| 102 |
+
-->
|
| 103 |
+
<script>
|
| 104 |
+
// =====================================================
|
| 105 |
+
// CANVAS SETUP (desktop + mobile)
|
| 106 |
+
// =====================================================
|
| 107 |
+
let canvas = document.getElementById("canvas");
|
| 108 |
+
let ctx = canvas.getContext("2d");
|
| 109 |
+
let drawing = false;
|
| 110 |
+
let brushSize = 12; // <--- NEW
|
| 111 |
+
|
| 112 |
+
// --------------------------
|
| 113 |
+
// Desktop events
|
| 114 |
+
// --------------------------
|
| 115 |
+
canvas.addEventListener("mousedown", () => { drawing = true; });
|
| 116 |
+
canvas.addEventListener("mouseup", () => { drawing = false; });
|
| 117 |
+
canvas.addEventListener("mousemove", draw);
|
| 118 |
+
|
| 119 |
+
// --------------------------
|
| 120 |
+
// Mobile events
|
| 121 |
+
// --------------------------
|
| 122 |
+
canvas.addEventListener("touchstart", (e) => {
|
| 123 |
+
e.preventDefault();
|
| 124 |
+
drawing = true;
|
| 125 |
+
});
|
| 126 |
+
canvas.addEventListener("touchend", () => { drawing = false; });
|
| 127 |
+
canvas.addEventListener("touchmove", drawTouch);
|
| 128 |
+
|
| 129 |
+
// --------------------------
|
| 130 |
+
// Update brush size
|
| 131 |
+
// --------------------------
|
| 132 |
+
function updateBrushSize() {
|
| 133 |
+
brushSize = document.getElementById("brushSize").value;
|
| 134 |
+
document.getElementById("brushValue").innerText = brushSize;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
// --------------------------
|
| 138 |
+
// Draw functions
|
| 139 |
+
// --------------------------
|
| 140 |
+
function draw(e) {
|
| 141 |
+
if (!drawing) return;
|
| 142 |
+
ctx.fillStyle = "black";
|
| 143 |
+
ctx.beginPath();
|
| 144 |
+
ctx.arc(e.offsetX, e.offsetY, brushSize, 0, Math.PI * 2);
|
| 145 |
+
ctx.fill();
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
function drawTouch(e) {
|
| 149 |
+
e.preventDefault();
|
| 150 |
+
if (!drawing) return;
|
| 151 |
+
|
| 152 |
+
let rect = canvas.getBoundingClientRect();
|
| 153 |
+
let touch = e.touches[0];
|
| 154 |
+
let x = touch.clientX - rect.left;
|
| 155 |
+
let y = touch.clientY - rect.top;
|
| 156 |
+
|
| 157 |
+
ctx.fillStyle = "black";
|
| 158 |
+
ctx.beginPath();
|
| 159 |
+
ctx.arc(x, y, brushSize, 0, Math.PI * 2);
|
| 160 |
+
ctx.fill();
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
// =====================================================
|
| 164 |
+
// CANVAS CONTROL FUNCTIONS
|
| 165 |
+
// =====================================================
|
| 166 |
+
function clearCanvas() {
|
| 167 |
+
ctx.fillStyle = "white";
|
| 168 |
+
ctx.fillRect(0, 0, 300, 300);
|
| 169 |
+
document.getElementById("result").innerText = "Prediction: ";
|
| 170 |
+
}
|
| 171 |
+
clearCanvas();
|
| 172 |
+
|
| 173 |
+
// =====================================================
|
| 174 |
+
// PREDICT FUNCTION
|
| 175 |
+
// =====================================================
|
| 176 |
+
async function predict() {
|
| 177 |
+
let dataURL = canvas.toDataURL("image/png");
|
| 178 |
+
|
| 179 |
+
let response = await fetch("/predict", {
|
| 180 |
+
method: "POST",
|
| 181 |
+
headers: { "Content-Type": "application/json" },
|
| 182 |
+
body: JSON.stringify({ image: dataURL })
|
| 183 |
+
});
|
| 184 |
+
|
| 185 |
+
let result = await response.json();
|
| 186 |
+
document.getElementById("result").innerText = "Prediction: " + result.prediction;
|
| 187 |
+
}
|
| 188 |
</script>
|
| 189 |
|
| 190 |
</body>
|