Spaces:
Build error
Build error
Create static/js/script.js
Browse files- static/js/script.js +71 -0
static/js/script.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// static/js/script.js
|
| 2 |
+
|
| 3 |
+
let hMin = 0, hMax = 179, sMin = 0, sMax = 255, vMin = 0, vMax = 255;
|
| 4 |
+
let currentImage = null;
|
| 5 |
+
|
| 6 |
+
function updateImage(src) {
|
| 7 |
+
const img = document.getElementById("image");
|
| 8 |
+
img.src = src + "?t=" + new Date().getTime(); // Cache-busting
|
| 9 |
+
currentImage = src;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
function sendPixelClick(x, y) {
|
| 13 |
+
fetch("/click", {
|
| 14 |
+
method: "POST",
|
| 15 |
+
headers: { "Content-Type": "application/json" },
|
| 16 |
+
body: JSON.stringify({ x: x, y: y })
|
| 17 |
+
})
|
| 18 |
+
.then(res => res.json())
|
| 19 |
+
.then(data => {
|
| 20 |
+
document.getElementById("pixel-info").innerText = data.info;
|
| 21 |
+
hMin = data.hMin; hMax = data.hMax;
|
| 22 |
+
sMin = data.sMin; sMax = data.sMax;
|
| 23 |
+
vMin = data.vMin; vMax = data.vMax;
|
| 24 |
+
updateSegmentation();
|
| 25 |
+
});
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
function updateSegmentation() {
|
| 29 |
+
fetch("/update_mask", {
|
| 30 |
+
method: "POST",
|
| 31 |
+
headers: { "Content-Type": "application/json" },
|
| 32 |
+
body: JSON.stringify({ hMin, hMax, sMin, sMax, vMin, vMax })
|
| 33 |
+
})
|
| 34 |
+
.then(res => res.json())
|
| 35 |
+
.then(data => {
|
| 36 |
+
updateImage(data.image_path);
|
| 37 |
+
});
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function adjustValue(type, delta) {
|
| 41 |
+
switch(type) {
|
| 42 |
+
case "hMin": hMin = Math.max(0, Math.min(179, hMin + delta)); break;
|
| 43 |
+
case "hMax": hMax = Math.max(0, Math.min(179, hMax + delta)); break;
|
| 44 |
+
case "sMin": sMin = Math.max(0, Math.min(255, sMin + delta)); break;
|
| 45 |
+
case "sMax": sMax = Math.max(0, Math.min(255, sMax + delta)); break;
|
| 46 |
+
case "vMin": vMin = Math.max(0, Math.min(255, vMin + delta)); break;
|
| 47 |
+
case "vMax": vMax = Math.max(0, Math.min(255, vMax + delta)); break;
|
| 48 |
+
}
|
| 49 |
+
updateSegmentation();
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
function setupCanvas() {
|
| 53 |
+
const img = document.getElementById("image");
|
| 54 |
+
const canvas = document.getElementById("canvas");
|
| 55 |
+
const ctx = canvas.getContext("2d");
|
| 56 |
+
|
| 57 |
+
img.onload = function() {
|
| 58 |
+
canvas.width = img.width;
|
| 59 |
+
canvas.height = img.height;
|
| 60 |
+
ctx.drawImage(img, 0, 0);
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
canvas.addEventListener("click", function(e) {
|
| 64 |
+
const rect = canvas.getBoundingClientRect();
|
| 65 |
+
const x = Math.floor(e.clientX - rect.left);
|
| 66 |
+
const y = Math.floor(e.clientY - rect.top);
|
| 67 |
+
sendPixelClick(x, y);
|
| 68 |
+
});
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
document.addEventListener("DOMContentLoaded", setupCanvas);
|