|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + |
| 4 | +<head> |
| 5 | + <title>ULabel - set_annotations Demo</title> |
| 6 | + |
| 7 | + <!-- ULabel Library --> |
| 8 | + <script src="/ulabel.js"></script> |
| 9 | + |
| 10 | + <!-- JQuery Library --> |
| 11 | + <script src="https://code.jquery.com/jquery-3.5.1.min.js" |
| 12 | + integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> |
| 13 | + |
| 14 | + <style> |
| 15 | + body { margin: 0; font-family: system-ui, sans-serif; } |
| 16 | + #container { |
| 17 | + width: 100%; |
| 18 | + height: calc(100vh - 84px); |
| 19 | + position: absolute; |
| 20 | + top: 84px; |
| 21 | + left: 0; |
| 22 | + } |
| 23 | + #controls { |
| 24 | + position: fixed; |
| 25 | + top: 0; |
| 26 | + left: 0; |
| 27 | + right: 0; |
| 28 | + height: 84px; |
| 29 | + box-sizing: border-box; |
| 30 | + padding: 8px 12px; |
| 31 | + background: #1e293b; |
| 32 | + color: #f8fafc; |
| 33 | + display: flex; |
| 34 | + flex-wrap: wrap; |
| 35 | + gap: 8px; |
| 36 | + align-items: center; |
| 37 | + z-index: 1000; |
| 38 | + border-bottom: 1px solid #334155; |
| 39 | + } |
| 40 | + #controls button { |
| 41 | + background: #334155; |
| 42 | + color: #f8fafc; |
| 43 | + border: 1px solid #475569; |
| 44 | + padding: 6px 12px; |
| 45 | + font-size: 13px; |
| 46 | + border-radius: 4px; |
| 47 | + cursor: pointer; |
| 48 | + } |
| 49 | + #controls button:hover { background: #475569; } |
| 50 | + #controls button:active { background: #64748b; } |
| 51 | + #status { |
| 52 | + margin-left: auto; |
| 53 | + font-size: 12px; |
| 54 | + color: #cbd5e1; |
| 55 | + } |
| 56 | + .checklist { |
| 57 | + font-size: 11px; |
| 58 | + color: #94a3b8; |
| 59 | + width: 100%; |
| 60 | + margin-top: 4px; |
| 61 | + } |
| 62 | + .checklist code { background: #0f172a; padding: 1px 4px; border-radius: 3px; } |
| 63 | + </style> |
| 64 | +</head> |
| 65 | + |
| 66 | +<body> |
| 67 | + <div id="controls"> |
| 68 | + <span style="font-weight: 600;">set_annotations:</span> |
| 69 | + <button id="btn-empty">Empty</button> |
| 70 | + <button id="btn-small">Small (3 bboxes)</button> |
| 71 | + <button id="btn-medium">Medium (50 bboxes + polygons)</button> |
| 72 | + <button id="btn-large">Large (500 bboxes)</button> |
| 73 | + <button id="btn-bitmask-rle">Bitmasks via RLE</button> |
| 74 | + <button id="btn-bitmask-raw">Bitmasks via raw Uint8Array</button> |
| 75 | + <button id="btn-restore">Restore original</button> |
| 76 | + <span id="status">idle</span> |
| 77 | + <div class="checklist"> |
| 78 | + After each swap, verify: hover an annotation shows the id-dialog highlight; |
| 79 | + press <code>g</code> to toggle brush — the brush circle must appear at the cursor. |
| 80 | + Open devtools and inspect <code>window.ulabel</code>. |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <div id="container"></div> |
| 84 | + |
| 85 | + <script> |
| 86 | + /* global $, ULabel */ |
| 87 | + $(window).on("load", function () { |
| 88 | + |
| 89 | + function on_submit(annotations) { |
| 90 | + const element = document.createElement("a"); |
| 91 | + element.setAttribute( |
| 92 | + "href", |
| 93 | + "data:text/plain;charset=utf-8," + |
| 94 | + encodeURIComponent(JSON.stringify(annotations, null, 2)) |
| 95 | + ); |
| 96 | + element.setAttribute("download", "annotations.json"); |
| 97 | + element.style.display = "none"; |
| 98 | + document.body.appendChild(element); |
| 99 | + element.click(); |
| 100 | + document.body.removeChild(element); |
| 101 | + } |
| 102 | + |
| 103 | + // Original preset — restored via the "Restore original" button. |
| 104 | + const original_annotations = [ |
| 105 | + { |
| 106 | + spatial_type: "bbox", |
| 107 | + spatial_payload: [[100, 100], [300, 250]], |
| 108 | + classification_payloads: [{ class_id: 10, confidence: 1.0 }], |
| 109 | + }, |
| 110 | + { |
| 111 | + spatial_type: "bbox", |
| 112 | + spatial_payload: [[400, 150], [600, 320]], |
| 113 | + classification_payloads: [{ class_id: 11, confidence: 1.0 }], |
| 114 | + }, |
| 115 | + { |
| 116 | + spatial_type: "polygon", |
| 117 | + spatial_payload: [[[700, 400], [900, 400], [900, 600], [700, 600], [700, 400]]], |
| 118 | + classification_payloads: [{ class_id: 12, confidence: 1.0 }], |
| 119 | + }, |
| 120 | + ]; |
| 121 | + |
| 122 | + const subtasks = { |
| 123 | + segmentation: { |
| 124 | + display_name: "Segmentation Demo", |
| 125 | + classes: [ |
| 126 | + { name: "Sedan", color: "blue", id: 10, keybind: "1" }, |
| 127 | + { name: "SUV", color: "green", id: 11, keybind: "2" }, |
| 128 | + { name: "Truck", color: "orange", id: 12, keybind: "3" }, |
| 129 | + ], |
| 130 | + allowed_modes: ["bbox", "polygon", "point", "bitmask"], |
| 131 | + resume_from: JSON.parse(JSON.stringify(original_annotations)), |
| 132 | + task_meta: null, |
| 133 | + annotation_meta: null, |
| 134 | + }, |
| 135 | + }; |
| 136 | + |
| 137 | + const ulabel = new ULabel({ |
| 138 | + container_id: "container", |
| 139 | + image_data: "https://ulabel.s3.us-east-2.amazonaws.com/cs-demo-0.png", |
| 140 | + username: "DemoUser", |
| 141 | + submit_buttons: [{ name: "Submit", hook: on_submit }], |
| 142 | + subtasks: subtasks, |
| 143 | + initial_line_size: 2, |
| 144 | + }); |
| 145 | + |
| 146 | + window.ulabel = ulabel; |
| 147 | + |
| 148 | + const status_el = document.getElementById("status"); |
| 149 | + function set_status(text) { |
| 150 | + status_el.textContent = text; |
| 151 | + } |
| 152 | + |
| 153 | + // Helpers for building annotation shapes. |
| 154 | + function make_bbox(class_id, x0, y0, x1, y1) { |
| 155 | + return { |
| 156 | + spatial_type: "bbox", |
| 157 | + spatial_payload: [[x0, y0], [x1, y1]], |
| 158 | + classification_payloads: [{ class_id: class_id, confidence: 1.0 }], |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + function make_polygon(class_id, cx, cy, radius, n_points) { |
| 163 | + const pts = []; |
| 164 | + for (let i = 0; i < n_points; i++) { |
| 165 | + const t = (i / n_points) * 2 * Math.PI; |
| 166 | + pts.push([cx + Math.cos(t) * radius, cy + Math.sin(t) * radius]); |
| 167 | + } |
| 168 | + pts.push(pts[0].slice()); |
| 169 | + return { |
| 170 | + spatial_type: "polygon", |
| 171 | + spatial_payload: [pts], |
| 172 | + classification_payloads: [{ class_id: class_id, confidence: 1.0 }], |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + // Fill a raw Uint8Array mask with a circle at (cx, cy) of radius r. |
| 177 | + function make_raw_bitmask_annotation(class_id, cx, cy, r) { |
| 178 | + const width = ulabel.config.image_width; |
| 179 | + const height = ulabel.config.image_height; |
| 180 | + const data = new Uint8Array(width * height); |
| 181 | + const r_sq = r * r; |
| 182 | + const min_x = Math.max(0, cx - r); |
| 183 | + const max_x = Math.min(width - 1, cx + r); |
| 184 | + const min_y = Math.max(0, cy - r); |
| 185 | + const max_y = Math.min(height - 1, cy + r); |
| 186 | + for (let y = min_y; y <= max_y; y++) { |
| 187 | + const dy = y - cy; |
| 188 | + const row = y * width; |
| 189 | + for (let x = min_x; x <= max_x; x++) { |
| 190 | + const dx = x - cx; |
| 191 | + if (dx * dx + dy * dy <= r_sq) data[row + x] = 1; |
| 192 | + } |
| 193 | + } |
| 194 | + return { |
| 195 | + spatial_type: "bitmask", |
| 196 | + // The new raw shape; ULabel copies the buffer defensively. |
| 197 | + spatial_payload: { data: data, size: [height, width] }, |
| 198 | + classification_payloads: [{ class_id: class_id, confidence: 1.0 }], |
| 199 | + }; |
| 200 | + } |
| 201 | + |
| 202 | + // Build a bitmask annotation via the pre-existing RLE path. |
| 203 | + function make_rle_bitmask_annotation(class_id, cx, cy, r) { |
| 204 | + const raw = make_raw_bitmask_annotation(class_id, cx, cy, r); |
| 205 | + // Encode row-major -> column-major RLE. Cheapest way for demo purposes: |
| 206 | + // let ULabel do the decode via the raw path, then read the mask back and |
| 207 | + // hand-encode. Instead we just reuse the raw path for construction and |
| 208 | + // wrap it in the RLE shape via a scratch ULabelMask. |
| 209 | + // Simpler for the demo: keep only the counts derivation below. |
| 210 | + const width = ulabel.config.image_width; |
| 211 | + const height = ulabel.config.image_height; |
| 212 | + const data = raw.spatial_payload.data; |
| 213 | + const counts = []; |
| 214 | + let current_is_fg = false; |
| 215 | + let run = 0; |
| 216 | + for (let x = 0; x < width; x++) { |
| 217 | + for (let y = 0; y < height; y++) { |
| 218 | + const is_fg = data[y * width + x] !== 0; |
| 219 | + if (is_fg === current_is_fg) run++; |
| 220 | + else { counts.push(run); current_is_fg = is_fg; run = 1; } |
| 221 | + } |
| 222 | + } |
| 223 | + counts.push(run); |
| 224 | + return { |
| 225 | + spatial_type: "bitmask", |
| 226 | + spatial_payload: { counts: counts, size: [height, width] }, |
| 227 | + classification_payloads: [{ class_id: class_id, confidence: 1.0 }], |
| 228 | + }; |
| 229 | + } |
| 230 | + |
| 231 | + async function swap(label, annotations) { |
| 232 | + set_status(`swapping to ${label} (${annotations.length} annotations)...`); |
| 233 | + const t0 = performance.now(); |
| 234 | + await ulabel.set_annotations(annotations, "segmentation"); |
| 235 | + const dt = (performance.now() - t0).toFixed(1); |
| 236 | + set_status(`${label}: ${annotations.length} annotations, ${dt} ms`); |
| 237 | + } |
| 238 | + |
| 239 | + ulabel.init(function () { |
| 240 | + set_status(`ready (${original_annotations.length} original annotations)`); |
| 241 | + |
| 242 | + document.getElementById("btn-empty").addEventListener("click", () => { |
| 243 | + swap("empty", []); |
| 244 | + }); |
| 245 | + document.getElementById("btn-small").addEventListener("click", () => { |
| 246 | + swap("small", [ |
| 247 | + make_bbox(10, 200, 200, 320, 320), |
| 248 | + make_bbox(11, 500, 200, 620, 320), |
| 249 | + make_bbox(12, 800, 200, 920, 320), |
| 250 | + ]); |
| 251 | + }); |
| 252 | + document.getElementById("btn-medium").addEventListener("click", () => { |
| 253 | + const anns = []; |
| 254 | + for (let i = 0; i < 40; i++) { |
| 255 | + const x = 50 + (i % 8) * 120; |
| 256 | + const y = 50 + Math.floor(i / 8) * 120; |
| 257 | + anns.push(make_bbox(10 + (i % 3), x, y, x + 90, y + 90)); |
| 258 | + } |
| 259 | + for (let i = 0; i < 10; i++) { |
| 260 | + anns.push(make_polygon(10 + (i % 3), 200 + i * 80, 700, 40, 8)); |
| 261 | + } |
| 262 | + swap("medium", anns); |
| 263 | + }); |
| 264 | + document.getElementById("btn-large").addEventListener("click", () => { |
| 265 | + const anns = []; |
| 266 | + for (let i = 0; i < 500; i++) { |
| 267 | + const x = 10 + (i % 40) * 40; |
| 268 | + const y = 10 + Math.floor(i / 40) * 40; |
| 269 | + anns.push(make_bbox(10 + (i % 3), x, y, x + 30, y + 30)); |
| 270 | + } |
| 271 | + swap("large", anns); |
| 272 | + }); |
| 273 | + document.getElementById("btn-bitmask-rle").addEventListener("click", () => { |
| 274 | + swap("bitmask-rle", [ |
| 275 | + make_rle_bitmask_annotation(10, 300, 300, 80), |
| 276 | + make_rle_bitmask_annotation(11, 700, 400, 120), |
| 277 | + ]); |
| 278 | + }); |
| 279 | + document.getElementById("btn-bitmask-raw").addEventListener("click", () => { |
| 280 | + swap("bitmask-raw", [ |
| 281 | + make_raw_bitmask_annotation(10, 400, 300, 100), |
| 282 | + make_raw_bitmask_annotation(11, 800, 500, 140), |
| 283 | + make_raw_bitmask_annotation(12, 600, 700, 80), |
| 284 | + ]); |
| 285 | + }); |
| 286 | + document.getElementById("btn-restore").addEventListener("click", () => { |
| 287 | + swap("original", JSON.parse(JSON.stringify(original_annotations))); |
| 288 | + }); |
| 289 | + }); |
| 290 | + }); |
| 291 | + </script> |
| 292 | +</body> |
| 293 | + |
| 294 | +</html> |
0 commit comments