easyannotate 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data_factory/__init__.py +82 -0
- data_factory/__main__.py +6 -0
- data_factory/annotate/__init__.py +15 -0
- data_factory/annotate/attach.py +398 -0
- data_factory/annotate/demo.py +161 -0
- data_factory/annotate/images.py +443 -0
- data_factory/annotate/labels.py +399 -0
- data_factory/annotate/loop.py +359 -0
- data_factory/annotate/projects.py +207 -0
- data_factory/annotate/review.py +471 -0
- data_factory/annotate/schema.sql +171 -0
- data_factory/annotate/shapes.py +488 -0
- data_factory/annotate/store.py +292 -0
- data_factory/annotate/tools/__init__.py +39 -0
- data_factory/annotate/tools/fake.py +170 -0
- data_factory/annotate/tools/install.py +254 -0
- data_factory/annotate/tools/local.py +389 -0
- data_factory/annotate/tools/postprocess.py +175 -0
- data_factory/annotate/tools/protocol.py +238 -0
- data_factory/annotate/tools/registry.py +196 -0
- data_factory/annotate/tools/results.py +83 -0
- data_factory/annotate/tools/serve.py +106 -0
- data_factory/cli/__init__.py +21 -0
- data_factory/cli/main.py +837 -0
- data_factory/cli/menu.py +173 -0
- data_factory/cli/render.py +158 -0
- data_factory/core/__init__.py +11 -0
- data_factory/core/annotations/__init__.py +5 -0
- data_factory/core/annotations/coco.py +394 -0
- data_factory/core/annotations/csvfmt.py +174 -0
- data_factory/core/annotations/dota.py +98 -0
- data_factory/core/annotations/formats.py +626 -0
- data_factory/core/annotations/labelme.py +153 -0
- data_factory/core/annotations/model.py +334 -0
- data_factory/core/annotations/voc.py +231 -0
- data_factory/core/annotations/yolo.py +283 -0
- data_factory/core/bktree.py +89 -0
- data_factory/core/fs.py +245 -0
- data_factory/core/imaging.py +626 -0
- data_factory/core/ops/__init__.py +44 -0
- data_factory/core/ops/analyze.py +383 -0
- data_factory/core/ops/annotate_export.py +326 -0
- data_factory/core/ops/audit.py +274 -0
- data_factory/core/ops/augment.py +454 -0
- data_factory/core/ops/collect.py +241 -0
- data_factory/core/ops/combine.py +582 -0
- data_factory/core/ops/convert.py +212 -0
- data_factory/core/ops/dataset.py +246 -0
- data_factory/core/ops/dedup.py +235 -0
- data_factory/core/ops/export.py +168 -0
- data_factory/core/ops/image.py +407 -0
- data_factory/core/ops/labels.py +215 -0
- data_factory/core/ops/pairing.py +133 -0
- data_factory/core/ops/rename.py +154 -0
- data_factory/core/ops/report.py +323 -0
- data_factory/core/ops/visualize.py +164 -0
- data_factory/core/pairs.py +144 -0
- data_factory/core/parallel.py +191 -0
- data_factory/core/provenance.py +136 -0
- data_factory/core/registry.py +339 -0
- data_factory/core/stats.py +403 -0
- data_factory/core/types.py +148 -0
- data_factory/core/writer.py +194 -0
- data_factory/desktop.py +207 -0
- data_factory/hardware.py +243 -0
- data_factory/pipeline.py +192 -0
- data_factory/py.typed +0 -0
- data_factory/service/__init__.py +26 -0
- data_factory/service/app.py +483 -0
- data_factory/service/binding.py +174 -0
- data_factory/service/deps.py +133 -0
- data_factory/service/jobs.py +177 -0
- data_factory/service/limits.py +71 -0
- data_factory/service/routers/__init__.py +11 -0
- data_factory/service/routers/annotate.py +582 -0
- data_factory/service/routers/local.py +464 -0
- data_factory/service/routers/media.py +154 -0
- data_factory/service/routers/predictions.py +116 -0
- data_factory/service/routers/promote.py +182 -0
- data_factory/service/routers/tools.py +308 -0
- data_factory/service/static/annotate/api.js +246 -0
- data_factory/service/static/annotate/app.js +1696 -0
- data_factory/service/static/annotate/assist.js +106 -0
- data_factory/service/static/annotate/filmstrip.js +286 -0
- data_factory/service/static/annotate/hit.js +146 -0
- data_factory/service/static/annotate/labels.js +197 -0
- data_factory/service/static/annotate/render.js +286 -0
- data_factory/service/static/annotate/scene.js +277 -0
- data_factory/service/static/annotate/shapelist.js +72 -0
- data_factory/service/static/annotate/tools/box.js +96 -0
- data_factory/service/static/annotate/tools/line.js +96 -0
- data_factory/service/static/annotate/tools/obb.js +88 -0
- data_factory/service/static/annotate/tools/point.js +49 -0
- data_factory/service/static/annotate/tools/polygon.js +110 -0
- data_factory/service/static/annotate/tools/select.js +227 -0
- data_factory/service/static/annotate/undo.js +176 -0
- data_factory/service/static/annotate/viewport.js +156 -0
- data_factory/service/static/annotate.html +390 -0
- data_factory/service/static/index.html +632 -0
- data_factory/service/workspace.py +244 -0
- easyannotate/__init__.py +64 -0
- easyannotate/__main__.py +5 -0
- easyannotate/project.py +181 -0
- easyannotate/py.typed +0 -0
- easyannotate-0.4.0.dist-info/METADATA +767 -0
- easyannotate-0.4.0.dist-info/RECORD +109 -0
- easyannotate-0.4.0.dist-info/WHEEL +4 -0
- easyannotate-0.4.0.dist-info/entry_points.txt +4 -0
- easyannotate-0.4.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,1696 @@
|
|
|
1
|
+
// The editor: wiring, keyboard, and autosave.
|
|
2
|
+
//
|
|
3
|
+
// Everything with an opinion lives in the modules this imports. What is here
|
|
4
|
+
// is the loop, the shortcuts, and the rules about when work reaches the
|
|
5
|
+
// server.
|
|
6
|
+
//
|
|
7
|
+
// Two of those rules are the product:
|
|
8
|
+
//
|
|
9
|
+
// * There is no Save button. Edits are saved 400ms after you stop, and
|
|
10
|
+
// before you navigate. A Save button is a way to lose an afternoon.
|
|
11
|
+
// * Opening a project resumes where you left off. Not at image 1.
|
|
12
|
+
|
|
13
|
+
import { Api, ApiError, Conflict } from "./api.js";
|
|
14
|
+
import { Filmstrip } from "./filmstrip.js";
|
|
15
|
+
import { LabelRail } from "./labels.js";
|
|
16
|
+
import { hitTest } from "./hit.js";
|
|
17
|
+
import { draw } from "./render.js";
|
|
18
|
+
import { Scene, attributesOf, centroid, cloneShape, draftId, rebuildBox, rotatePoints } from "./scene.js";
|
|
19
|
+
import { ShapeList } from "./shapelist.js";
|
|
20
|
+
import { AssistPanel } from "./assist.js";
|
|
21
|
+
import {
|
|
22
|
+
UndoStack,
|
|
23
|
+
addMany,
|
|
24
|
+
addShape,
|
|
25
|
+
changeAttributes,
|
|
26
|
+
changeGeometry,
|
|
27
|
+
changeLabel,
|
|
28
|
+
changeMany,
|
|
29
|
+
removeShape,
|
|
30
|
+
snapshot,
|
|
31
|
+
} from "./undo.js";
|
|
32
|
+
import { Viewport, decodePixels, releaseMips } from "./viewport.js";
|
|
33
|
+
// tools/ are the *drawing* tools. assist.js is the panel for optional
|
|
34
|
+
// model-backed helpers -- two different things that were both called
|
|
35
|
+
// "tools" until the import block made the collision obvious.
|
|
36
|
+
import * as boxTool from "./tools/box.js";
|
|
37
|
+
import * as lineTool from "./tools/line.js";
|
|
38
|
+
import * as obbTool from "./tools/obb.js";
|
|
39
|
+
import * as pointTool from "./tools/point.js";
|
|
40
|
+
import * as polygonTool from "./tools/polygon.js";
|
|
41
|
+
import * as selectTool from "./tools/select.js";
|
|
42
|
+
|
|
43
|
+
const SAVE_DELAY = 400;
|
|
44
|
+
const PIXEL_CACHE = 3;
|
|
45
|
+
|
|
46
|
+
class Editor {
|
|
47
|
+
constructor() {
|
|
48
|
+
this.api = new Api();
|
|
49
|
+
this.scene = new Scene();
|
|
50
|
+
this.view = new Viewport();
|
|
51
|
+
this.undo = new UndoStack();
|
|
52
|
+
|
|
53
|
+
this.canvas = document.getElementById("canvas");
|
|
54
|
+
this.ctx = this.canvas.getContext("2d", { alpha: false });
|
|
55
|
+
this.status = document.getElementById("status");
|
|
56
|
+
this.imageName = document.getElementById("image-name");
|
|
57
|
+
this.progress = document.getElementById("progress");
|
|
58
|
+
this.help = document.getElementById("help");
|
|
59
|
+
|
|
60
|
+
this.projectId = new URLSearchParams(location.search).get("project") || "";
|
|
61
|
+
this.project = null;
|
|
62
|
+
this.image = null;
|
|
63
|
+
this.mips = null;
|
|
64
|
+
this.pixelCache = new Map();
|
|
65
|
+
this.opening = 0;
|
|
66
|
+
this.saveTimer = null;
|
|
67
|
+
this.saving = null;
|
|
68
|
+
this.pendingGeometry = null;
|
|
69
|
+
// True while the selection is only "the shape just drawn". It keeps its
|
|
70
|
+
// handles, so it can be resized at once, but it does not capture a class
|
|
71
|
+
// pick -- choosing the class for the *next* box must not re-class the
|
|
72
|
+
// last one -- and a drag across it draws rather than moves.
|
|
73
|
+
this.selectionFromDrawing = false;
|
|
74
|
+
this.pressed = null;
|
|
75
|
+
this.spacePanning = false;
|
|
76
|
+
// Client-side only: filtering predictions is a slider, and a round trip
|
|
77
|
+
// per drag would make it feel broken.
|
|
78
|
+
this.minConfidence = 0;
|
|
79
|
+
|
|
80
|
+
this.tools = {
|
|
81
|
+
select: selectTool.create(this),
|
|
82
|
+
box: boxTool.create(this),
|
|
83
|
+
polygon: polygonTool.create(this),
|
|
84
|
+
line: lineTool.create(this),
|
|
85
|
+
point: pointTool.create(this),
|
|
86
|
+
obb: obbTool.create(this),
|
|
87
|
+
};
|
|
88
|
+
this.tool = this.tools.select;
|
|
89
|
+
|
|
90
|
+
// Hiding is a way of looking, kept per project in this browser only: it
|
|
91
|
+
// changes nothing anyone else sees and nothing that is exported.
|
|
92
|
+
this.hiddenClasses = new Set(readPreference(`hidden:${this.projectId}`, []));
|
|
93
|
+
this.hideAll = false;
|
|
94
|
+
this.look = readPreference(`look:${this.projectId}`, { brightness: 100, contrast: 100 });
|
|
95
|
+
|
|
96
|
+
this.rail = new LabelRail(document.getElementById("labels"), {
|
|
97
|
+
// Picking a class draws a held shape if there is one, and otherwise
|
|
98
|
+
// re-classes the selection -- the same as its number key.
|
|
99
|
+
onPick: (label) => {
|
|
100
|
+
if (this.pendingGeometry) this.applyPendingGeometry();
|
|
101
|
+
else this.assignClassToSelection(label);
|
|
102
|
+
},
|
|
103
|
+
onToggleHidden: (idx) => this.toggleClassHidden(idx),
|
|
104
|
+
onCreate: (name) => this.createLabel(name),
|
|
105
|
+
onRename: (idx, name) => this.renameLabel(idx, name),
|
|
106
|
+
onRetire: (idx, name) => this.retireLabel(idx, name),
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
this.assistPanel = new AssistPanel(document.getElementById("tools"), {
|
|
110
|
+
onArm: (tool) => {
|
|
111
|
+
this.armedTool = tool;
|
|
112
|
+
// Encode-ahead starts the first time a tool that wants it is armed,
|
|
113
|
+
// and from then on follows every image opened. Not before: encoding
|
|
114
|
+
// costs seconds of CPU per image, which nobody drawing plain boxes
|
|
115
|
+
// should pay for.
|
|
116
|
+
if (tool && (tool.requires || []).includes("prepare")) {
|
|
117
|
+
this.encodeTool = tool;
|
|
118
|
+
this.prepareAhead();
|
|
119
|
+
}
|
|
120
|
+
if (!tool) this.say("");
|
|
121
|
+
else if (this.assistPanel.wantsPoints(tool)) this.say(`${tool.title}: click the object`);
|
|
122
|
+
else this.say(`${tool.title}: drag a box around the object`);
|
|
123
|
+
},
|
|
124
|
+
onPrompt: (tool, text, scope) => this.runPrompt(tool, text, scope),
|
|
125
|
+
});
|
|
126
|
+
this.armedTool = null;
|
|
127
|
+
|
|
128
|
+
this.shapeList = new ShapeList(document.getElementById("shapes"), {
|
|
129
|
+
onSelect: (id, additive) => {
|
|
130
|
+
const shape = this.scene.get(id);
|
|
131
|
+
if (shape && !this.visible(shape)) {
|
|
132
|
+
this.say("that shape is hidden — show its class to edit it");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
this.scene.select(id, { additive });
|
|
136
|
+
this.onSelectionChanged();
|
|
137
|
+
},
|
|
138
|
+
onHover: (id) => {
|
|
139
|
+
this.scene.hover = id;
|
|
140
|
+
this.scene.invalidate();
|
|
141
|
+
},
|
|
142
|
+
colourFor: (shape) => this.rail.colourFor(shape),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
this.strip = new Filmstrip(document.getElementById("filmstrip"), {
|
|
146
|
+
onOpen: (id) => this.openImage(id),
|
|
147
|
+
thumbUrl: (id) => this.api.thumbUrl(this.projectId, id),
|
|
148
|
+
fetchPage: (query) => this.api.images(this.projectId, query),
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
this.bindCanvas();
|
|
152
|
+
this.bindKeyboard();
|
|
153
|
+
this.bindChrome();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// -- lifecycle ----------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
async boot() {
|
|
159
|
+
if (!this.projectId) {
|
|
160
|
+
this.say("no project chosen -- open one from the main page");
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
this.project = await this.api.project(this.projectId);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
this.say(describe(error));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
document.title = `${this.project.name} — EasyAnnotate`;
|
|
170
|
+
document.getElementById("project-name").textContent = this.project.name;
|
|
171
|
+
|
|
172
|
+
this.rail.setLabels(this.project.labels || []);
|
|
173
|
+
this.rail.setHidden(this.hiddenClasses);
|
|
174
|
+
|
|
175
|
+
// Heavy tools are optional. An empty list hides the panel entirely --
|
|
176
|
+
// never a greyed-out button, never an install nag. Asked for in
|
|
177
|
+
// parallel and never awaited before the first image: the first call
|
|
178
|
+
// starts the sidecar process, and the picture must not wait for a
|
|
179
|
+
// Python interpreter to boot (measured: 2.4s to first image when it did).
|
|
180
|
+
this.api
|
|
181
|
+
.tools()
|
|
182
|
+
.then((payload) => this.assistPanel.setTools(payload))
|
|
183
|
+
.catch(() => this.assistPanel.setTools({ tools: [] }));
|
|
184
|
+
this.showModelPanel();
|
|
185
|
+
|
|
186
|
+
const resume = this.project.resume;
|
|
187
|
+
const opening = resume ? this.openImage(resume.id) : null;
|
|
188
|
+
await this.loadImageList();
|
|
189
|
+
// The strip learns its length only now; if the image opened first, its
|
|
190
|
+
// scroll-to-current happened against an empty list. Do it again.
|
|
191
|
+
if (this.strip.currentId) this.strip.reveal(this.strip.currentId);
|
|
192
|
+
this.updateReviewPanel(this.project.agreement);
|
|
193
|
+
if (opening) await opening;
|
|
194
|
+
else this.say("this project has no images yet");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Only the counts: the strip fetches its own rows, a page at a time.
|
|
198
|
+
async loadImageList() {
|
|
199
|
+
const payload = await this.api.images(this.projectId, { limit: 1 });
|
|
200
|
+
this.strip.reset(payload.counts);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async openImage(imageId) {
|
|
204
|
+
// Holding N fires opens faster than they finish. Each takes a ticket and
|
|
205
|
+
// only the newest may touch the screen -- otherwise whichever request
|
|
206
|
+
// lands last wins, and one image's pixels sit under another's shapes.
|
|
207
|
+
const ticket = ++this.opening;
|
|
208
|
+
|
|
209
|
+
// Never navigate away from unsaved work. This is the single most likely
|
|
210
|
+
// place to lose an edit, so it is awaited rather than fired and forgotten.
|
|
211
|
+
await this.flushSave();
|
|
212
|
+
if (ticket !== this.opening) return;
|
|
213
|
+
|
|
214
|
+
let payload;
|
|
215
|
+
let pixels;
|
|
216
|
+
try {
|
|
217
|
+
// Shapes and pixels together, and the pixels are usually decoded
|
|
218
|
+
// already: the previous open prefetched its neighbours.
|
|
219
|
+
[payload, pixels] = await Promise.all([
|
|
220
|
+
this.api.image(this.projectId, imageId),
|
|
221
|
+
this.pixels(imageId),
|
|
222
|
+
]);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (ticket === this.opening) this.say(describe(error));
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (ticket !== this.opening) return;
|
|
228
|
+
|
|
229
|
+
// Everything below swaps in one go, so there is never a frame that shows
|
|
230
|
+
// the old picture with the new shapes.
|
|
231
|
+
this.image = payload;
|
|
232
|
+
this.scene.load(payload);
|
|
233
|
+
this.undo.clear();
|
|
234
|
+
this.pendingGeometry = null;
|
|
235
|
+
this.mips = pixels ? pixels.mips : null;
|
|
236
|
+
// The database is the authority on size, but if a row predates the EXIF
|
|
237
|
+
// work its dimensions could disagree with the pixels. Trust what decoded,
|
|
238
|
+
// or every box on this image is drawn in the wrong space.
|
|
239
|
+
if (pixels && pixels.width && pixels.height) {
|
|
240
|
+
this.scene.width = pixels.width;
|
|
241
|
+
this.scene.height = pixels.height;
|
|
242
|
+
}
|
|
243
|
+
this.view.setImageSize(this.scene.width, this.scene.height);
|
|
244
|
+
this.view.fit(this.canvas.getBoundingClientRect());
|
|
245
|
+
this.strip.setCurrent(imageId);
|
|
246
|
+
this.imageName.textContent = payload.name;
|
|
247
|
+
this.setStatusDot("saved");
|
|
248
|
+
this.cancelTool();
|
|
249
|
+
this.updateReviewPanel(null);
|
|
250
|
+
this.scene.invalidate();
|
|
251
|
+
this.say(
|
|
252
|
+
pixels
|
|
253
|
+
? `${payload.name} — ${payload.shapes.length} shape(s)`
|
|
254
|
+
: "that image could not be loaded — its shapes are still editable",
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
this.prepareAhead();
|
|
258
|
+
|
|
259
|
+
// Warm both neighbours: N is the common case, E the second most common.
|
|
260
|
+
for (const neighbour of [payload.next, payload.previous]) {
|
|
261
|
+
if (neighbour) this.pixels(neighbour);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Ask the sidecar to encode the open image, so the first click is fast.
|
|
266
|
+
// Debounced: holding N must not send a prepare per image flown past.
|
|
267
|
+
prepareAhead() {
|
|
268
|
+
if (!this.encodeTool || !this.image) return;
|
|
269
|
+
clearTimeout(this.prepareTimer);
|
|
270
|
+
const imageId = this.image.id;
|
|
271
|
+
const name = this.encodeTool.name;
|
|
272
|
+
this.prepareTimer = setTimeout(() => {
|
|
273
|
+
if (this.image && this.image.id === imageId) {
|
|
274
|
+
this.api.prepareTool(this.projectId, name, imageId).catch(() => {});
|
|
275
|
+
}
|
|
276
|
+
}, 250);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Decoded pixels by image id, least recently used first. Kept small on
|
|
280
|
+
// purpose: each entry is a full-resolution image plus its mips, and three
|
|
281
|
+
// covers the previous, current and next image.
|
|
282
|
+
pixels(imageId) {
|
|
283
|
+
let entry = this.pixelCache.get(imageId);
|
|
284
|
+
if (entry) {
|
|
285
|
+
this.pixelCache.delete(imageId);
|
|
286
|
+
} else {
|
|
287
|
+
entry = decodePixels(this.api.imageUrl(this.projectId, imageId));
|
|
288
|
+
// A failure is not cached: the next attempt may well succeed.
|
|
289
|
+
entry.then((result) => {
|
|
290
|
+
if (!result && this.pixelCache.get(imageId) === entry) this.pixelCache.delete(imageId);
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
this.pixelCache.set(imageId, entry);
|
|
294
|
+
while (this.pixelCache.size > PIXEL_CACHE) {
|
|
295
|
+
const oldest = this.pixelCache.keys().next().value;
|
|
296
|
+
const evicted = this.pixelCache.get(oldest);
|
|
297
|
+
this.pixelCache.delete(oldest);
|
|
298
|
+
evicted.then((result) => {
|
|
299
|
+
if (result && result.mips !== this.mips) releaseMips(result.mips);
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
return entry;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// -- saving -------------------------------------------------------------
|
|
306
|
+
|
|
307
|
+
queueSave() {
|
|
308
|
+
clearTimeout(this.saveTimer);
|
|
309
|
+
this.setStatusDot("pending");
|
|
310
|
+
this.saveTimer = setTimeout(() => this.save(), SAVE_DELAY);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
async flushSave() {
|
|
314
|
+
clearTimeout(this.saveTimer);
|
|
315
|
+
if (this.saving) await this.saving.catch(() => {});
|
|
316
|
+
if (this.scene.changed) await this.save();
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
async save() {
|
|
320
|
+
// One save at a time. Two in flight would both carry the same base
|
|
321
|
+
// version, the second would 409, and the conflict path would reload the
|
|
322
|
+
// server copy over the user's newest edit.
|
|
323
|
+
while (this.saving) await this.saving;
|
|
324
|
+
if (!this.image) return;
|
|
325
|
+
if (!this.scene.changed) {
|
|
326
|
+
// Nothing to send. Say so, rather than leaving "unsaved" on screen --
|
|
327
|
+
// an indicator that is wrong even once is one nobody trusts again, and
|
|
328
|
+
// this is the only thing telling people their work is safe.
|
|
329
|
+
this.setStatusDot("saved");
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const imageId = this.scene.imageId;
|
|
333
|
+
const revision = this.scene.revision;
|
|
334
|
+
const submitted = this.scene.toWire();
|
|
335
|
+
const localIds = this.scene.order.slice();
|
|
336
|
+
|
|
337
|
+
this.setStatusDot("saving");
|
|
338
|
+
this.saving = this.api
|
|
339
|
+
.saveShapes(this.projectId, imageId, this.scene.version, submitted)
|
|
340
|
+
.then((result) => {
|
|
341
|
+
// Only reconcile if we are still on the image we saved. Navigating
|
|
342
|
+
// mid-flight is normal, and applying the result to a different image
|
|
343
|
+
// would corrupt it.
|
|
344
|
+
if (this.scene.imageId !== imageId) return;
|
|
345
|
+
this.scene.version = result.version;
|
|
346
|
+
// The server may assign a new id to any shape, not only drafts -- an
|
|
347
|
+
// undone delete comes back under an id it no longer has. Adopt every
|
|
348
|
+
// one, or the next save inserts that shape a second time.
|
|
349
|
+
(result.ids || []).forEach((serverId, index) => {
|
|
350
|
+
const local = localIds[index];
|
|
351
|
+
if (local && local !== serverId) this.scene.reid(local, serverId);
|
|
352
|
+
});
|
|
353
|
+
if (result.labels) this.rail.setLabels(result.labels);
|
|
354
|
+
if (result.state) this.strip.setState(imageId, result.state);
|
|
355
|
+
this.scene.invalidate();
|
|
356
|
+
if (this.scene.revision === revision) {
|
|
357
|
+
this.scene.changed = false;
|
|
358
|
+
this.setStatusDot("saved");
|
|
359
|
+
} else {
|
|
360
|
+
// Edited while the request was in flight: that work is not saved
|
|
361
|
+
// yet, so neither the flag nor the dot may say it is.
|
|
362
|
+
this.queueSave();
|
|
363
|
+
}
|
|
364
|
+
})
|
|
365
|
+
.catch(async (error) => {
|
|
366
|
+
if (this.scene.imageId !== imageId) {
|
|
367
|
+
this.say(`an earlier image failed to save: ${describe(error)}`);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
if (error instanceof Conflict) {
|
|
371
|
+
// Somebody else moved this image. Take their version rather than
|
|
372
|
+
// overwrite it, and say so plainly.
|
|
373
|
+
this.scene.load(error.current);
|
|
374
|
+
this.undo.clear();
|
|
375
|
+
this.setStatusDot("conflict");
|
|
376
|
+
this.say("this image was changed elsewhere — reloaded the newer version");
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
this.setStatusDot("error");
|
|
380
|
+
this.say(describe(error));
|
|
381
|
+
})
|
|
382
|
+
.finally(() => {
|
|
383
|
+
this.saving = null;
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
await this.saving;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
setStatusDot(state) {
|
|
390
|
+
this.status.dataset.state = state;
|
|
391
|
+
this.status.textContent = {
|
|
392
|
+
pending: "unsaved",
|
|
393
|
+
saving: "saving…",
|
|
394
|
+
saved: "saved",
|
|
395
|
+
conflict: "reloaded",
|
|
396
|
+
error: "not saved",
|
|
397
|
+
}[state] || "";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// -- labels -------------------------------------------------------------
|
|
401
|
+
|
|
402
|
+
currentLabel() {
|
|
403
|
+
return this.rail.current;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
async createLabel(name) {
|
|
407
|
+
try {
|
|
408
|
+
await this.api.createLabel(this.projectId, name);
|
|
409
|
+
const payload = await this.api.labels(this.projectId);
|
|
410
|
+
this.rail.setLabels(payload.labels);
|
|
411
|
+
const made = payload.labels.find((l) => l.name === name.trim());
|
|
412
|
+
if (made) {
|
|
413
|
+
this.rail.pick(made);
|
|
414
|
+
// Replace the "name this class" prompt: a hint still asking for a
|
|
415
|
+
// name after it was given reads as the name not having taken.
|
|
416
|
+
const position = this.rail.labels.indexOf(made);
|
|
417
|
+
const key = position >= 0 && position < 10 ? ` · key ${(position + 1) % 10}` : "";
|
|
418
|
+
this.say(`drawing "${made.name}"${key}`);
|
|
419
|
+
}
|
|
420
|
+
// Hand the keyboard back to the canvas: naming a class is part of
|
|
421
|
+
// drawing, and the next keystroke belongs to the drawing.
|
|
422
|
+
this.rail.releaseInput();
|
|
423
|
+
} catch (error) {
|
|
424
|
+
this.say(describe(error));
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async renameLabel(idx, name) {
|
|
429
|
+
// Renaming a class onto one that already exists is how people try to merge
|
|
430
|
+
// duplicates -- and the real door set has four of them (Close/close/
|
|
431
|
+
// Open/open). Refusing outright leaves them stuck, so offer the merge they
|
|
432
|
+
// were reaching for. It is destructive, so it is asked, not assumed.
|
|
433
|
+
const clash = this.rail.labels.find(
|
|
434
|
+
(label) => label.name.toLowerCase() === name.trim().toLowerCase() && label.idx !== idx
|
|
435
|
+
);
|
|
436
|
+
if (clash) {
|
|
437
|
+
const from = this.rail.labels.find((label) => label.idx === idx);
|
|
438
|
+
const ok = confirm(
|
|
439
|
+
`"${clash.name}" already exists.\n\n` +
|
|
440
|
+
`Merge "${from ? from.name : idx}" into it? Every shape using it moves ` +
|
|
441
|
+
`to "${clash.name}", and class index ${idx} is retired.\n\n` +
|
|
442
|
+
`Class indices already exported do not change.`
|
|
443
|
+
);
|
|
444
|
+
if (!ok) return;
|
|
445
|
+
try {
|
|
446
|
+
const result = await this.api.mergeLabel(this.projectId, idx, clash.idx);
|
|
447
|
+
this.rail.setLabels(result.labels);
|
|
448
|
+
await this.openImage(this.image.id);
|
|
449
|
+
this.say(`merged — ${result.moved} shape(s) moved to "${clash.name}"`);
|
|
450
|
+
} catch (error) {
|
|
451
|
+
this.say(describe(error));
|
|
452
|
+
}
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
try {
|
|
457
|
+
await this.api.renameLabel(this.projectId, idx, name);
|
|
458
|
+
const payload = await this.api.labels(this.projectId);
|
|
459
|
+
this.rail.setLabels(payload.labels);
|
|
460
|
+
// Shapes carry the name for display, so refresh them from the index.
|
|
461
|
+
for (const shape of this.scene.shapes.values()) {
|
|
462
|
+
const label = payload.labels.find((l) => l.idx === shape.labelIdx);
|
|
463
|
+
if (label) shape.label = label.name;
|
|
464
|
+
}
|
|
465
|
+
this.scene.invalidate();
|
|
466
|
+
this.say(`renamed to "${name}" — the class index did not move`);
|
|
467
|
+
} catch (error) {
|
|
468
|
+
this.say(describe(error));
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async retireLabel(idx, name) {
|
|
473
|
+
if (!confirm(`Hide "${name}" from the palette? Shapes already drawn with it keep it.`)) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
try {
|
|
477
|
+
await this.api.retireLabel(this.projectId, idx);
|
|
478
|
+
const payload = await this.api.labels(this.projectId);
|
|
479
|
+
this.rail.setLabels(payload.labels);
|
|
480
|
+
this.say(`"${name}" retired — its class index stays reserved`);
|
|
481
|
+
} catch (error) {
|
|
482
|
+
this.say(describe(error));
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// A shape was drawn before any class existed. Hold the geometry, ask for a
|
|
487
|
+
// name, and apply it the moment one arrives -- rather than discarding the
|
|
488
|
+
// drag and telling the user to go and configure something.
|
|
489
|
+
promptForLabel(geometry) {
|
|
490
|
+
this.pendingGeometry = geometry;
|
|
491
|
+
this.say("name this class, then press Enter");
|
|
492
|
+
this.rail.focusInput();
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
applyPendingGeometry() {
|
|
496
|
+
const geometry = this.pendingGeometry;
|
|
497
|
+
const label = this.currentLabel();
|
|
498
|
+
if (!geometry || !label) return;
|
|
499
|
+
this.pendingGeometry = null;
|
|
500
|
+
const shape = this.scene.clampShape({
|
|
501
|
+
id: draftId(),
|
|
502
|
+
label: label.name,
|
|
503
|
+
labelIdx: label.idx,
|
|
504
|
+
bbox: geometry.bbox,
|
|
505
|
+
polygon: geometry.polygon,
|
|
506
|
+
kind: geometry.kind || (geometry.polygon ? "polygon" : "box"),
|
|
507
|
+
origin: "human",
|
|
508
|
+
confidence: null,
|
|
509
|
+
edited: false,
|
|
510
|
+
});
|
|
511
|
+
this.undo.run(addShape(this.scene, shape));
|
|
512
|
+
this.drew();
|
|
513
|
+
this.queueSave();
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
drew() {
|
|
517
|
+
this.selectionFromDrawing = true;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
assignClassToSelection(label) {
|
|
521
|
+
if (this.selectionFromDrawing) return false;
|
|
522
|
+
const selected = this.scene.selected();
|
|
523
|
+
if (!selected.length || !label) return false;
|
|
524
|
+
let changed = 0;
|
|
525
|
+
for (const shape of selected) {
|
|
526
|
+
if (shape.labelIdx === label.idx) continue;
|
|
527
|
+
this.undo.run(
|
|
528
|
+
changeLabel(
|
|
529
|
+
this.scene,
|
|
530
|
+
shape.id,
|
|
531
|
+
{ label: shape.label, labelIdx: shape.labelIdx },
|
|
532
|
+
{ label: label.name, labelIdx: label.idx }
|
|
533
|
+
)
|
|
534
|
+
);
|
|
535
|
+
this.markEdited(shape);
|
|
536
|
+
changed += 1;
|
|
537
|
+
}
|
|
538
|
+
// Re-picking the class a shape already has is a no-op, not an edit. Saving
|
|
539
|
+
// anyway would mark the image unsaved for something that changed nothing.
|
|
540
|
+
if (changed) this.queueSave();
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// A person touching a model shape makes it theirs: it stops rendering
|
|
545
|
+
// dashed, and starts counting as a correction in the agreement figure.
|
|
546
|
+
markEdited(shape) {
|
|
547
|
+
if (shape && shape.origin === "model" && !shape.edited) {
|
|
548
|
+
shape.edited = true;
|
|
549
|
+
this.scene.invalidate();
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
onSelectionChanged() {
|
|
554
|
+
this.selectionFromDrawing = false;
|
|
555
|
+
const selected = this.scene.selected();
|
|
556
|
+
if (selected.length === 1) {
|
|
557
|
+
const label = this.rail.labels.find((l) => l.idx === selected[0].labelIdx);
|
|
558
|
+
if (label) this.rail.pick(label);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// -- canvas -------------------------------------------------------------
|
|
563
|
+
|
|
564
|
+
bindCanvas() {
|
|
565
|
+
const resize = () => {
|
|
566
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
567
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
568
|
+
this.canvas.width = Math.round(rect.width * dpr);
|
|
569
|
+
this.canvas.height = Math.round(rect.height * dpr);
|
|
570
|
+
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
571
|
+
this.scene.invalidate();
|
|
572
|
+
};
|
|
573
|
+
new ResizeObserver(resize).observe(this.canvas);
|
|
574
|
+
resize();
|
|
575
|
+
|
|
576
|
+
this.canvas.addEventListener("pointerdown", (event) => {
|
|
577
|
+
this.canvas.setPointerCapture(event.pointerId);
|
|
578
|
+
const [ix, iy] = this.view.toImage(event.offsetX, event.offsetY);
|
|
579
|
+
|
|
580
|
+
// Middle button and held space always pan, whatever tool is active.
|
|
581
|
+
if (event.button === 1 || this.spacePanning) {
|
|
582
|
+
this.panning = { x: event.offsetX, y: event.offsetY };
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
// Right-click belongs to the context-menu delete. Letting it through
|
|
586
|
+
// to the tools added a polygon vertex or started a box underneath it.
|
|
587
|
+
if (event.button !== 0) return;
|
|
588
|
+
// A click-prompted helper (SAM from a point) takes the click itself.
|
|
589
|
+
if (this.armedTool && this.assistPanel.wantsPoints(this.armedTool)) {
|
|
590
|
+
this.runTool(this.armedTool, { points: [[Math.round(ix), Math.round(iy)]] });
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
// Left-drag with the select tool draws a box: no mode to enter, which
|
|
594
|
+
// is the entire interaction argument of this product. That includes a
|
|
595
|
+
// drag starting on a shape you have not selected -- a handle on a
|
|
596
|
+
// door, a plate on a car -- because nested objects are the common case,
|
|
597
|
+
// and having to switch tools to draw inside a box was the first thing
|
|
598
|
+
// to break the flow. A click there still selects; a drag on a shape
|
|
599
|
+
// you *have* selected moves it.
|
|
600
|
+
if (this.tool === this.tools.select) {
|
|
601
|
+
const hit = hitTest(this.scene, ix, iy, this.view.scale, (s) => this.visible(s));
|
|
602
|
+
if (!hit) {
|
|
603
|
+
this.tools.box.pointerdown(event, ix, iy);
|
|
604
|
+
this.drawingBox = true;
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
const owned = this.scene.selection.has(hit.id) && !this.selectionFromDrawing;
|
|
608
|
+
if (hit.part === "body" && !owned && !event.shiftKey && !event.altKey) {
|
|
609
|
+
this.pressed = { id: hit.id, sx: event.offsetX, sy: event.offsetY, ix, iy };
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
this.tool.pointerdown(event, ix, iy);
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
this.canvas.addEventListener("pointermove", (event) => {
|
|
617
|
+
const [ix, iy] = this.view.toImage(event.offsetX, event.offsetY);
|
|
618
|
+
this.pointer = { x: event.offsetX, y: event.offsetY };
|
|
619
|
+
if (this.wantsCrosshair()) this.scene.invalidate();
|
|
620
|
+
if (this.panning) {
|
|
621
|
+
this.view.panBy(event.offsetX - this.panning.x, event.offsetY - this.panning.y);
|
|
622
|
+
this.panning = { x: event.offsetX, y: event.offsetY };
|
|
623
|
+
this.scene.invalidate();
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
if (this.pressed) {
|
|
627
|
+
// Past a few pixels a press on a shape becomes a drawn box.
|
|
628
|
+
const { sx, sy } = this.pressed;
|
|
629
|
+
if (Math.hypot(event.offsetX - sx, event.offsetY - sy) > 4) {
|
|
630
|
+
this.tools.box.pointerdown(event, this.pressed.ix, this.pressed.iy);
|
|
631
|
+
this.drawingBox = true;
|
|
632
|
+
this.pressed = null;
|
|
633
|
+
this.tools.box.pointermove(event, ix, iy);
|
|
634
|
+
}
|
|
635
|
+
return;
|
|
636
|
+
}
|
|
637
|
+
if (this.drawingBox) {
|
|
638
|
+
this.tools.box.pointermove(event, ix, iy);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
this.tool.pointermove(event, ix, iy);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
const release = (event) => {
|
|
645
|
+
if (this.panning) {
|
|
646
|
+
this.panning = null;
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
if (this.pressed) {
|
|
650
|
+
// It never moved: a click, so it selects.
|
|
651
|
+
const { id } = this.pressed;
|
|
652
|
+
this.pressed = null;
|
|
653
|
+
this.scene.select(id);
|
|
654
|
+
this.onSelectionChanged();
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (this.drawingBox) {
|
|
658
|
+
if (this.armedTool) {
|
|
659
|
+
const draft = this.tools.box.draft();
|
|
660
|
+
this.tools.box.cancel();
|
|
661
|
+
this.drawingBox = false;
|
|
662
|
+
if (draft) this.runTool(this.armedTool, { box: draft.bbox });
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
this.tools.box.pointerup(event);
|
|
666
|
+
this.drawingBox = false;
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
this.tool.pointerup(event);
|
|
670
|
+
};
|
|
671
|
+
this.canvas.addEventListener("pointerleave", () => {
|
|
672
|
+
this.pointer = null;
|
|
673
|
+
this.scene.invalidate();
|
|
674
|
+
});
|
|
675
|
+
this.canvas.addEventListener("pointerup", release);
|
|
676
|
+
this.canvas.addEventListener("pointercancel", release);
|
|
677
|
+
|
|
678
|
+
this.canvas.addEventListener(
|
|
679
|
+
"wheel",
|
|
680
|
+
(event) => {
|
|
681
|
+
event.preventDefault();
|
|
682
|
+
// Firefox reports lines, not pixels, and its zoom was a crawl.
|
|
683
|
+
const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? 400 : 1;
|
|
684
|
+
const dx = event.deltaX * unit;
|
|
685
|
+
const dy = event.deltaY * unit;
|
|
686
|
+
if (event.ctrlKey) {
|
|
687
|
+
// A trackpad pinch arrives as a ctrl+wheel with small deltas.
|
|
688
|
+
this.view.zoomAt(event.offsetX, event.offsetY, Math.exp(-dy * 0.01));
|
|
689
|
+
} else if (looksLikeTrackpad(event, dx, dy)) {
|
|
690
|
+
// Two fingers move the picture, as in every map and design tool;
|
|
691
|
+
// zooming on them made a trackpad unusable.
|
|
692
|
+
this.view.panBy(-dx, -dy);
|
|
693
|
+
} else {
|
|
694
|
+
this.view.zoomAt(event.offsetX, event.offsetY, Math.exp(-dy * 0.0015));
|
|
695
|
+
}
|
|
696
|
+
this.scene.invalidate();
|
|
697
|
+
},
|
|
698
|
+
{ passive: false }
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
// Right-click deletes what is under the cursor. Faster than select-then-
|
|
702
|
+
// Delete, and the undo stack makes it safe.
|
|
703
|
+
this.canvas.addEventListener("contextmenu", (event) => {
|
|
704
|
+
event.preventDefault();
|
|
705
|
+
const [ix, iy] = this.view.toImage(event.offsetX, event.offsetY);
|
|
706
|
+
const hit = this.hitSomething(ix, iy);
|
|
707
|
+
if (hit) {
|
|
708
|
+
this.undo.run(removeShape(this.scene, hit));
|
|
709
|
+
this.queueSave();
|
|
710
|
+
}
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
requestAnimationFrame(() => this.frame());
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// The same test the select tool uses. A bbox-only check here treated the
|
|
717
|
+
// outer half of every resize handle as empty canvas -- grabbing a corner
|
|
718
|
+
// started a new box instead -- and the hollow of a concave polygon as solid.
|
|
719
|
+
hitSomething(ix, iy) {
|
|
720
|
+
const hit = hitTest(this.scene, ix, iy, this.view.scale, (s) => this.visible(s));
|
|
721
|
+
return hit ? hit.id : null;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
frame() {
|
|
725
|
+
if (this.scene.dirty) {
|
|
726
|
+
this.scene.dirty = false;
|
|
727
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
728
|
+
const draft = this.drawingBox ? this.tools.box.draft() : this.tool.draft();
|
|
729
|
+
draw(this.ctx, rect, this.scene, this.view, this.mips, {
|
|
730
|
+
colourFor: (shape) => this.rail.colourFor(shape),
|
|
731
|
+
visible: (shape) => this.visible(shape),
|
|
732
|
+
draft,
|
|
733
|
+
crosshair: this.wantsCrosshair() ? this.pointer : null,
|
|
734
|
+
imageFilter: this.imageFilter(),
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
this.updateAttributePanel();
|
|
738
|
+
this.shapeList.update(
|
|
739
|
+
this.scene,
|
|
740
|
+
(shape) => this.visible(shape),
|
|
741
|
+
`${this.hideAll}:${[...this.hiddenClasses].join(",")}:${this.minConfidence}`
|
|
742
|
+
);
|
|
743
|
+
requestAnimationFrame(() => this.frame());
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// Guides show whenever the next left-drag would draw: over empty canvas,
|
|
747
|
+
// with a drawing tool, or mid-draw. Over a shape they would only be noise.
|
|
748
|
+
wantsCrosshair() {
|
|
749
|
+
if (!this.pointer || this.panning || this.spacePanning) return false;
|
|
750
|
+
if (this.drawingBox || this.tool !== this.tools.select) return true;
|
|
751
|
+
// Wherever a drag would draw: empty canvas, or a shape not selected.
|
|
752
|
+
const hover = this.scene.hover;
|
|
753
|
+
return hover == null || !this.scene.selection.has(hover) || this.selectionFromDrawing;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// -- editing from the keyboard -------------------------------------------
|
|
757
|
+
|
|
758
|
+
// Move the selection by whole image pixels: 1, or 10 with Shift. A held
|
|
759
|
+
// key extends one undo step rather than filling the stack with repeats.
|
|
760
|
+
nudge(dx, dy) {
|
|
761
|
+
const selected = this.scene.selected().filter((s) => this.visible(s));
|
|
762
|
+
if (!selected.length) return false;
|
|
763
|
+
const now = performance.now();
|
|
764
|
+
const ids = selected.map((s) => s.id).sort().join(",");
|
|
765
|
+
const last = this.lastNudge;
|
|
766
|
+
const extending =
|
|
767
|
+
last && last.ids === ids && now - last.at < 700 && this.undo.past.at(-1) === last.command;
|
|
768
|
+
|
|
769
|
+
const entries = extending ? last.command.entries : [];
|
|
770
|
+
for (const shape of selected) {
|
|
771
|
+
let entry = entries.find((e) => this.scene.resolve(e.id) === shape.id);
|
|
772
|
+
if (!entry) {
|
|
773
|
+
entry = { id: shape.id, before: snapshot(shape), after: null };
|
|
774
|
+
entries.push(entry);
|
|
775
|
+
}
|
|
776
|
+
shape.bbox = shape.bbox.map((v, i) => v + (i % 2 === 0 ? dx : dy));
|
|
777
|
+
if (shape.polygon) shape.polygon = shape.polygon.map(([x, y]) => [x + dx, y + dy]);
|
|
778
|
+
this.scene.clampShape(shape);
|
|
779
|
+
entry.after = snapshot(shape);
|
|
780
|
+
this.markEdited(shape);
|
|
781
|
+
}
|
|
782
|
+
this.scene.touch();
|
|
783
|
+
if (extending) {
|
|
784
|
+
last.at = now;
|
|
785
|
+
} else {
|
|
786
|
+
const command = changeMany(this.scene, entries);
|
|
787
|
+
this.undo.push(command);
|
|
788
|
+
this.lastNudge = { ids, at: now, command };
|
|
789
|
+
}
|
|
790
|
+
this.queueSave();
|
|
791
|
+
return true;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// Turn the selected polygons and rotated boxes about their centres.
|
|
795
|
+
rotateSelection(degrees) {
|
|
796
|
+
const selected = this.scene
|
|
797
|
+
.selected()
|
|
798
|
+
.filter((s) => s.polygon && s.polygon.length >= 3 && this.visible(s));
|
|
799
|
+
if (!selected.length) return false;
|
|
800
|
+
for (const shape of selected) {
|
|
801
|
+
const before = snapshot(shape);
|
|
802
|
+
const [cx, cy] = centroid(shape.polygon);
|
|
803
|
+
shape.polygon = rotatePoints(shape.polygon, cx, cy, (degrees * Math.PI) / 180);
|
|
804
|
+
rebuildBox(shape);
|
|
805
|
+
this.scene.clampShape(shape);
|
|
806
|
+
this.undo.push(changeGeometry(this.scene, shape.id, before, snapshot(shape)));
|
|
807
|
+
this.markEdited(shape);
|
|
808
|
+
}
|
|
809
|
+
this.scene.touch();
|
|
810
|
+
this.queueSave();
|
|
811
|
+
return true;
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// Tab walks the visible shapes. The only way to reach a small box buried
|
|
815
|
+
// under a big one without moving the big one.
|
|
816
|
+
cycleSelection(direction) {
|
|
817
|
+
const ids = this.scene.order.filter((id) => this.visible(this.scene.get(id)));
|
|
818
|
+
if (!ids.length) return;
|
|
819
|
+
const current = this.scene.selection.size === 1 ? [...this.scene.selection][0] : null;
|
|
820
|
+
const at = current ? ids.indexOf(current) : -1;
|
|
821
|
+
const next = ids[(at + direction + ids.length) % ids.length] ?? ids[0];
|
|
822
|
+
this.scene.select(next);
|
|
823
|
+
this.onSelectionChanged();
|
|
824
|
+
const shape = this.scene.get(next);
|
|
825
|
+
const score = shape.confidence != null ? ` · ${Math.round(shape.confidence * 100)}%` : "";
|
|
826
|
+
this.say(`${shape.label}${score}`);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
copySelection() {
|
|
830
|
+
const selected = this.scene.selected();
|
|
831
|
+
if (!selected.length) return;
|
|
832
|
+
this.clipboard = { imageId: this.scene.imageId, shapes: selected.map(cloneShape) };
|
|
833
|
+
this.say(`copied ${selected.length} shape(s) — Ctrl+V pastes, on this image or another`);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// Pasting onto another image lands in place -- the point is carrying a box
|
|
837
|
+
// to the next frame. On the same image it is offset, or the copy would sit
|
|
838
|
+
// exactly on its original and look like nothing happened.
|
|
839
|
+
pasteClipboard() {
|
|
840
|
+
if (!this.clipboard || !this.clipboard.shapes.length || !this.image) return;
|
|
841
|
+
const offset = this.clipboard.imageId === this.scene.imageId ? 10 : 0;
|
|
842
|
+
const pasted = this.clipboard.shapes.map((source) =>
|
|
843
|
+
this.scene.clampShape({
|
|
844
|
+
...cloneShape(source),
|
|
845
|
+
id: draftId(),
|
|
846
|
+
bbox: source.bbox.map((v) => v + offset),
|
|
847
|
+
polygon: source.polygon ? source.polygon.map(([x, y]) => [x + offset, y + offset]) : null,
|
|
848
|
+
origin: "human",
|
|
849
|
+
confidence: null,
|
|
850
|
+
edited: false,
|
|
851
|
+
})
|
|
852
|
+
);
|
|
853
|
+
for (const shape of pasted) this.scene.add(shape);
|
|
854
|
+
this.undo.push(addMany(this.scene, pasted));
|
|
855
|
+
this.scene.clearSelection();
|
|
856
|
+
for (const shape of pasted) this.scene.select(shape.id, { additive: true });
|
|
857
|
+
this.drew();
|
|
858
|
+
this.queueSave();
|
|
859
|
+
this.say(`pasted ${pasted.length} shape(s)`);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// -- keyboard -----------------------------------------------------------
|
|
863
|
+
|
|
864
|
+
bindKeyboard() {
|
|
865
|
+
window.addEventListener("keydown", (event) => {
|
|
866
|
+
if (isTypingTarget(event.target)) return;
|
|
867
|
+
|
|
868
|
+
if (event.key === " " && !event.repeat) {
|
|
869
|
+
this.spacePanning = true;
|
|
870
|
+
this.canvas.style.cursor = "grab";
|
|
871
|
+
event.preventDefault();
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
if (this.tool.key && this.tool.key(event)) {
|
|
876
|
+
event.preventDefault();
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
const ctrl = event.ctrlKey || event.metaKey;
|
|
881
|
+
|
|
882
|
+
if (ctrl && event.key.toLowerCase() === "z") {
|
|
883
|
+
event.preventDefault();
|
|
884
|
+
if (event.shiftKey) this.undo.redo();
|
|
885
|
+
else this.undo.undo();
|
|
886
|
+
this.scene.invalidate();
|
|
887
|
+
this.queueSave();
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
if (ctrl && event.key.toLowerCase() === "y") {
|
|
891
|
+
event.preventDefault();
|
|
892
|
+
this.undo.redo();
|
|
893
|
+
this.scene.invalidate();
|
|
894
|
+
this.queueSave();
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
if (ctrl && event.key.toLowerCase() === "s") {
|
|
898
|
+
// There is no Save button, but muscle memory deserves an answer.
|
|
899
|
+
event.preventDefault();
|
|
900
|
+
this.flushSave();
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
if (ctrl && event.key.toLowerCase() === "d") {
|
|
904
|
+
event.preventDefault();
|
|
905
|
+
this.copyPrevious();
|
|
906
|
+
return;
|
|
907
|
+
}
|
|
908
|
+
if (ctrl && event.key.toLowerCase() === "c" && this.scene.selection.size) {
|
|
909
|
+
event.preventDefault();
|
|
910
|
+
this.copySelection();
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
if (ctrl && event.key.toLowerCase() === "v") {
|
|
914
|
+
event.preventDefault();
|
|
915
|
+
this.pasteClipboard();
|
|
916
|
+
return;
|
|
917
|
+
}
|
|
918
|
+
if (ctrl && event.key.toLowerCase() === "a") {
|
|
919
|
+
event.preventDefault();
|
|
920
|
+
this.scene.clearSelection();
|
|
921
|
+
for (const id of this.scene.order) {
|
|
922
|
+
if (this.visible(this.scene.get(id))) this.scene.select(id, { additive: true });
|
|
923
|
+
}
|
|
924
|
+
this.onSelectionChanged();
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (event.key === "Tab") {
|
|
928
|
+
event.preventDefault();
|
|
929
|
+
this.cycleSelection(event.shiftKey ? -1 : 1);
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
if (event.key.startsWith("Arrow")) {
|
|
933
|
+
const step = event.shiftKey ? 10 : 1;
|
|
934
|
+
const [dx, dy] = {
|
|
935
|
+
ArrowLeft: [-step, 0],
|
|
936
|
+
ArrowRight: [step, 0],
|
|
937
|
+
ArrowUp: [0, -step],
|
|
938
|
+
ArrowDown: [0, step],
|
|
939
|
+
}[event.key] || [0, 0];
|
|
940
|
+
event.preventDefault();
|
|
941
|
+
// With nothing selected, arrows move the view instead.
|
|
942
|
+
if (!this.nudge(dx, dy)) {
|
|
943
|
+
this.view.panBy(-dx * 5, -dy * 5);
|
|
944
|
+
this.scene.invalidate();
|
|
945
|
+
}
|
|
946
|
+
return;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
// Actual size is Shift+0, by physical key so it works on any layout.
|
|
950
|
+
// Plain 0 is class ten; it cannot be both.
|
|
951
|
+
if (event.shiftKey && event.code === "Digit0") {
|
|
952
|
+
event.preventDefault();
|
|
953
|
+
this.view.actual(this.canvas.getBoundingClientRect());
|
|
954
|
+
this.scene.invalidate();
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
if (/^[0-9]$/.test(event.key)) {
|
|
959
|
+
event.preventDefault();
|
|
960
|
+
const digit = Number(event.key);
|
|
961
|
+
const before = this.rail.current;
|
|
962
|
+
if (this.rail.pickByNumber(digit)) {
|
|
963
|
+
// With a selection, a number key re-classes it. Without one, it
|
|
964
|
+
// chooses what the next box will be.
|
|
965
|
+
if (!this.assignClassToSelection(this.rail.current) && before !== this.rail.current) {
|
|
966
|
+
this.say(`drawing "${this.rail.current.name}"`);
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
switch (event.key.toLowerCase()) {
|
|
973
|
+
case "escape":
|
|
974
|
+
this.pendingGeometry = null;
|
|
975
|
+
this.assistPanel.disarm();
|
|
976
|
+
this.cancelTool();
|
|
977
|
+
this.scene.clearSelection();
|
|
978
|
+
break;
|
|
979
|
+
case "delete":
|
|
980
|
+
case "backspace": {
|
|
981
|
+
const selected = [...this.scene.selection];
|
|
982
|
+
if (!selected.length) break;
|
|
983
|
+
event.preventDefault();
|
|
984
|
+
for (const id of selected) this.undo.run(removeShape(this.scene, id));
|
|
985
|
+
this.queueSave();
|
|
986
|
+
break;
|
|
987
|
+
}
|
|
988
|
+
case "p":
|
|
989
|
+
this.setTool("polygon");
|
|
990
|
+
break;
|
|
991
|
+
case "v":
|
|
992
|
+
this.setTool("select");
|
|
993
|
+
break;
|
|
994
|
+
case "b":
|
|
995
|
+
this.setTool("box");
|
|
996
|
+
break;
|
|
997
|
+
case "k":
|
|
998
|
+
this.setTool("point");
|
|
999
|
+
break;
|
|
1000
|
+
case "l":
|
|
1001
|
+
this.setTool("line");
|
|
1002
|
+
break;
|
|
1003
|
+
case "o":
|
|
1004
|
+
this.setTool("obb");
|
|
1005
|
+
break;
|
|
1006
|
+
case "[":
|
|
1007
|
+
case "]": {
|
|
1008
|
+
const step = (event.shiftKey ? 15 : 1) * (event.key === "[" ? -1 : 1);
|
|
1009
|
+
if (this.rotateSelection(step)) event.preventDefault();
|
|
1010
|
+
break;
|
|
1011
|
+
}
|
|
1012
|
+
case "f":
|
|
1013
|
+
this.view.fit(this.canvas.getBoundingClientRect());
|
|
1014
|
+
this.scene.invalidate();
|
|
1015
|
+
break;
|
|
1016
|
+
case "n":
|
|
1017
|
+
this.step(1);
|
|
1018
|
+
break;
|
|
1019
|
+
case "e":
|
|
1020
|
+
this.step(-1);
|
|
1021
|
+
break;
|
|
1022
|
+
case "r":
|
|
1023
|
+
this.markReviewedAndAdvance();
|
|
1024
|
+
break;
|
|
1025
|
+
case "a":
|
|
1026
|
+
// Not Space, which is already held-to-pan. Dual-binding the biggest
|
|
1027
|
+
// key on the keyboard would mean panning on a predicted image
|
|
1028
|
+
// silently accepted every box on it.
|
|
1029
|
+
this.acceptAll();
|
|
1030
|
+
break;
|
|
1031
|
+
case "s":
|
|
1032
|
+
this.markState("skipped");
|
|
1033
|
+
break;
|
|
1034
|
+
case "h":
|
|
1035
|
+
// See the bare image -- the fastest check of whether a box is tight.
|
|
1036
|
+
this.hideAll = !this.hideAll;
|
|
1037
|
+
this.dropHiddenFromSelection();
|
|
1038
|
+
this.scene.invalidate();
|
|
1039
|
+
this.say(this.hideAll ? "shapes hidden — H to show them" : "shapes shown");
|
|
1040
|
+
break;
|
|
1041
|
+
case "/":
|
|
1042
|
+
event.preventDefault();
|
|
1043
|
+
this.rail.focusInput();
|
|
1044
|
+
break;
|
|
1045
|
+
default:
|
|
1046
|
+
break;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
if (event.key === "?" || (event.key === "/" && event.shiftKey)) {
|
|
1050
|
+
this.help.hidden = !this.help.hidden;
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
|
|
1054
|
+
// Alt-Tab with space held never delivers the keyup, and every click
|
|
1055
|
+
// afterwards would pan. Losing focus releases everything held.
|
|
1056
|
+
window.addEventListener("blur", () => {
|
|
1057
|
+
this.spacePanning = false;
|
|
1058
|
+
this.panning = null;
|
|
1059
|
+
this.canvas.style.cursor = this.tool === this.tools.select ? "default" : "crosshair";
|
|
1060
|
+
});
|
|
1061
|
+
|
|
1062
|
+
window.addEventListener("keyup", (event) => {
|
|
1063
|
+
if (event.key === " ") {
|
|
1064
|
+
this.spacePanning = false;
|
|
1065
|
+
this.panning = null;
|
|
1066
|
+
this.canvas.style.cursor = "default";
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
// A close with unsaved work is the one failure people never forgive.
|
|
1071
|
+
window.addEventListener("beforeunload", (event) => {
|
|
1072
|
+
if (this.scene.changed) {
|
|
1073
|
+
event.preventDefault();
|
|
1074
|
+
event.returnValue = "";
|
|
1075
|
+
}
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
bindChrome() {
|
|
1080
|
+
document.getElementById("btn-help").addEventListener("click", () => {
|
|
1081
|
+
this.help.hidden = !this.help.hidden;
|
|
1082
|
+
});
|
|
1083
|
+
this.help.addEventListener("click", () => {
|
|
1084
|
+
this.help.hidden = true;
|
|
1085
|
+
});
|
|
1086
|
+
document.getElementById("btn-fit").addEventListener("click", () => {
|
|
1087
|
+
this.view.fit(this.canvas.getBoundingClientRect());
|
|
1088
|
+
this.scene.invalidate();
|
|
1089
|
+
});
|
|
1090
|
+
document.getElementById("btn-prev").addEventListener("click", () => this.step(-1));
|
|
1091
|
+
document.getElementById("btn-next").addEventListener("click", () => this.step(1));
|
|
1092
|
+
document.getElementById("btn-review").addEventListener("click", () =>
|
|
1093
|
+
this.markReviewedAndAdvance()
|
|
1094
|
+
);
|
|
1095
|
+
for (const button of document.querySelectorAll("[data-tool]")) {
|
|
1096
|
+
button.addEventListener("click", () => this.setTool(button.dataset.tool));
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
for (const key of ["brightness", "contrast"]) {
|
|
1100
|
+
const control = document.getElementById(key);
|
|
1101
|
+
control.value = String(this.look[key]);
|
|
1102
|
+
control.addEventListener("input", () => {
|
|
1103
|
+
this.look = { ...this.look, [key]: Number(control.value) };
|
|
1104
|
+
writePreference(`look:${this.projectId}`, this.look);
|
|
1105
|
+
this.scene.invalidate();
|
|
1106
|
+
});
|
|
1107
|
+
control.addEventListener("change", () => control.blur());
|
|
1108
|
+
// Double-click puts it back, the quickest way out of a bad adjustment.
|
|
1109
|
+
control.addEventListener("dblclick", () => {
|
|
1110
|
+
control.value = "100";
|
|
1111
|
+
control.dispatchEvent(new Event("input"));
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
for (const box of document.querySelectorAll("#attributes [data-attr]")) {
|
|
1116
|
+
box.addEventListener("change", () => {
|
|
1117
|
+
this.setAttribute(box.dataset.attr, box.checked);
|
|
1118
|
+
box.blur();
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
const slider = document.getElementById("conf");
|
|
1123
|
+
slider.addEventListener("input", () => {
|
|
1124
|
+
this.minConfidence = Number(slider.value) / 100;
|
|
1125
|
+
document.getElementById("conf-value").textContent = `${slider.value}%`;
|
|
1126
|
+
this.scene.invalidate();
|
|
1127
|
+
});
|
|
1128
|
+
// Hand the keyboard back once the drag ends. A focused range input counts
|
|
1129
|
+
// as a typing target, so every shortcut went dead until the next click.
|
|
1130
|
+
slider.addEventListener("change", () => slider.blur());
|
|
1131
|
+
|
|
1132
|
+
document.getElementById("btn-accept").addEventListener("click", () => this.acceptAll());
|
|
1133
|
+
document.getElementById("btn-export").addEventListener("click", () => this.exportProject());
|
|
1134
|
+
document.getElementById("btn-train").addEventListener("click", () => this.runLoop("train"));
|
|
1135
|
+
document.getElementById("btn-predict").addEventListener("click", () => this.runLoop("predict"));
|
|
1136
|
+
|
|
1137
|
+
const picker = document.getElementById("predictions-file");
|
|
1138
|
+
document.getElementById("btn-predictions").addEventListener("click", () => picker.click());
|
|
1139
|
+
picker.addEventListener("change", () => {
|
|
1140
|
+
const file = picker.files && picker.files[0];
|
|
1141
|
+
picker.value = "";
|
|
1142
|
+
if (file) this.loadPredictions(file);
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// -- review -------------------------------------------------------------
|
|
1147
|
+
|
|
1148
|
+
// A shape hidden by the confidence slider is not deleted, only unseen: the
|
|
1149
|
+
// filter is a way of looking, not an edit. Accepting an image is what turns
|
|
1150
|
+
// what you can see into what you keep.
|
|
1151
|
+
imageFilter() {
|
|
1152
|
+
const { brightness, contrast } = this.look;
|
|
1153
|
+
if (brightness === 100 && contrast === 100) return "";
|
|
1154
|
+
return `brightness(${brightness}%) contrast(${contrast}%)`;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
// -- attributes -----------------------------------------------------------
|
|
1158
|
+
|
|
1159
|
+
// Shown for a selection, so the flags of what you are looking at are one
|
|
1160
|
+
// click away -- and set on every selected shape at once.
|
|
1161
|
+
updateAttributePanel() {
|
|
1162
|
+
const selected = this.scene.selected();
|
|
1163
|
+
const signature = `${this.scene.revision}|${selected.map((s) => s.id).join(",")}`;
|
|
1164
|
+
if (signature === this.attributeSignature) return;
|
|
1165
|
+
this.attributeSignature = signature;
|
|
1166
|
+
const panel = document.getElementById("attributes");
|
|
1167
|
+
panel.hidden = selected.length === 0;
|
|
1168
|
+
for (const box of panel.querySelectorAll("[data-attr]")) {
|
|
1169
|
+
const values = selected.map((s) => Number((s.attributes || {})[box.dataset.attr] || 0));
|
|
1170
|
+
box.checked = values.length > 0 && values.every(Boolean);
|
|
1171
|
+
box.indeterminate = values.some(Boolean) && !box.checked;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
setAttribute(key, on) {
|
|
1176
|
+
const selected = this.scene.selected();
|
|
1177
|
+
if (!selected.length) return;
|
|
1178
|
+
for (const shape of selected) {
|
|
1179
|
+
const before = { ...(shape.attributes || {}) };
|
|
1180
|
+
const after = { ...before, [key]: on ? 1 : 0 };
|
|
1181
|
+
if (Number(before[key] || 0) === after[key]) continue;
|
|
1182
|
+
this.undo.run(changeAttributes(this.scene, shape.id, before, after));
|
|
1183
|
+
this.markEdited(shape);
|
|
1184
|
+
}
|
|
1185
|
+
this.queueSave();
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
passesConfidence(shape) {
|
|
1189
|
+
if (shape.confidence == null) return true;
|
|
1190
|
+
return shape.confidence >= this.minConfidence;
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
// What is drawn and what can be grabbed. Three different reasons to hide,
|
|
1194
|
+
// and only the confidence slider is a judgement on the shape -- see
|
|
1195
|
+
// acceptAll, which must never treat a hidden *class* as rejected.
|
|
1196
|
+
visible(shape) {
|
|
1197
|
+
if (!shape || this.hideAll) return false;
|
|
1198
|
+
if (this.hiddenClasses.has(shape.labelIdx)) return false;
|
|
1199
|
+
return this.passesConfidence(shape);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
toggleClassHidden(idx) {
|
|
1203
|
+
if (this.hiddenClasses.has(idx)) this.hiddenClasses.delete(idx);
|
|
1204
|
+
else this.hiddenClasses.add(idx);
|
|
1205
|
+
writePreference(`hidden:${this.projectId}`, [...this.hiddenClasses]);
|
|
1206
|
+
this.rail.setHidden(this.hiddenClasses);
|
|
1207
|
+
this.dropHiddenFromSelection();
|
|
1208
|
+
this.scene.invalidate();
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// Never leave something selected that cannot be seen: Delete would remove
|
|
1212
|
+
// it, and arrows would move it, with no feedback at all.
|
|
1213
|
+
dropHiddenFromSelection() {
|
|
1214
|
+
for (const id of [...this.scene.selection]) {
|
|
1215
|
+
if (!this.visible(this.scene.get(id))) this.scene.selection.delete(id);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
hasPredictions() {
|
|
1220
|
+
for (const shape of this.scene.shapes.values()) {
|
|
1221
|
+
if (shape.origin === "model" && !shape.edited) return true;
|
|
1222
|
+
}
|
|
1223
|
+
return false;
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
updateReviewPanel(agreement) {
|
|
1227
|
+
const panel = document.getElementById("review");
|
|
1228
|
+
panel.hidden = !this.hasPredictions() && !(agreement && agreement.model_shapes);
|
|
1229
|
+
if (!agreement) return;
|
|
1230
|
+
const label = document.getElementById("agreement");
|
|
1231
|
+
if (agreement.agreement == null) {
|
|
1232
|
+
label.textContent = agreement.unreviewed
|
|
1233
|
+
? `${agreement.unreviewed} prediction(s) not yet reviewed`
|
|
1234
|
+
: "";
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
label.textContent =
|
|
1238
|
+
`model agreement ${Math.round(agreement.agreement * 100)}% ` +
|
|
1239
|
+
`(${agreement.corrected} of ${agreement.model_shapes} corrected)`;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
async acceptAll() {
|
|
1243
|
+
if (!this.image) return;
|
|
1244
|
+
// Anything the slider is hiding is a rejection, so it has to be removed
|
|
1245
|
+
// before the accept -- otherwise "accept what I can see" silently keeps
|
|
1246
|
+
// what you could not.
|
|
1247
|
+
// Only the confidence slider rejects. A class hidden to declutter the
|
|
1248
|
+
// view is still wanted, and deleting it here would be silent data loss.
|
|
1249
|
+
const hidden = [...this.scene.shapes.values()].filter((s) => !this.passesConfidence(s));
|
|
1250
|
+
if (hidden.length) {
|
|
1251
|
+
for (const shape of hidden) this.undo.run(removeShape(this.scene, shape.id));
|
|
1252
|
+
await this.flushSave();
|
|
1253
|
+
}
|
|
1254
|
+
try {
|
|
1255
|
+
const result = await this.api.accept(this.projectId, this.image.id);
|
|
1256
|
+
this.strip.setState(this.image.id, "reviewed");
|
|
1257
|
+
this.strip.setCounts(result.counts);
|
|
1258
|
+
this.updateReviewPanel(result.agreement);
|
|
1259
|
+
this.say(
|
|
1260
|
+
`accepted ${result.confirmed} prediction(s)` +
|
|
1261
|
+
(hidden.length ? `, rejected ${hidden.length}` : "")
|
|
1262
|
+
);
|
|
1263
|
+
if (this.strip.isQueue) await this.step(1);
|
|
1264
|
+
else if (result.next) await this.openImage(result.next);
|
|
1265
|
+
else this.say("that was the last image");
|
|
1266
|
+
} catch (error) {
|
|
1267
|
+
this.say(describe(error));
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
// -- your model -----------------------------------------------------------
|
|
1272
|
+
|
|
1273
|
+
// Shown only to the person at this computer with the AI tools installed:
|
|
1274
|
+
// training starts processes and writes models, which no web page may ask.
|
|
1275
|
+
async showModelPanel() {
|
|
1276
|
+
// No token: not opened from this computer, so not even worth the request.
|
|
1277
|
+
if (!this.api.localToken()) return;
|
|
1278
|
+
let state;
|
|
1279
|
+
try {
|
|
1280
|
+
state = await this.api.local("/tools");
|
|
1281
|
+
} catch {
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
if (!state.installed) return;
|
|
1285
|
+
document.getElementById("model").hidden = false;
|
|
1286
|
+
const profile = state.profile || {};
|
|
1287
|
+
document.getElementById("model-state").textContent =
|
|
1288
|
+
`on ${profile.verified_device || profile.device || "cpu"} · reviewed images train it`;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
async runLoop(kind) {
|
|
1292
|
+
await this.flushSave();
|
|
1293
|
+
const button = document.getElementById(kind === "train" ? "btn-train" : "btn-predict");
|
|
1294
|
+
button.disabled = true;
|
|
1295
|
+
const state = document.getElementById("model-state");
|
|
1296
|
+
try {
|
|
1297
|
+
const job = await this.api.local(`/${kind}`, { project: this.projectId, epochs: 30 });
|
|
1298
|
+
let status = job;
|
|
1299
|
+
while (!["finished", "failed", "cancelled"].includes(status.state)) {
|
|
1300
|
+
await pause(1000);
|
|
1301
|
+
status = await this.api.job(job.id);
|
|
1302
|
+
const progress = status.progress || {};
|
|
1303
|
+
if (progress.message) state.textContent = progress.message;
|
|
1304
|
+
}
|
|
1305
|
+
const result = status.result || {};
|
|
1306
|
+
if (status.state !== "finished" || result.ok === false) {
|
|
1307
|
+
state.textContent = `${kind} failed: ${(result.errors || []).join("; ") || status.error || status.state}`;
|
|
1308
|
+
return;
|
|
1309
|
+
}
|
|
1310
|
+
const data = result.data || {};
|
|
1311
|
+
const predicted = data.predicted || {};
|
|
1312
|
+
const score = (data.metrics || {})["metrics/mAP50(B)"];
|
|
1313
|
+
state.textContent =
|
|
1314
|
+
(kind === "train" ? `trained ${data.model}${score != null ? ` (mAP50 ${score.toFixed(2)})` : ""}; ` : "") +
|
|
1315
|
+
`${predicted.shapes ?? 0} prediction(s) on ${predicted.predicted ?? 0} image(s)` +
|
|
1316
|
+
(predicted.failed ? `; ${predicted.failed} failed (${predicted.first_error || "file missing"})` : "") +
|
|
1317
|
+
(predicted.skipped_changed ? `; ${predicted.skipped_changed} left alone: reviewed meanwhile` : "");
|
|
1318
|
+
await this.loadImageList();
|
|
1319
|
+
if (this.image) await this.openImage(this.image.id);
|
|
1320
|
+
this.say("review them hardest first: pick 'predicted, least sure first' in the image list");
|
|
1321
|
+
} catch (error) {
|
|
1322
|
+
state.textContent = describe(error);
|
|
1323
|
+
} finally {
|
|
1324
|
+
button.disabled = false;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// -- export ---------------------------------------------------------------
|
|
1329
|
+
|
|
1330
|
+
// The download is the point of all of this. Reviewed images only, unless
|
|
1331
|
+
// asked -- and skipped ones never, which is a promise, not a default.
|
|
1332
|
+
async exportProject() {
|
|
1333
|
+
if (!this.projectId) return;
|
|
1334
|
+
await this.flushSave();
|
|
1335
|
+
const to = document.getElementById("export-format").value;
|
|
1336
|
+
const everything = document.getElementById("export-all").checked;
|
|
1337
|
+
const button = document.getElementById("btn-export");
|
|
1338
|
+
button.disabled = true;
|
|
1339
|
+
this.say(`exporting as ${to.toUpperCase()}, ${everything ? "every image except skipped" : "reviewed images only"}…`);
|
|
1340
|
+
try {
|
|
1341
|
+
const job = await this.api.exportProject(this.projectId, {
|
|
1342
|
+
to,
|
|
1343
|
+
states: everything ? ["todo", "predicted", "in_progress", "reviewed"] : ["reviewed"],
|
|
1344
|
+
});
|
|
1345
|
+
let status = job;
|
|
1346
|
+
while (!["finished", "failed", "cancelled"].includes(status.state)) {
|
|
1347
|
+
await pause(600);
|
|
1348
|
+
status = await this.api.job(job.id);
|
|
1349
|
+
}
|
|
1350
|
+
if (status.state !== "finished") {
|
|
1351
|
+
this.say(`export ${status.state}: ${status.error || "no detail"}`);
|
|
1352
|
+
return;
|
|
1353
|
+
}
|
|
1354
|
+
this.say("export ready — downloading");
|
|
1355
|
+
window.location = job.download;
|
|
1356
|
+
} catch (error) {
|
|
1357
|
+
this.say(describe(error));
|
|
1358
|
+
} finally {
|
|
1359
|
+
button.disabled = false;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
// -- heavy tools ---------------------------------------------------------
|
|
1364
|
+
|
|
1365
|
+
async runTool(tool, params, { label: labelOverride } = {}) {
|
|
1366
|
+
if (!this.image) return;
|
|
1367
|
+
const label = labelOverride === undefined ? this.currentLabel() : null;
|
|
1368
|
+
this.say(`${tool.title}…`);
|
|
1369
|
+
|
|
1370
|
+
const imageId = this.image.id;
|
|
1371
|
+
const body = {
|
|
1372
|
+
imageId,
|
|
1373
|
+
params,
|
|
1374
|
+
// A prompt tool names what it found; a gesture tool is told the class.
|
|
1375
|
+
label: labelOverride !== undefined ? labelOverride : label ? label.name : "",
|
|
1376
|
+
};
|
|
1377
|
+
let result;
|
|
1378
|
+
const started = performance.now();
|
|
1379
|
+
for (;;) {
|
|
1380
|
+
try {
|
|
1381
|
+
result = await this.api.runTool(this.projectId, tool.name, body);
|
|
1382
|
+
break;
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
// A tool that is warming up, or still working on a slow machine, is
|
|
1385
|
+
// not a failure: wait as it asks and ask again, saying so, for up to
|
|
1386
|
+
// two minutes -- the first use downloads weights. Never lose the box.
|
|
1387
|
+
const detail = error.status === 503 && error.payload ? error.payload.detail : null;
|
|
1388
|
+
if (detail && detail.retry_after_ms && performance.now() - started < 120000) {
|
|
1389
|
+
if (this.image && this.image.id !== imageId) return; // moved on
|
|
1390
|
+
const seconds = Math.round((performance.now() - started) / 1000);
|
|
1391
|
+
this.say(`${tool.title}: ${detail.message || "working"}… ${seconds}s`);
|
|
1392
|
+
await pause(Math.max(300, detail.retry_after_ms));
|
|
1393
|
+
continue;
|
|
1394
|
+
}
|
|
1395
|
+
this.say(`${describe(error)} — drawing by hand still works`);
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
if (!this.image || this.image.id !== imageId) return;
|
|
1400
|
+
|
|
1401
|
+
const made = result.shapes || [];
|
|
1402
|
+
if (!made.length) {
|
|
1403
|
+
this.say(result.detail || "the tool found nothing there");
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
// Added as one undo step, so a bad suggestion costs one Ctrl+Z.
|
|
1408
|
+
const shapes = made.map((raw) =>
|
|
1409
|
+
this.scene.clampShape({
|
|
1410
|
+
id: draftId(),
|
|
1411
|
+
label: raw.label,
|
|
1412
|
+
labelIdx: label ? label.idx : this.rail.indexFor(raw.label),
|
|
1413
|
+
bbox: raw.bbox.slice(),
|
|
1414
|
+
polygon: raw.polygon ? raw.polygon.map((p) => p.slice()) : null,
|
|
1415
|
+
kind: raw.kind || (raw.polygon ? "polygon" : "box"),
|
|
1416
|
+
origin: "model",
|
|
1417
|
+
confidence: raw.confidence ?? null,
|
|
1418
|
+
edited: false,
|
|
1419
|
+
})
|
|
1420
|
+
);
|
|
1421
|
+
for (const shape of shapes) this.scene.add(shape);
|
|
1422
|
+
this.undo.push(addMany(this.scene, shapes));
|
|
1423
|
+
this.queueSave();
|
|
1424
|
+
|
|
1425
|
+
const points = shapes[0].polygon ? shapes[0].polygon.length : 0;
|
|
1426
|
+
this.say(
|
|
1427
|
+
`${tool.title}: ${shapes.length} shape(s)` +
|
|
1428
|
+
(points ? `, ${points} points` : "") +
|
|
1429
|
+
" — drag a vertex to correct it"
|
|
1430
|
+
);
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
// "Find every handle": on this image now, or on every unreviewed image as
|
|
1434
|
+
// a job whose results land in the review queue, dashed and filterable.
|
|
1435
|
+
async runPrompt(tool, text, scope) {
|
|
1436
|
+
if (!text) {
|
|
1437
|
+
this.say("type what to find first");
|
|
1438
|
+
return;
|
|
1439
|
+
}
|
|
1440
|
+
if (scope === "image") {
|
|
1441
|
+
await this.runTool(tool, { text }, { label: "" });
|
|
1442
|
+
if (this.rail.labels.every((l) => l.name.toLowerCase() !== text.toLowerCase())) {
|
|
1443
|
+
// The class is made by the save, as always; refresh the rail after it.
|
|
1444
|
+
await this.flushSave();
|
|
1445
|
+
const payload = await this.api.labels(this.projectId);
|
|
1446
|
+
this.rail.setLabels(payload.labels);
|
|
1447
|
+
}
|
|
1448
|
+
return;
|
|
1449
|
+
}
|
|
1450
|
+
await this.flushSave();
|
|
1451
|
+
this.say(`finding "${text}" on every unreviewed image…`);
|
|
1452
|
+
try {
|
|
1453
|
+
const job = await this.api.runToolBatch(this.projectId, tool.name, { params: { text }, states: ["todo"] });
|
|
1454
|
+
let status = job;
|
|
1455
|
+
while (!["finished", "failed", "cancelled"].includes(status.state)) {
|
|
1456
|
+
await pause(800);
|
|
1457
|
+
status = await this.api.job(job.id);
|
|
1458
|
+
// progress is {done, total, message, percent}, not a string.
|
|
1459
|
+
const progress = status.progress || {};
|
|
1460
|
+
if (progress.total) this.say(`finding "${text}": ${progress.done}/${progress.total} ${progress.message || ""}`);
|
|
1461
|
+
}
|
|
1462
|
+
if (status.state !== "finished") {
|
|
1463
|
+
this.say(`search ${status.state}: ${status.error || "no detail"}`);
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1466
|
+
const data = (status.result && status.result.data) || {};
|
|
1467
|
+
await this.loadImageList();
|
|
1468
|
+
const payload = await this.api.labels(this.projectId);
|
|
1469
|
+
this.rail.setLabels(payload.labels);
|
|
1470
|
+
if (this.image) await this.openImage(this.image.id);
|
|
1471
|
+
this.updateReviewPanel(data.agreement || null);
|
|
1472
|
+
this.say(
|
|
1473
|
+
`found ${data.shapes || 0} "${text}" on ${data.predicted || 0} image(s) — ` +
|
|
1474
|
+
`they are predictions now: A accepts an image, the slider hides weak ones`
|
|
1475
|
+
);
|
|
1476
|
+
} catch (error) {
|
|
1477
|
+
this.say(describe(error));
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
async loadPredictions(file) {
|
|
1482
|
+
this.say(`reading ${file.name}…`);
|
|
1483
|
+
try {
|
|
1484
|
+
const report = await this.api.importPredictions(this.projectId, file, {
|
|
1485
|
+
minConfidence: 0,
|
|
1486
|
+
});
|
|
1487
|
+
this.rail.setLabels(report.labels || this.rail.labels);
|
|
1488
|
+
this.strip.setCounts(report.counts);
|
|
1489
|
+
await this.loadImageList();
|
|
1490
|
+
if (this.image) await this.openImage(this.image.id);
|
|
1491
|
+
this.updateReviewPanel(report.agreement);
|
|
1492
|
+
|
|
1493
|
+
// The matching report is the point. Importing zero shapes because the
|
|
1494
|
+
// filenames did not line up looks exactly like a model that predicted
|
|
1495
|
+
// nothing, so it is stated rather than left to be inferred.
|
|
1496
|
+
const parts = [
|
|
1497
|
+
`${report.matched} image(s) matched, ${report.shapes_imported} prediction(s) loaded`,
|
|
1498
|
+
];
|
|
1499
|
+
if (report.unmatched_files) parts.push(`${report.unmatched_files} file(s) matched nothing`);
|
|
1500
|
+
if (report.skipped_reviewed) parts.push(`${report.skipped_reviewed} already reviewed, skipped`);
|
|
1501
|
+
this.say(parts.join(" · "));
|
|
1502
|
+
if (report.matched === 0) {
|
|
1503
|
+
alert(
|
|
1504
|
+
"Nothing matched.\n\n" +
|
|
1505
|
+
"Predictions are matched to images by filename stem, so check the " +
|
|
1506
|
+
"names line up.\n\n" +
|
|
1507
|
+
(report.notes || []).join("\n")
|
|
1508
|
+
);
|
|
1509
|
+
}
|
|
1510
|
+
} catch (error) {
|
|
1511
|
+
this.say(describe(error));
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
setTool(name) {
|
|
1516
|
+
const tool = this.tools[name];
|
|
1517
|
+
if (!tool) return;
|
|
1518
|
+
this.cancelTool();
|
|
1519
|
+
this.tool = tool;
|
|
1520
|
+
for (const button of document.querySelectorAll("[data-tool]")) {
|
|
1521
|
+
if (button.dataset.tool === name) button.dataset.current = "true";
|
|
1522
|
+
else delete button.dataset.current;
|
|
1523
|
+
}
|
|
1524
|
+
this.canvas.style.cursor = name === "select" ? "default" : "crosshair";
|
|
1525
|
+
this.say(
|
|
1526
|
+
{
|
|
1527
|
+
select: "select tool",
|
|
1528
|
+
box: "box tool: drag",
|
|
1529
|
+
polygon: "polygon tool: click points, Enter to close",
|
|
1530
|
+
line: "line tool: click points, Enter to finish",
|
|
1531
|
+
point: "point tool: click",
|
|
1532
|
+
obb: "rotated box: drag, then turn it with the handle or [ ]",
|
|
1533
|
+
}[name] || `${name} tool`
|
|
1534
|
+
);
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
cancelTool() {
|
|
1538
|
+
for (const tool of Object.values(this.tools)) tool.cancel();
|
|
1539
|
+
this.drawingBox = false;
|
|
1540
|
+
this.scene.invalidate();
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
// -- navigation ---------------------------------------------------------
|
|
1544
|
+
|
|
1545
|
+
async step(direction) {
|
|
1546
|
+
if (!this.image) return;
|
|
1547
|
+
// With a queue chosen in the strip -- "predicted, least sure first" --
|
|
1548
|
+
// N and E walk that queue, not the folder. Reviewing hardest first only
|
|
1549
|
+
// works if moving on goes to the next hardest.
|
|
1550
|
+
if (this.strip.isQueue) {
|
|
1551
|
+
await this.flushSave();
|
|
1552
|
+
let target = null;
|
|
1553
|
+
try {
|
|
1554
|
+
target = await this.strip.neighbour(this.image.id, direction);
|
|
1555
|
+
} catch (error) {
|
|
1556
|
+
this.say(describe(error));
|
|
1557
|
+
return;
|
|
1558
|
+
}
|
|
1559
|
+
if (!target) {
|
|
1560
|
+
this.say(direction > 0 ? "that was the last one in this list" : "that was the first one in this list");
|
|
1561
|
+
return;
|
|
1562
|
+
}
|
|
1563
|
+
await this.openImage(target);
|
|
1564
|
+
return;
|
|
1565
|
+
}
|
|
1566
|
+
const target = direction > 0 ? this.image.next : this.image.previous;
|
|
1567
|
+
if (!target) {
|
|
1568
|
+
this.say(direction > 0 ? "that was the last image" : "that was the first image");
|
|
1569
|
+
return;
|
|
1570
|
+
}
|
|
1571
|
+
await this.openImage(target);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
async markReviewedAndAdvance() {
|
|
1575
|
+
if (!this.image) return;
|
|
1576
|
+
await this.markState("reviewed");
|
|
1577
|
+
await this.step(1);
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
async markState(state) {
|
|
1581
|
+
if (!this.image) return;
|
|
1582
|
+
await this.flushSave();
|
|
1583
|
+
try {
|
|
1584
|
+
await this.api.setState(this.projectId, this.image.id, state);
|
|
1585
|
+
this.strip.setState(this.image.id, state);
|
|
1586
|
+
const payload = await this.api.images(this.projectId, { limit: 1 });
|
|
1587
|
+
this.strip.setCounts(payload.counts);
|
|
1588
|
+
this.say(`marked ${state}`);
|
|
1589
|
+
} catch (error) {
|
|
1590
|
+
this.say(describe(error));
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
// Copy the previous image's shapes onto this one. For video frames this is
|
|
1595
|
+
// worth more than any model: consecutive frames differ by a few pixels.
|
|
1596
|
+
async copyPrevious() {
|
|
1597
|
+
if (!this.image || !this.image.previous) {
|
|
1598
|
+
this.say("no previous image to copy from");
|
|
1599
|
+
return;
|
|
1600
|
+
}
|
|
1601
|
+
try {
|
|
1602
|
+
const previous = await this.api.image(this.projectId, this.image.previous);
|
|
1603
|
+
if (!previous.shapes.length) {
|
|
1604
|
+
this.say("the previous image has no shapes");
|
|
1605
|
+
return;
|
|
1606
|
+
}
|
|
1607
|
+
const copies = previous.shapes.map((raw) =>
|
|
1608
|
+
this.scene.clampShape({
|
|
1609
|
+
id: draftId(),
|
|
1610
|
+
label: raw.label,
|
|
1611
|
+
labelIdx: raw.label_idx,
|
|
1612
|
+
bbox: raw.bbox.slice(),
|
|
1613
|
+
polygon: raw.polygon ? raw.polygon.map((p) => p.slice()) : null,
|
|
1614
|
+
kind: raw.kind || (raw.polygon ? "polygon" : "box"),
|
|
1615
|
+
origin: "human",
|
|
1616
|
+
confidence: null,
|
|
1617
|
+
edited: false,
|
|
1618
|
+
attributes: attributesOf(raw),
|
|
1619
|
+
})
|
|
1620
|
+
);
|
|
1621
|
+
for (const shape of copies) this.scene.add(cloneShape(shape));
|
|
1622
|
+
this.undo.push(addMany(this.scene, copies));
|
|
1623
|
+
this.queueSave();
|
|
1624
|
+
this.say(`copied ${copies.length} shape(s) from ${previous.name}`);
|
|
1625
|
+
} catch (error) {
|
|
1626
|
+
this.say(describe(error));
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
|
|
1630
|
+
say(message) {
|
|
1631
|
+
this.progress.textContent = message;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
// Mouse wheels step in large whole notches with no sideways component;
|
|
1636
|
+
// trackpads scroll in small, often fractional, two-axis deltas. A guess, but
|
|
1637
|
+
// a wrong guess costs one scroll, and always zooming made trackpads useless.
|
|
1638
|
+
function looksLikeTrackpad(event, dx, dy) {
|
|
1639
|
+
if (event.deltaMode !== 0) return false;
|
|
1640
|
+
if (dx !== 0) return true;
|
|
1641
|
+
return Math.abs(dy) < 40 && !Number.isInteger(dy);
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
// Per-browser conveniences. Storage can be missing or refuse (private
|
|
1645
|
+
// windows, blocked site data); the editor must work identically without it.
|
|
1646
|
+
function readPreference(key, fallback) {
|
|
1647
|
+
try {
|
|
1648
|
+
const raw = localStorage.getItem(`easyannotate:${key}`);
|
|
1649
|
+
return raw == null ? fallback : JSON.parse(raw);
|
|
1650
|
+
} catch {
|
|
1651
|
+
return fallback;
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
function writePreference(key, value) {
|
|
1656
|
+
try {
|
|
1657
|
+
localStorage.setItem(`easyannotate:${key}`, JSON.stringify(value));
|
|
1658
|
+
} catch {
|
|
1659
|
+
// Not persisted; still applied for this session.
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
function pause(ms) {
|
|
1664
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
function isTypingTarget(node) {
|
|
1668
|
+
if (!node) return false;
|
|
1669
|
+
const tag = node.tagName;
|
|
1670
|
+
return tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || node.isContentEditable;
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
function describe(error) {
|
|
1674
|
+
if (error instanceof ApiError && error.status === 0) {
|
|
1675
|
+
return "cannot reach the server — your work is still here, retrying will save it";
|
|
1676
|
+
}
|
|
1677
|
+
return error && error.message ? error.message : String(error);
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
// The local-link token arrives in the URL fragment when a launcher opened
|
|
1681
|
+
// this tab. Keep it for this tab only, and take it out of the address bar so
|
|
1682
|
+
// it is not bookmarked, shared or shown.
|
|
1683
|
+
(function adoptLocalToken() {
|
|
1684
|
+
const match = /(?:^|[#&])token=([^&]+)/.exec(location.hash || "");
|
|
1685
|
+
if (!match) return;
|
|
1686
|
+
try {
|
|
1687
|
+
sessionStorage.setItem("easyannotate:local-token", decodeURIComponent(match[1]));
|
|
1688
|
+
} catch {
|
|
1689
|
+
// Storage refused: the editor works the same; only folder opening needs it.
|
|
1690
|
+
}
|
|
1691
|
+
history.replaceState(null, "", location.pathname + location.search);
|
|
1692
|
+
})();
|
|
1693
|
+
|
|
1694
|
+
const editor = new Editor();
|
|
1695
|
+
editor.boot();
|
|
1696
|
+
window.__editor = editor;
|