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,106 @@
|
|
|
1
|
+
// The Assist panel: whatever model-backed tools are installed, rendered
|
|
2
|
+
// generically.
|
|
3
|
+
//
|
|
4
|
+
// Named 'assist', not 'tools', because the editor already has tools/ --
|
|
5
|
+
// select, box, polygon -- and those are what a person draws with. These
|
|
6
|
+
// are optional helpers that may not be installed at all.
|
|
7
|
+
//
|
|
8
|
+
// There is no per-tool front-end code here and there must never be. A tool
|
|
9
|
+
// ships a spec (name, title, summary, input schema) and this renders a button
|
|
10
|
+
// for it -- the same "declare once, render everywhere" the operation registry
|
|
11
|
+
// already does for the batch panel.
|
|
12
|
+
//
|
|
13
|
+
// The panel is **absent** when no tool is installed, which is the normal case.
|
|
14
|
+
// Not greyed out, not an upsell: absent. A control you cannot use is worse
|
|
15
|
+
// than no control, and a plain `pip install data-factory` is a complete,
|
|
16
|
+
// working annotator.
|
|
17
|
+
|
|
18
|
+
export class AssistPanel {
|
|
19
|
+
constructor(root, { onArm, onPrompt }) {
|
|
20
|
+
this.root = root;
|
|
21
|
+
this.list = root.querySelector('[data-role="tool-list"]');
|
|
22
|
+
this.template = root.querySelector('[data-role="tool-item"]');
|
|
23
|
+
this.promptTemplate = root.querySelector('[data-role="prompt-item"]');
|
|
24
|
+
this.onArm = onArm;
|
|
25
|
+
this.onPrompt = onPrompt || (() => {});
|
|
26
|
+
this.tools = [];
|
|
27
|
+
this.armed = null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setTools(payload) {
|
|
31
|
+
this.tools = ((payload && payload.tools) || []).filter((tool) => tool.kind !== "model");
|
|
32
|
+
this.root.hidden = this.tools.length === 0;
|
|
33
|
+
this.list.replaceChildren();
|
|
34
|
+
|
|
35
|
+
for (const tool of this.tools) {
|
|
36
|
+
// A model-file tool is command-line only (`easyannotate predict`):
|
|
37
|
+
// loading one runs it, so the browser never gets a button for it.
|
|
38
|
+
if (tool.kind === "model") continue;
|
|
39
|
+
if (tool.kind === "prompt") {
|
|
40
|
+
this.list.append(this.promptNode(tool));
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
const node = this.template.content.firstElementChild.cloneNode(true);
|
|
44
|
+
const button = node.querySelector("button");
|
|
45
|
+
button.textContent = tool.title || tool.name;
|
|
46
|
+
button.title = tool.summary || "";
|
|
47
|
+
button.dataset.tool = tool.name;
|
|
48
|
+
button.addEventListener("click", () => this.arm(tool));
|
|
49
|
+
this.list.append(node);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A prompt tool takes words, not a gesture: "find every handle". It runs on
|
|
54
|
+
// this image, or on every unreviewed image as a job -- which is where typing
|
|
55
|
+
// a class name turns into a review queue instead of a drawing session.
|
|
56
|
+
promptNode(tool) {
|
|
57
|
+
const node = this.promptTemplate.content.firstElementChild.cloneNode(true);
|
|
58
|
+
const text = node.querySelector('[data-role="prompt-text"]');
|
|
59
|
+
const here = node.querySelector('[data-role="prompt-image"]');
|
|
60
|
+
const all = node.querySelector('[data-role="prompt-all"]');
|
|
61
|
+
node.querySelector('[data-role="prompt-title"]').textContent = tool.title || tool.name;
|
|
62
|
+
text.title = tool.summary || "";
|
|
63
|
+
text.addEventListener("keydown", (event) => {
|
|
64
|
+
if (event.key === "Enter") {
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
this.onPrompt(tool, text.value.trim(), "image");
|
|
67
|
+
}
|
|
68
|
+
if (event.key === "Escape") text.blur();
|
|
69
|
+
event.stopPropagation();
|
|
70
|
+
});
|
|
71
|
+
here.addEventListener("click", () => this.onPrompt(tool, text.value.trim(), "image"));
|
|
72
|
+
all.addEventListener("click", () => this.onPrompt(tool, text.value.trim(), "all"));
|
|
73
|
+
return node;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Arming is a toggle, not a mode you can get stuck in: clicking the armed
|
|
77
|
+
// tool again disarms it, and Escape does too.
|
|
78
|
+
arm(tool) {
|
|
79
|
+
this.armed = this.armed && this.armed.name === tool.name ? null : tool;
|
|
80
|
+
for (const button of this.list.querySelectorAll("button")) {
|
|
81
|
+
if (this.armed && button.dataset.tool === this.armed.name) {
|
|
82
|
+
button.dataset.current = "true";
|
|
83
|
+
} else {
|
|
84
|
+
delete button.dataset.current;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
this.onArm(this.armed);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
disarm() {
|
|
91
|
+
if (this.armed) this.arm(this.armed);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Whether this tool wants a box dragged out before it runs. Read from the
|
|
95
|
+
// declared input schema rather than the tool's name, so a tool nobody here
|
|
96
|
+
// has heard of still works.
|
|
97
|
+
wantsBox(tool) {
|
|
98
|
+
const properties = (tool && tool.inputs && tool.inputs.properties) || {};
|
|
99
|
+
return Object.prototype.hasOwnProperty.call(properties, "box");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
wantsPoints(tool) {
|
|
103
|
+
const properties = (tool && tool.inputs && tool.inputs.properties) || {};
|
|
104
|
+
return Object.prototype.hasOwnProperty.call(properties, "points");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// The image list down the side: where you are, and how much is left.
|
|
2
|
+
//
|
|
3
|
+
// Virtual, because projects are large. A spacer as tall as every row would be
|
|
4
|
+
// gives the scrollbar its true length; only the rows on screen exist in the
|
|
5
|
+
// DOM, and their data arrives a page at a time as you scroll. The first
|
|
6
|
+
// version built a node per image and stopped at 2,000 -- the door set alone
|
|
7
|
+
// has 15,514.
|
|
8
|
+
//
|
|
9
|
+
// State is shown as a colour bar rather than an icon, because the useful
|
|
10
|
+
// question is "how much of this strip is green", answered by glancing rather
|
|
11
|
+
// than reading.
|
|
12
|
+
|
|
13
|
+
export const STATE_COLOURS = {
|
|
14
|
+
todo: "#3a3a3a",
|
|
15
|
+
predicted: "#b08900",
|
|
16
|
+
in_progress: "#3b7dd8",
|
|
17
|
+
reviewed: "#3cb44b",
|
|
18
|
+
skipped: "#8a4b8a",
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const ROW_HEIGHT = 41;
|
|
22
|
+
const PAGE = 200;
|
|
23
|
+
const OVERSCAN = 8;
|
|
24
|
+
// Rows kept built while off screen, so scrolling back does not refetch or
|
|
25
|
+
// re-decode thumbnails. Bounded: an unbounded cache is a slow leak.
|
|
26
|
+
const NODE_CACHE = 400;
|
|
27
|
+
|
|
28
|
+
export class Filmstrip {
|
|
29
|
+
constructor(root, { onOpen, thumbUrl, fetchPage }) {
|
|
30
|
+
this.root = root;
|
|
31
|
+
this.list = root.querySelector('[data-role="strip-list"]');
|
|
32
|
+
this.template = root.querySelector('[data-role="strip-item"]');
|
|
33
|
+
this.counts = root.querySelector('[data-role="strip-counts"]');
|
|
34
|
+
this.filterBox = root.querySelector('[data-role="strip-filter"]');
|
|
35
|
+
this.onOpen = onOpen;
|
|
36
|
+
this.thumbUrl = thumbUrl;
|
|
37
|
+
this.fetchPage = fetchPage;
|
|
38
|
+
|
|
39
|
+
// The filter box holds "state" or "state:order". Kept apart here because
|
|
40
|
+
// counts are per state, while the order only changes what comes first.
|
|
41
|
+
this.filter = "";
|
|
42
|
+
this.order = "";
|
|
43
|
+
this.total = 0;
|
|
44
|
+
this.lastCounts = null;
|
|
45
|
+
this.pages = new Map(); // page number -> images[] (or a pending promise)
|
|
46
|
+
this.nodes = new Map(); // image id -> row node, LRU by insertion order
|
|
47
|
+
this.currentId = null;
|
|
48
|
+
this.generation = 0;
|
|
49
|
+
|
|
50
|
+
this.spacer = document.createElement("div");
|
|
51
|
+
this.spacer.style.position = "relative";
|
|
52
|
+
this.list.replaceChildren(this.spacer);
|
|
53
|
+
|
|
54
|
+
let queued = false;
|
|
55
|
+
this.list.addEventListener("scroll", () => {
|
|
56
|
+
if (queued) return;
|
|
57
|
+
queued = true;
|
|
58
|
+
requestAnimationFrame(() => {
|
|
59
|
+
queued = false;
|
|
60
|
+
this.draw();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
new ResizeObserver(() => this.draw()).observe(this.list);
|
|
64
|
+
|
|
65
|
+
if (this.filterBox) {
|
|
66
|
+
this.filterBox.addEventListener("change", () => {
|
|
67
|
+
this.setFilter(this.filterBox.value);
|
|
68
|
+
this.filterBox.blur();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// -- data ---------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
// Forget every loaded page and start over -- after an import, or when the
|
|
76
|
+
// filter changes what the list contains.
|
|
77
|
+
reset(counts) {
|
|
78
|
+
this.generation += 1;
|
|
79
|
+
this.pages.clear();
|
|
80
|
+
if (counts) this.setCounts(counts);
|
|
81
|
+
this.draw();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
setFilter(value) {
|
|
85
|
+
const [state, order] = String(value || "").split(":");
|
|
86
|
+
this.filter = state || "";
|
|
87
|
+
this.order = order || "";
|
|
88
|
+
this.list.scrollTop = 0;
|
|
89
|
+
this.reset(this.lastCounts);
|
|
90
|
+
if (this.currentId) this.reveal(this.currentId);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
imageAt(index) {
|
|
94
|
+
const page = this.pages.get(Math.floor(index / PAGE));
|
|
95
|
+
return Array.isArray(page) ? page[index % PAGE] || null : null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
ensurePage(number) {
|
|
99
|
+
if (this.pages.has(number) || number * PAGE >= this.total) return;
|
|
100
|
+
const generation = this.generation;
|
|
101
|
+
const pending = this.fetchPage({ state: this.filter, order: this.order, offset: number * PAGE, limit: PAGE })
|
|
102
|
+
.then((payload) => {
|
|
103
|
+
// A filter change or reset while this was in flight makes it stale.
|
|
104
|
+
if (generation !== this.generation) return;
|
|
105
|
+
this.pages.set(number, payload.images);
|
|
106
|
+
this.draw();
|
|
107
|
+
})
|
|
108
|
+
.catch(() => {
|
|
109
|
+
if (generation === this.generation) this.pages.delete(number);
|
|
110
|
+
});
|
|
111
|
+
this.pages.set(number, pending);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// -- drawing ------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
draw() {
|
|
117
|
+
this.spacer.style.height = `${this.total * ROW_HEIGHT}px`;
|
|
118
|
+
const top = this.list.scrollTop;
|
|
119
|
+
const height = this.list.clientHeight || 600;
|
|
120
|
+
const first = Math.max(0, Math.floor(top / ROW_HEIGHT) - OVERSCAN);
|
|
121
|
+
const last = Math.min(this.total - 1, Math.ceil((top + height) / ROW_HEIGHT) + OVERSCAN);
|
|
122
|
+
|
|
123
|
+
const wanted = [];
|
|
124
|
+
for (let index = first; index <= last; index++) {
|
|
125
|
+
this.ensurePage(Math.floor(index / PAGE));
|
|
126
|
+
const image = this.imageAt(index);
|
|
127
|
+
if (image) wanted.push([index, image]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const keep = new Set();
|
|
131
|
+
for (const [index, image] of wanted) {
|
|
132
|
+
const node = this.nodeFor(image);
|
|
133
|
+
node.style.top = `${index * ROW_HEIGHT}px`;
|
|
134
|
+
keep.add(node);
|
|
135
|
+
if (node.parentNode !== this.spacer) this.spacer.append(node);
|
|
136
|
+
}
|
|
137
|
+
for (const child of [...this.spacer.children]) {
|
|
138
|
+
if (!keep.has(child)) child.remove();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
nodeFor(image) {
|
|
143
|
+
let node = this.nodes.get(image.id);
|
|
144
|
+
if (node) {
|
|
145
|
+
// Refresh LRU position, and pick up a state that changed meanwhile.
|
|
146
|
+
this.nodes.delete(image.id);
|
|
147
|
+
this.nodes.set(image.id, node);
|
|
148
|
+
this.paintState(node, image.state);
|
|
149
|
+
return node;
|
|
150
|
+
}
|
|
151
|
+
node = this.template.content.firstElementChild.cloneNode(true);
|
|
152
|
+
const img = node.querySelector("img");
|
|
153
|
+
const name = node.querySelector('[data-role="strip-name"]');
|
|
154
|
+
|
|
155
|
+
img.src = this.thumbUrl(image.id);
|
|
156
|
+
img.alt = image.name;
|
|
157
|
+
// A broken thumbnail is a normal finding in a real dataset -- a corrupt
|
|
158
|
+
// file is one of the things inspect.health looks for -- so it must not
|
|
159
|
+
// leave a browser's broken-image glyph implying the tool failed.
|
|
160
|
+
img.addEventListener("error", () => {
|
|
161
|
+
img.remove();
|
|
162
|
+
node.dataset.broken = "true";
|
|
163
|
+
});
|
|
164
|
+
name.textContent = image.name;
|
|
165
|
+
name.title = image.path;
|
|
166
|
+
node.dataset.id = image.id;
|
|
167
|
+
node.style.position = "absolute";
|
|
168
|
+
node.style.left = "0";
|
|
169
|
+
node.style.right = "0";
|
|
170
|
+
this.paintState(node, image.state);
|
|
171
|
+
if (image.id === this.currentId) node.dataset.current = "true";
|
|
172
|
+
node.addEventListener("click", () => this.onOpen(image.id));
|
|
173
|
+
|
|
174
|
+
this.nodes.set(image.id, node);
|
|
175
|
+
while (this.nodes.size > NODE_CACHE) {
|
|
176
|
+
const [oldest, stale] = this.nodes.entries().next().value;
|
|
177
|
+
if (stale.isConnected) break; // never evict a row on screen
|
|
178
|
+
this.nodes.delete(oldest);
|
|
179
|
+
}
|
|
180
|
+
return node;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
paintState(node, state) {
|
|
184
|
+
node.dataset.state = state;
|
|
185
|
+
const bar = node.querySelector('[data-role="strip-state"]');
|
|
186
|
+
if (bar) bar.style.background = STATE_COLOURS[state] || STATE_COLOURS.todo;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// -- the open image ---------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
setCurrent(imageId, position) {
|
|
192
|
+
const previous = this.nodes.get(this.currentId);
|
|
193
|
+
if (previous) delete previous.dataset.current;
|
|
194
|
+
this.currentId = imageId;
|
|
195
|
+
const node = this.nodes.get(imageId);
|
|
196
|
+
if (node) node.dataset.current = "true";
|
|
197
|
+
if (position != null) this.scrollToIndex(position);
|
|
198
|
+
else this.reveal(imageId);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Scroll so the open image is on screen, asking the server where it is when
|
|
202
|
+
// it is not in a page already loaded.
|
|
203
|
+
async reveal(imageId) {
|
|
204
|
+
for (const [number, page] of this.pages) {
|
|
205
|
+
if (!Array.isArray(page)) continue;
|
|
206
|
+
const at = page.findIndex((image) => image.id === imageId);
|
|
207
|
+
if (at >= 0) {
|
|
208
|
+
this.lastPosition = number * PAGE + at;
|
|
209
|
+
this.scrollToIndex(number * PAGE + at);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const payload = await this.fetchPage({ state: this.filter, order: this.order, offset: 0, limit: 1, locate: imageId });
|
|
215
|
+
if (payload.position != null && imageId === this.currentId) {
|
|
216
|
+
this.lastPosition = payload.position;
|
|
217
|
+
this.scrollToIndex(payload.position);
|
|
218
|
+
}
|
|
219
|
+
} catch {
|
|
220
|
+
// Not knowing where it is only costs the scroll.
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
scrollToIndex(index) {
|
|
225
|
+
const top = index * ROW_HEIGHT;
|
|
226
|
+
const view = this.list.clientHeight || 600;
|
|
227
|
+
if (top < this.list.scrollTop || top + ROW_HEIGHT > this.list.scrollTop + view) {
|
|
228
|
+
this.list.scrollTop = Math.max(0, top - view / 2 + ROW_HEIGHT / 2);
|
|
229
|
+
}
|
|
230
|
+
this.draw();
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Whether navigation should follow this list rather than file order: any
|
|
234
|
+
// filter or ordering means the person chose a queue to work through.
|
|
235
|
+
get isQueue() {
|
|
236
|
+
return Boolean(this.filter || this.order);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// The image beside `imageId` in this queue. When the current image has
|
|
240
|
+
// just left it -- accepted, so no longer "predicted" -- the one that slid
|
|
241
|
+
// into its place is next, so `lastPosition` remembers where it was.
|
|
242
|
+
async neighbour(imageId, direction) {
|
|
243
|
+
const query = { state: this.filter, order: this.order };
|
|
244
|
+
const located = await this.fetchPage({ ...query, offset: 0, limit: 1, locate: imageId });
|
|
245
|
+
let offset;
|
|
246
|
+
if (located.position != null) {
|
|
247
|
+
this.lastPosition = located.position;
|
|
248
|
+
offset = located.position + direction;
|
|
249
|
+
} else {
|
|
250
|
+
offset = (this.lastPosition ?? 0) + (direction > 0 ? 0 : -1);
|
|
251
|
+
}
|
|
252
|
+
if (offset < 0) return null;
|
|
253
|
+
const page = await this.fetchPage({ ...query, offset, limit: 1 });
|
|
254
|
+
return page.images && page.images.length ? page.images[0].id : null;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
setState(imageId, state) {
|
|
258
|
+
for (const page of this.pages.values()) {
|
|
259
|
+
if (!Array.isArray(page)) continue;
|
|
260
|
+
const image = page.find((each) => each.id === imageId);
|
|
261
|
+
if (image) image.state = state;
|
|
262
|
+
}
|
|
263
|
+
const node = this.nodes.get(imageId);
|
|
264
|
+
if (node) this.paintState(node, state);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
setCounts(counts) {
|
|
268
|
+
if (!counts) return;
|
|
269
|
+
this.lastCounts = counts;
|
|
270
|
+
const total = this.filter ? counts[this.filter] || 0 : counts.total || 0;
|
|
271
|
+
if (total !== this.total) {
|
|
272
|
+
// A different length means rows moved -- an image left this filter.
|
|
273
|
+
// Pages already loaded are no longer where they claim to be.
|
|
274
|
+
if (this.filter) {
|
|
275
|
+
this.generation += 1;
|
|
276
|
+
this.pages.clear();
|
|
277
|
+
}
|
|
278
|
+
this.total = total;
|
|
279
|
+
}
|
|
280
|
+
const done = counts.reviewed || 0;
|
|
281
|
+
const all = counts.total || 0;
|
|
282
|
+
const percent = all ? Math.round((done / all) * 100) : 0;
|
|
283
|
+
this.counts.textContent = `${done} / ${all} reviewed (${percent}%)`;
|
|
284
|
+
this.draw();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// What is under the cursor.
|
|
2
|
+
//
|
|
3
|
+
// A linear scan, deliberately. Phase 0 measured hit-testing at 0.0ms median
|
|
4
|
+
// and still 0.4ms at 10,000 shapes on one image, against a realistic load of
|
|
5
|
+
// tens. A spatial index here would be complexity bought with no return.
|
|
6
|
+
//
|
|
7
|
+
// Priority is what makes it feel right: the selected shape's handles first,
|
|
8
|
+
// then its vertices, then shape bodies topmost-first. Without that ordering a
|
|
9
|
+
// resize handle sitting over another shape becomes unreachable.
|
|
10
|
+
|
|
11
|
+
//: Grab radius in *screen* pixels, converted at the current zoom, so a handle
|
|
12
|
+
//: is the same physical size whether you are at 5% or 800%.
|
|
13
|
+
export const TOLERANCE = 10;
|
|
14
|
+
|
|
15
|
+
//: How far the rotation handle sits beyond the top edge, in screen pixels.
|
|
16
|
+
export const ROTATE_STEM = 28;
|
|
17
|
+
|
|
18
|
+
const kindOf = (shape) =>
|
|
19
|
+
shape.kind || (shape.polygon && shape.polygon.length >= 3 ? "polygon" : "box");
|
|
20
|
+
|
|
21
|
+
// Where the rotation handle of a rotated box is: out from the centre through
|
|
22
|
+
// the midpoint of its first edge, so it turns with the box.
|
|
23
|
+
export function rotationHandle(shape, scale) {
|
|
24
|
+
const p = shape.polygon;
|
|
25
|
+
if (!p || p.length !== 4) return null;
|
|
26
|
+
const cx = (p[0][0] + p[1][0] + p[2][0] + p[3][0]) / 4;
|
|
27
|
+
const cy = (p[0][1] + p[1][1] + p[2][1] + p[3][1]) / 4;
|
|
28
|
+
const mx = (p[0][0] + p[1][0]) / 2;
|
|
29
|
+
const my = (p[0][1] + p[1][1]) / 2;
|
|
30
|
+
const len = Math.hypot(mx - cx, my - cy) || 1;
|
|
31
|
+
const stem = ROTATE_STEM / scale;
|
|
32
|
+
return [mx + ((mx - cx) / len) * stem, my + ((my - cy) / len) * stem];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function handlesOf(shape) {
|
|
36
|
+
const [x1, y1, x2, y2] = shape.bbox;
|
|
37
|
+
const mx = (x1 + x2) / 2;
|
|
38
|
+
const my = (y1 + y2) / 2;
|
|
39
|
+
return [
|
|
40
|
+
[x1, y1], [x2, y1], [x2, y2], [x1, y2],
|
|
41
|
+
[mx, y1], [mx, y2], [x1, my], [x2, my],
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function hitTest(scene, ix, iy, scale, visible = null) {
|
|
46
|
+
const tol = TOLERANCE / scale;
|
|
47
|
+
// A shape the confidence slider is hiding must not be grabbable. Dragging
|
|
48
|
+
// something you cannot see is indistinguishable from the tool being broken.
|
|
49
|
+
const shown = visible || (() => true);
|
|
50
|
+
|
|
51
|
+
for (const id of scene.selection) {
|
|
52
|
+
const shape = scene.shapes.get(id);
|
|
53
|
+
if (!shape || !shown(shape)) continue;
|
|
54
|
+
const kind = kindOf(shape);
|
|
55
|
+
|
|
56
|
+
if (kind === "obb") {
|
|
57
|
+
const handle = rotationHandle(shape, scale);
|
|
58
|
+
if (handle && near(ix, iy, handle, tol)) return { id, part: "rotate" };
|
|
59
|
+
}
|
|
60
|
+
// Box handles are axis-aligned, which is wrong for everything but a box
|
|
61
|
+
// or a polygon's bounding frame.
|
|
62
|
+
if (kind === "box" || kind === "polygon") {
|
|
63
|
+
const handles = handlesOf(shape);
|
|
64
|
+
for (let h = 0; h < handles.length; h++) {
|
|
65
|
+
if (near(ix, iy, handles[h], tol)) return { id, part: `handle-${h}` };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (shape.polygon) {
|
|
70
|
+
for (let v = 0; v < shape.polygon.length; v++) {
|
|
71
|
+
if (near(ix, iy, shape.polygon[v], tol)) return { id, part: `vertex-${v}` };
|
|
72
|
+
}
|
|
73
|
+
// Edge midpoints insert a vertex. Checked after real vertices so a
|
|
74
|
+
// three-point polygon, where midpoints crowd the corners, still lets you
|
|
75
|
+
// grab the corner. A rotated box keeps exactly four.
|
|
76
|
+
if (kind !== "obb") {
|
|
77
|
+
const edge = nearestEdge(shape.polygon, ix, iy, tol, kind === "line");
|
|
78
|
+
if (edge >= 0) return { id, part: `edge-${edge}` };
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
for (let i = scene.order.length - 1; i >= 0; i--) {
|
|
84
|
+
const shape = scene.shapes.get(scene.order[i]);
|
|
85
|
+
if (!shape || !shown(shape)) continue;
|
|
86
|
+
if (hitsBody(shape, ix, iy, tol)) return { id: shape.id, part: "body" };
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function hitsBody(shape, ix, iy, tol) {
|
|
92
|
+
const kind = kindOf(shape);
|
|
93
|
+
if (kind === "point") return near(ix, iy, shape.bbox, tol * 1.5);
|
|
94
|
+
if (kind === "line") {
|
|
95
|
+
const p = shape.polygon || [];
|
|
96
|
+
for (let i = 0; i + 1 < p.length; i++) {
|
|
97
|
+
if (distanceToSegment(ix, iy, p[i], p[i + 1]) <= tol) return true;
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (shape.polygon && shape.polygon.length >= 3) return pointInPolygon(ix, iy, shape.polygon);
|
|
102
|
+
return inBox(ix, iy, shape.bbox);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function distanceToSegment(x, y, [ax, ay], [bx, by]) {
|
|
106
|
+
const dx = bx - ax;
|
|
107
|
+
const dy = by - ay;
|
|
108
|
+
const lengthSq = dx * dx + dy * dy || 1;
|
|
109
|
+
const t = Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / lengthSq));
|
|
110
|
+
return Math.hypot(x - (ax + t * dx), y - (ay + t * dy));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function near(x, y, [px, py], tol) {
|
|
114
|
+
return Math.abs(x - px) <= tol && Math.abs(y - py) <= tol;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function inBox(x, y, [x1, y1, x2, y2]) {
|
|
118
|
+
return x >= x1 && x <= x2 && y >= y1 && y <= y2;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Even-odd ray casting. Handles concave outlines, which matters: a door frame
|
|
122
|
+
// traced around its handle is concave, and a convex test would claim the hole
|
|
123
|
+
// is inside the shape.
|
|
124
|
+
export function pointInPolygon(x, y, points) {
|
|
125
|
+
let inside = false;
|
|
126
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
|
|
127
|
+
const [xi, yi] = points[i];
|
|
128
|
+
const [xj, yj] = points[j];
|
|
129
|
+
if (yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) {
|
|
130
|
+
inside = !inside;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return inside;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Index of the edge whose midpoint is within tolerance, or -1. An open
|
|
137
|
+
// polyline has no edge from its last point back to its first.
|
|
138
|
+
function nearestEdge(points, x, y, tol, open = false) {
|
|
139
|
+
const count = open ? points.length - 1 : points.length;
|
|
140
|
+
for (let i = 0; i < count; i++) {
|
|
141
|
+
const [ax, ay] = points[i];
|
|
142
|
+
const [bx, by] = points[(i + 1) % points.length];
|
|
143
|
+
if (near(x, y, [(ax + bx) / 2, (ay + by) / 2], tol)) return i;
|
|
144
|
+
}
|
|
145
|
+
return -1;
|
|
146
|
+
}
|