Skip to content

Commit 25e519e

Browse files
Merge pull request #253 from SenteraLLC/hotfix/set-annotations
fix/set-annotations
2 parents 610495d + ce8213f commit 25e519e

8 files changed

Lines changed: 331 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented here.
44

55
## [unreleased]
66

7+
## [0.26.2] - Aug 13th, 2026
8+
- **Fix regression in `set_annotations()` where hover feedback and the brush stopped working after a swap.** The 0.26.1 bulk-teardown path called `$("#canvasses__<subtask>").empty()`, which removed not just the per-annotation canvases but also the subtask's front canvas and the `#dialogs__<subtask>` container (which owns the brush circle and polygon ender). `state.front_context` was left pointing at a detached canvas so hover highlights painted into nothing, and the brush had no parent to attach to. The teardown now removes only `> canvas.annotation_canvas` children, leaving the front/back canvases and dialogs container intact.
9+
- New demo: [`demo/set-annotations.html`](demo/set-annotations.html) with buttons that swap through empty / small / medium / large / RLE-bitmask / raw-Uint8Array-bitmask presets so this regression stays visible.
10+
711
## [0.26.1] - Aug 13th, 2026
812
- **Bitmask annotations can now be imported as raw `Uint8Array` payloads.** In addition to the existing RLE `{ counts, size }` shape, `spatial_payload` may now be `{ data: Uint8Array, size: [height, width] }`. This lets callers that already have the mask as a pixel buffer skip the RLE encode step before handing it to ULabel.
913
- **Loader no longer flashes on fast operations.** `ULabelLoader.add_loader_div()` now appends the overlay hidden and reveals it only after a delay (200 ms by default). Callers can pass an explicit `delay_ms` — including `0` to opt back into immediate-show.

‎demo.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ console.log(`http://localhost:${port}/box-roi.html`);
1414
console.log(`http://localhost:${port}/resume-from.html`);
1515
console.log(`http://localhost:${port}/row-filtering-example.html`);
1616
console.log(`http://localhost:${port}/bitmask-example.html`);
17+
console.log(`http://localhost:${port}/set-annotations.html`);
1718
console.log(`http://localhost:${port}/live_demo.html`);

‎demo/set-annotations.html‎

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
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 &mdash; 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>

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "ulabel",
33
"description": "An image annotation tool.",
4-
"version": "0.26.1",
4+
"version": "0.26.2",
55
"main": "dist/ulabel.min.js",
66
"module": "dist/ulabel.min.js",
77
"types": "dist/index.d.ts",

‎src/index.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7227,7 +7227,10 @@ export class ULabel {
72277227
delete anno["_bitmask_box_hint"];
72287228
}
72297229
}
7230-
$("#canvasses__" + subtask).empty();
7230+
// Only remove per-annotation canvases. The subtask's front/back canvases and the
7231+
// #dialogs__<subtask> container (which owns the brush circle, polygon ender, and
7232+
// id dialogs) live in the same parent and MUST survive.
7233+
$("#canvasses__" + subtask + " > canvas.annotation_canvas").remove();
72317234
this.subtasks[subtask]["state"]["annotation_contexts"] = {};
72327235
}
72337236

‎src/version.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const ULABEL_VERSION = "0.26.1";
1+
export const ULABEL_VERSION = "0.26.2";

‎tests/annotation.test.js‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,23 @@ describe("Annotation Processing", () => {
381381
const canvasses = document.createElement("div");
382382
canvasses.id = "canvasses__test_task";
383383
document.body.appendChild(canvasses);
384-
const fake_canvas = document.createElement("canvas");
385-
fake_canvas.id = "canvas__fake";
386-
canvasses.appendChild(fake_canvas);
384+
// Mirror the init-time DOM: back canvas, front canvas, dialogs container, then
385+
// per-annotation canvases (which are the only children we should be removing).
386+
const back = document.createElement("canvas");
387+
back.id = "back_canvas";
388+
canvasses.appendChild(back);
389+
const front = document.createElement("canvas");
390+
front.id = "front_canvas";
391+
canvasses.appendChild(front);
392+
const dialogs = document.createElement("div");
393+
dialogs.id = "dialogs__test_task";
394+
canvasses.appendChild(dialogs);
395+
const anno_canvas = document.createElement("canvas");
396+
anno_canvas.id = "canvas__anno";
397+
anno_canvas.classList.add("annotation_canvas");
398+
canvasses.appendChild(anno_canvas);
387399
ulabel.subtasks.test_task.state.annotation_contexts = {
388-
canvas__fake: { annotation_ids: [anno_id], context: {} },
400+
canvas__anno: { annotation_ids: [anno_id], context: {} },
389401
};
390402

391403
ulabel._clear_subtask_annotation_canvases("test_task");
@@ -394,8 +406,14 @@ describe("Annotation Processing", () => {
394406
expect(annotation._mask).toBeUndefined();
395407
expect(annotation._mask_render).toBeUndefined();
396408
expect(annotation._bitmask_box_hint).toBeUndefined();
397-
// Canvas DOM subtree wiped.
398-
expect(canvasses.children.length).toBe(0);
409+
// Per-annotation canvas removed.
410+
expect(document.getElementById("canvas__anno")).toBeNull();
411+
// Back/front canvases and the dialogs container MUST survive; otherwise hover
412+
// feedback (front canvas) and brush/polygon-ender attach points (dialogs div)
413+
// become detached-DOM ghosts.
414+
expect(document.getElementById("back_canvas")).not.toBeNull();
415+
expect(document.getElementById("front_canvas")).not.toBeNull();
416+
expect(document.getElementById("dialogs__test_task")).not.toBeNull();
399417
// Annotation-context bookkeeping reset.
400418
expect(ulabel.subtasks.test_task.state.annotation_contexts).toEqual({});
401419
});

0 commit comments

Comments
 (0)