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.
Files changed (109) hide show
  1. data_factory/__init__.py +82 -0
  2. data_factory/__main__.py +6 -0
  3. data_factory/annotate/__init__.py +15 -0
  4. data_factory/annotate/attach.py +398 -0
  5. data_factory/annotate/demo.py +161 -0
  6. data_factory/annotate/images.py +443 -0
  7. data_factory/annotate/labels.py +399 -0
  8. data_factory/annotate/loop.py +359 -0
  9. data_factory/annotate/projects.py +207 -0
  10. data_factory/annotate/review.py +471 -0
  11. data_factory/annotate/schema.sql +171 -0
  12. data_factory/annotate/shapes.py +488 -0
  13. data_factory/annotate/store.py +292 -0
  14. data_factory/annotate/tools/__init__.py +39 -0
  15. data_factory/annotate/tools/fake.py +170 -0
  16. data_factory/annotate/tools/install.py +254 -0
  17. data_factory/annotate/tools/local.py +389 -0
  18. data_factory/annotate/tools/postprocess.py +175 -0
  19. data_factory/annotate/tools/protocol.py +238 -0
  20. data_factory/annotate/tools/registry.py +196 -0
  21. data_factory/annotate/tools/results.py +83 -0
  22. data_factory/annotate/tools/serve.py +106 -0
  23. data_factory/cli/__init__.py +21 -0
  24. data_factory/cli/main.py +837 -0
  25. data_factory/cli/menu.py +173 -0
  26. data_factory/cli/render.py +158 -0
  27. data_factory/core/__init__.py +11 -0
  28. data_factory/core/annotations/__init__.py +5 -0
  29. data_factory/core/annotations/coco.py +394 -0
  30. data_factory/core/annotations/csvfmt.py +174 -0
  31. data_factory/core/annotations/dota.py +98 -0
  32. data_factory/core/annotations/formats.py +626 -0
  33. data_factory/core/annotations/labelme.py +153 -0
  34. data_factory/core/annotations/model.py +334 -0
  35. data_factory/core/annotations/voc.py +231 -0
  36. data_factory/core/annotations/yolo.py +283 -0
  37. data_factory/core/bktree.py +89 -0
  38. data_factory/core/fs.py +245 -0
  39. data_factory/core/imaging.py +626 -0
  40. data_factory/core/ops/__init__.py +44 -0
  41. data_factory/core/ops/analyze.py +383 -0
  42. data_factory/core/ops/annotate_export.py +326 -0
  43. data_factory/core/ops/audit.py +274 -0
  44. data_factory/core/ops/augment.py +454 -0
  45. data_factory/core/ops/collect.py +241 -0
  46. data_factory/core/ops/combine.py +582 -0
  47. data_factory/core/ops/convert.py +212 -0
  48. data_factory/core/ops/dataset.py +246 -0
  49. data_factory/core/ops/dedup.py +235 -0
  50. data_factory/core/ops/export.py +168 -0
  51. data_factory/core/ops/image.py +407 -0
  52. data_factory/core/ops/labels.py +215 -0
  53. data_factory/core/ops/pairing.py +133 -0
  54. data_factory/core/ops/rename.py +154 -0
  55. data_factory/core/ops/report.py +323 -0
  56. data_factory/core/ops/visualize.py +164 -0
  57. data_factory/core/pairs.py +144 -0
  58. data_factory/core/parallel.py +191 -0
  59. data_factory/core/provenance.py +136 -0
  60. data_factory/core/registry.py +339 -0
  61. data_factory/core/stats.py +403 -0
  62. data_factory/core/types.py +148 -0
  63. data_factory/core/writer.py +194 -0
  64. data_factory/desktop.py +207 -0
  65. data_factory/hardware.py +243 -0
  66. data_factory/pipeline.py +192 -0
  67. data_factory/py.typed +0 -0
  68. data_factory/service/__init__.py +26 -0
  69. data_factory/service/app.py +483 -0
  70. data_factory/service/binding.py +174 -0
  71. data_factory/service/deps.py +133 -0
  72. data_factory/service/jobs.py +177 -0
  73. data_factory/service/limits.py +71 -0
  74. data_factory/service/routers/__init__.py +11 -0
  75. data_factory/service/routers/annotate.py +582 -0
  76. data_factory/service/routers/local.py +464 -0
  77. data_factory/service/routers/media.py +154 -0
  78. data_factory/service/routers/predictions.py +116 -0
  79. data_factory/service/routers/promote.py +182 -0
  80. data_factory/service/routers/tools.py +308 -0
  81. data_factory/service/static/annotate/api.js +246 -0
  82. data_factory/service/static/annotate/app.js +1696 -0
  83. data_factory/service/static/annotate/assist.js +106 -0
  84. data_factory/service/static/annotate/filmstrip.js +286 -0
  85. data_factory/service/static/annotate/hit.js +146 -0
  86. data_factory/service/static/annotate/labels.js +197 -0
  87. data_factory/service/static/annotate/render.js +286 -0
  88. data_factory/service/static/annotate/scene.js +277 -0
  89. data_factory/service/static/annotate/shapelist.js +72 -0
  90. data_factory/service/static/annotate/tools/box.js +96 -0
  91. data_factory/service/static/annotate/tools/line.js +96 -0
  92. data_factory/service/static/annotate/tools/obb.js +88 -0
  93. data_factory/service/static/annotate/tools/point.js +49 -0
  94. data_factory/service/static/annotate/tools/polygon.js +110 -0
  95. data_factory/service/static/annotate/tools/select.js +227 -0
  96. data_factory/service/static/annotate/undo.js +176 -0
  97. data_factory/service/static/annotate/viewport.js +156 -0
  98. data_factory/service/static/annotate.html +390 -0
  99. data_factory/service/static/index.html +632 -0
  100. data_factory/service/workspace.py +244 -0
  101. easyannotate/__init__.py +64 -0
  102. easyannotate/__main__.py +5 -0
  103. easyannotate/project.py +181 -0
  104. easyannotate/py.typed +0 -0
  105. easyannotate-0.4.0.dist-info/METADATA +767 -0
  106. easyannotate-0.4.0.dist-info/RECORD +109 -0
  107. easyannotate-0.4.0.dist-info/WHEEL +4 -0
  108. easyannotate-0.4.0.dist-info/entry_points.txt +4 -0
  109. easyannotate-0.4.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,277 @@
1
+ // The retained scene: what is on this image, what is selected, what changed.
2
+ //
3
+ // A canvas is never diffed, so there is no virtual DOM here and no framework.
4
+ // What there is instead is one authoritative model that the renderer reads and
5
+ // the tools mutate, plus a dirty flag so we only redraw when something moved.
6
+ //
7
+ // Shapes keep their server id. That is what makes a save a diff rather than a
8
+ // delete-and-recreate, which in turn is what lets a model shape remember that
9
+ // it came from a model after somebody nudges it.
10
+
11
+ let temporary = 0;
12
+
13
+ // Client-side ids for shapes the server has never seen. Deliberately not in
14
+ // the server's id format, so `save` can tell "new" from "existing" without a
15
+ // separate flag that could drift out of step.
16
+ export function draftId() {
17
+ temporary += 1;
18
+ return `draft-${temporary}`;
19
+ }
20
+
21
+ export const isDraft = (id) => typeof id === "string" && id.startsWith("draft-");
22
+
23
+ export class Scene {
24
+ constructor() {
25
+ this.shapes = new Map();
26
+ this.order = [];
27
+ this.selection = new Set();
28
+ this.hover = null;
29
+ this.dirty = true;
30
+ this.changed = false;
31
+ // Bumped on every edit. A save compares it before and after the round
32
+ // trip: if it moved, the user kept working while the request was in
33
+ // flight, and "saved" would be a lie.
34
+ this.revision = 0;
35
+ // old id -> new id, for every shape the server renamed. Undo commands
36
+ // hold the id a shape had when they were recorded; without this, the
37
+ // first autosave after a drag would turn Ctrl+Z into a silent no-op.
38
+ this.aliases = new Map();
39
+ this.imageId = null;
40
+ this.version = 0;
41
+ this.width = 0;
42
+ this.height = 0;
43
+ }
44
+
45
+ invalidate() {
46
+ this.dirty = true;
47
+ }
48
+
49
+ // Mark the model as differing from what the server holds. Separate from
50
+ // `dirty`, which is only about pixels: panning is dirty but not changed, and
51
+ // autosaving on a pan would be absurd.
52
+ touch() {
53
+ this.revision += 1;
54
+ this.changed = true;
55
+ this.dirty = true;
56
+ }
57
+
58
+ load(payload) {
59
+ this.shapes.clear();
60
+ this.order = [];
61
+ this.selection.clear();
62
+ this.hover = null;
63
+ this.aliases.clear();
64
+ this.imageId = payload.id;
65
+ this.version = payload.version;
66
+ this.width = payload.width;
67
+ this.height = payload.height;
68
+ this.changed = false;
69
+
70
+ for (const raw of payload.shapes || []) {
71
+ const shape = {
72
+ id: raw.id,
73
+ label: raw.label,
74
+ labelIdx: raw.label_idx,
75
+ bbox: raw.bbox.slice(),
76
+ polygon: raw.polygon ? raw.polygon.map((p) => p.slice()) : null,
77
+ origin: raw.origin || "human",
78
+ confidence: raw.confidence,
79
+ edited: !!raw.edited,
80
+ kind: kindOf(raw),
81
+ // Carried through untouched so a save cannot reset them. VOC's
82
+ // difficult flag changes how a detector is scored; losing it to an
83
+ // unrelated box nudge is damage nobody sees until evaluation.
84
+ attributes: attributesOf(raw),
85
+ };
86
+ this.shapes.set(shape.id, shape);
87
+ this.order.push(shape.id);
88
+ }
89
+ this.dirty = true;
90
+ }
91
+
92
+ add(shape) {
93
+ if (!shape.id) shape.id = draftId();
94
+ this.shapes.set(shape.id, shape);
95
+ this.order.push(shape.id);
96
+ this.touch();
97
+ return shape;
98
+ }
99
+
100
+ // The current id of a shape known by any id it has ever had. A live id wins
101
+ // over an alias, so a shape re-inserted under its old id is still found.
102
+ resolve(id) {
103
+ let current = id;
104
+ for (let hops = 0; hops < 16 && !this.shapes.has(current); hops++) {
105
+ const next = this.aliases.get(current);
106
+ if (next === undefined) break;
107
+ current = next;
108
+ }
109
+ return current;
110
+ }
111
+
112
+ has(id) {
113
+ return this.shapes.has(this.resolve(id));
114
+ }
115
+
116
+ remove(id) {
117
+ id = this.resolve(id);
118
+ if (!this.shapes.has(id)) return null;
119
+ const shape = this.shapes.get(id);
120
+ this.shapes.delete(id);
121
+ this.order = this.order.filter((each) => each !== id);
122
+ this.selection.delete(id);
123
+ if (this.hover === id) this.hover = null;
124
+ this.touch();
125
+ return shape;
126
+ }
127
+
128
+ // Re-insert at a known position. Undo needs this: putting a deleted shape
129
+ // back on top of the stack would change what the user sees, so it goes back
130
+ // exactly where it was.
131
+ insertAt(shape, index) {
132
+ this.shapes.set(shape.id, shape);
133
+ this.order.splice(Math.max(0, Math.min(index, this.order.length)), 0, shape.id);
134
+ this.touch();
135
+ return shape;
136
+ }
137
+
138
+ indexOf(id) {
139
+ return this.order.indexOf(this.resolve(id));
140
+ }
141
+
142
+ // Give a shape the id the server assigned it, keeping its place, its
143
+ // selection and everything pointing at it. Called after a save, to turn
144
+ // drafts into real shapes -- otherwise the next save re-sends them with no
145
+ // id and they insert all over again.
146
+ reid(oldId, newId) {
147
+ const shape = this.shapes.get(oldId);
148
+ if (!shape || oldId === newId) return;
149
+ this.aliases.set(oldId, newId);
150
+ shape.id = newId;
151
+ this.shapes.delete(oldId);
152
+ this.shapes.set(newId, shape);
153
+ const index = this.order.indexOf(oldId);
154
+ if (index >= 0) this.order[index] = newId;
155
+ if (this.selection.delete(oldId)) this.selection.add(newId);
156
+ if (this.hover === oldId) this.hover = newId;
157
+ }
158
+
159
+ get(id) {
160
+ return this.shapes.get(this.resolve(id));
161
+ }
162
+
163
+ select(id, { additive = false } = {}) {
164
+ if (!additive) this.selection.clear();
165
+ if (id) this.selection.add(id);
166
+ this.dirty = true;
167
+ }
168
+
169
+ clearSelection() {
170
+ if (this.selection.size === 0) return;
171
+ this.selection.clear();
172
+ this.dirty = true;
173
+ }
174
+
175
+ selected() {
176
+ return [...this.selection].map((id) => this.shapes.get(id)).filter(Boolean);
177
+ }
178
+
179
+ // Keep every coordinate inside the image. The server clamps too -- this is
180
+ // so the user sees the box stop at the edge rather than watching it snap
181
+ // back after a save.
182
+ clampShape(shape) {
183
+ const maxX = Math.max(0, this.width - 1);
184
+ const maxY = Math.max(0, this.height - 1);
185
+ const cx = (v) => Math.max(0, Math.min(Math.round(v), maxX));
186
+ const cy = (v) => Math.max(0, Math.min(Math.round(v), maxY));
187
+ let [x1, y1, x2, y2] = shape.bbox;
188
+ if (x2 < x1) [x1, x2] = [x2, x1];
189
+ if (y2 < y1) [y1, y2] = [y2, y1];
190
+ shape.bbox = [cx(x1), cy(y1), cx(x2), cy(y2)];
191
+ if (shape.polygon) {
192
+ shape.polygon = shape.polygon.map(([x, y]) => [cx(x), cy(y)]);
193
+ rebuildBox(shape);
194
+ }
195
+ return shape;
196
+ }
197
+
198
+ toWire() {
199
+ return this.order.map((id) => {
200
+ const shape = this.shapes.get(id);
201
+ return {
202
+ // A draft id is ours, not the server's; sending it would be asking the
203
+ // server to update a row that does not exist.
204
+ id: isDraft(shape.id) ? undefined : shape.id,
205
+ label: shape.label,
206
+ bbox: shape.bbox,
207
+ polygon: shape.polygon,
208
+ kind: kindOf(shape),
209
+ confidence: shape.confidence ?? null,
210
+ // Always complete. The server reads a missing attribute as "keep
211
+ // what you have", so omitting one here could never turn a flag off.
212
+ ...ATTRIBUTE_DEFAULTS,
213
+ ...(shape.attributes || {}),
214
+ };
215
+ });
216
+ }
217
+ }
218
+
219
+ // A polygon's box is derived, never independent. Letting them drift is how a
220
+ // shape ends up selectable somewhere it is not drawn.
221
+ export function rebuildBox(shape) {
222
+ if (!shape.polygon || shape.polygon.length < 2) return shape;
223
+ const xs = shape.polygon.map((p) => p[0]);
224
+ const ys = shape.polygon.map((p) => p[1]);
225
+ shape.bbox = [
226
+ Math.round(Math.min(...xs)),
227
+ Math.round(Math.min(...ys)),
228
+ Math.round(Math.max(...xs)),
229
+ Math.round(Math.max(...ys)),
230
+ ];
231
+ return shape;
232
+ }
233
+
234
+ export const ATTRIBUTE_DEFAULTS = { pose: "Unspecified", truncated: 0, difficult: 0, occluded: 0 };
235
+ export const ATTRIBUTE_KEYS = Object.keys(ATTRIBUTE_DEFAULTS);
236
+
237
+ export function attributesOf(raw) {
238
+ const out = {};
239
+ for (const key of ATTRIBUTE_KEYS) if (raw[key] !== undefined) out[key] = raw[key];
240
+ return out;
241
+ }
242
+
243
+ // What the geometry means. Rows written before kinds existed have none and
244
+ // follow the old rule: an outline of three or more points is a polygon.
245
+ export function kindOf(shape) {
246
+ if (shape.kind) return shape.kind;
247
+ return shape.polygon && shape.polygon.length >= 3 ? "polygon" : "box";
248
+ }
249
+
250
+ export function centroid(points) {
251
+ let x = 0;
252
+ let y = 0;
253
+ for (const [px, py] of points) {
254
+ x += px;
255
+ y += py;
256
+ }
257
+ return [x / points.length, y / points.length];
258
+ }
259
+
260
+ export function rotatePoints(points, cx, cy, radians) {
261
+ const cos = Math.cos(radians);
262
+ const sin = Math.sin(radians);
263
+ return points.map(([x, y]) => {
264
+ const dx = x - cx;
265
+ const dy = y - cy;
266
+ return [cx + dx * cos - dy * sin, cy + dx * sin + dy * cos];
267
+ });
268
+ }
269
+
270
+ export function cloneShape(shape) {
271
+ return {
272
+ ...shape,
273
+ attributes: { ...(shape.attributes || {}) },
274
+ bbox: shape.bbox.slice(),
275
+ polygon: shape.polygon ? shape.polygon.map((p) => p.slice()) : null,
276
+ };
277
+ }
@@ -0,0 +1,72 @@
1
+ // The shapes on this image, as a list.
2
+ //
3
+ // A canvas answers "what is here" by looking; a list answers it by reading.
4
+ // The list is how you find the 4px box hiding under a big one, see that an
5
+ // image holds eleven predictions when you can make out nine, or check what
6
+ // class something is without hovering over it.
7
+ //
8
+ // Rendered from the scene whenever it changes, never edited directly: the
9
+ // scene stays the one source of truth.
10
+
11
+ import { readableOn } from "./render.js";
12
+
13
+ export class ShapeList {
14
+ constructor(root, { onSelect, onHover, colourFor }) {
15
+ this.root = root;
16
+ this.list = root.querySelector('[data-role="shape-list"]');
17
+ this.count = root.querySelector('[data-role="shape-count"]');
18
+ this.template = root.querySelector('[data-role="shape-item"]');
19
+ this.onSelect = onSelect;
20
+ this.onHover = onHover;
21
+ this.colourFor = colourFor;
22
+ this.signature = "";
23
+ }
24
+
25
+ // Cheap to call every frame: it only rebuilds when something it shows moved.
26
+ update(scene, visible, extra = "") {
27
+ const signature = [
28
+ scene.imageId,
29
+ scene.revision,
30
+ scene.order.join(","),
31
+ [...scene.selection].join(","),
32
+ extra,
33
+ ].join("|");
34
+ if (signature === this.signature) return;
35
+ this.signature = signature;
36
+ this.render(scene, visible);
37
+ }
38
+
39
+ render(scene, visible) {
40
+ this.list.replaceChildren();
41
+ let shown = 0;
42
+ scene.order.forEach((id) => {
43
+ const shape = scene.get(id);
44
+ if (!shape) return;
45
+ const node = this.template.content.firstElementChild.cloneNode(true);
46
+ const swatch = node.querySelector('[data-role="swatch"]');
47
+ const name = node.querySelector('[data-role="name"]');
48
+ const meta = node.querySelector('[data-role="meta"]');
49
+
50
+ const colour = this.colourFor(shape);
51
+ swatch.style.background = colour;
52
+ swatch.style.color = readableOn(colour);
53
+ name.textContent = shape.label;
54
+ // A prediction nobody has touched says how sure the model was; a
55
+ // corrected or hand-drawn shape has nothing to qualify.
56
+ meta.textContent =
57
+ shape.origin === "model" && !shape.edited && shape.confidence != null
58
+ ? `${Math.round(shape.confidence * 100)}%`
59
+ : "";
60
+ if (scene.selection.has(shape.id)) node.dataset.current = "true";
61
+ if (!visible(shape)) node.dataset.hidden = "true";
62
+ else shown += 1;
63
+
64
+ node.addEventListener("click", (event) => this.onSelect(shape.id, event.shiftKey));
65
+ node.addEventListener("mouseenter", () => this.onHover(shape.id));
66
+ node.addEventListener("mouseleave", () => this.onHover(null));
67
+ this.list.append(node);
68
+ });
69
+ const total = scene.order.length;
70
+ this.count.textContent = total === shown ? `${total}` : `${shown} of ${total} shown`;
71
+ }
72
+ }
@@ -0,0 +1,96 @@
1
+ // Drawing a box.
2
+ //
3
+ // There is no mode to enter. Left-drag on empty canvas draws a box in the
4
+ // current class -- that is the whole interaction, and it is the reason this
5
+ // tool is usually never selected explicitly: `select` hands off to it when a
6
+ // drag starts on empty space with a class chosen.
7
+
8
+ import { draftId } from "../scene.js";
9
+ import { addShape } from "../undo.js";
10
+
11
+ export const name = "box";
12
+ export const cursor = "crosshair";
13
+
14
+ //: Below this, in image pixels, it was a click and not a drag. Without it,
15
+ //: every stray click leaves a zero-area box that exports as a broken label.
16
+ const MIN_SIZE = 3;
17
+
18
+ export function create(editor) {
19
+ let start = null;
20
+ let current = null;
21
+
22
+ return {
23
+ name,
24
+
25
+ pointerdown(event, ix, iy) {
26
+ start = [ix, iy];
27
+ current = [ix, iy];
28
+ },
29
+
30
+ pointermove(event, ix, iy) {
31
+ if (!start) return;
32
+ current = [ix, iy];
33
+ editor.scene.invalidate();
34
+ },
35
+
36
+ pointerup() {
37
+ if (!start || !current) {
38
+ start = current = null;
39
+ return;
40
+ }
41
+ const [x1, y1] = start;
42
+ const [x2, y2] = current;
43
+ start = current = null;
44
+
45
+ if (Math.abs(x2 - x1) < MIN_SIZE || Math.abs(y2 - y1) < MIN_SIZE) {
46
+ editor.scene.invalidate();
47
+ return;
48
+ }
49
+
50
+ const label = editor.currentLabel();
51
+ if (!label) {
52
+ // Nothing is lost -- the geometry is remembered and applied as soon as
53
+ // a name is typed, so the user is never told to go and configure
54
+ // something first.
55
+ editor.promptForLabel({
56
+ bbox: normalise(x1, y1, x2, y2),
57
+ polygon: null,
58
+ });
59
+ return;
60
+ }
61
+
62
+ const shape = editor.scene.clampShape({
63
+ id: draftId(),
64
+ label: label.name,
65
+ labelIdx: label.idx,
66
+ bbox: normalise(x1, y1, x2, y2),
67
+ polygon: null,
68
+ origin: "human",
69
+ confidence: null,
70
+ edited: false,
71
+ });
72
+ editor.undo.run(addShape(editor.scene, shape));
73
+ editor.drew();
74
+ editor.queueSave();
75
+ },
76
+
77
+ cancel() {
78
+ start = current = null;
79
+ editor.scene.invalidate();
80
+ },
81
+
82
+ draft() {
83
+ if (!start || !current) return null;
84
+ return { kind: "box", bbox: normalise(start[0], start[1], current[0], current[1]) };
85
+ },
86
+ };
87
+ }
88
+
89
+ function normalise(x1, y1, x2, y2) {
90
+ return [
91
+ Math.round(Math.min(x1, x2)),
92
+ Math.round(Math.min(y1, y2)),
93
+ Math.round(Math.max(x1, x2)),
94
+ Math.round(Math.max(y1, y2)),
95
+ ];
96
+ }
@@ -0,0 +1,96 @@
1
+ // A polyline: click the vertices, Enter or double-click to finish.
2
+ //
3
+ // Lane markings, seams, cables, edges -- things with length and no area.
4
+ // Same gestures as the polygon tool, minus closing: there is no first point
5
+ // to click back on, and two points are already a line.
6
+
7
+ import { draftId, rebuildBox } from "../scene.js";
8
+ import { addShape } from "../undo.js";
9
+
10
+ export const name = "line";
11
+ export const cursor = "crosshair";
12
+
13
+ export function create(editor) {
14
+ let points = [];
15
+ let cursorAt = null;
16
+
17
+ function finish() {
18
+ if (points.length < 2) {
19
+ editor.say("a line needs at least two points");
20
+ return;
21
+ }
22
+ const outline = points.map(([x, y]) => [Math.round(x), Math.round(y)]);
23
+ points = [];
24
+ cursorAt = null;
25
+
26
+ const label = editor.currentLabel();
27
+ const geometry = rebuildBox({ polygon: outline, bbox: [0, 0, 0, 0] });
28
+ if (!label) {
29
+ editor.promptForLabel({ bbox: geometry.bbox, polygon: outline, kind: "line" });
30
+ return;
31
+ }
32
+ const shape = editor.scene.clampShape({
33
+ id: draftId(),
34
+ label: label.name,
35
+ labelIdx: label.idx,
36
+ bbox: geometry.bbox,
37
+ polygon: outline,
38
+ kind: "line",
39
+ origin: "human",
40
+ confidence: null,
41
+ edited: false,
42
+ });
43
+ editor.undo.run(addShape(editor.scene, shape));
44
+ editor.drew();
45
+ editor.queueSave();
46
+ }
47
+
48
+ return {
49
+ name,
50
+
51
+ pointerdown(event, ix, iy) {
52
+ if (event.detail >= 2) {
53
+ finish();
54
+ return;
55
+ }
56
+ points.push([ix, iy]);
57
+ editor.scene.invalidate();
58
+ },
59
+
60
+ pointermove(event, ix, iy) {
61
+ if (!points.length) return;
62
+ cursorAt = [ix, iy];
63
+ editor.scene.invalidate();
64
+ },
65
+
66
+ pointerup() {},
67
+
68
+ key(event) {
69
+ if (event.key === "Enter") {
70
+ finish();
71
+ return true;
72
+ }
73
+ if (event.key === "Backspace" && points.length) {
74
+ points.pop();
75
+ editor.scene.invalidate();
76
+ return true;
77
+ }
78
+ if (event.key === "Escape" && points.length) {
79
+ this.cancel();
80
+ return true;
81
+ }
82
+ return false;
83
+ },
84
+
85
+ cancel() {
86
+ points = [];
87
+ cursorAt = null;
88
+ editor.scene.invalidate();
89
+ },
90
+
91
+ draft() {
92
+ if (!points.length) return null;
93
+ return { kind: "line", points, cursor: cursorAt };
94
+ },
95
+ };
96
+ }
@@ -0,0 +1,88 @@
1
+ // A rotated box: drag it out like a box, then turn it.
2
+ //
3
+ // Drawn upright first, because that is one gesture and most objects start
4
+ // near upright anyway; the rotation handle above the box, or [ and ], do the
5
+ // turning. Stored as a four-corner polygon with kind "obb" -- which is
6
+ // exactly what YOLO-OBB and DOTA write, so export is a straight copy.
7
+
8
+ import { draftId } from "../scene.js";
9
+ import { addShape } from "../undo.js";
10
+
11
+ export const name = "obb";
12
+ export const cursor = "crosshair";
13
+
14
+ const MIN_SIZE = 3;
15
+
16
+ export function create(editor) {
17
+ let start = null;
18
+ let current = null;
19
+
20
+ return {
21
+ name,
22
+
23
+ pointerdown(event, ix, iy) {
24
+ start = [ix, iy];
25
+ current = [ix, iy];
26
+ },
27
+
28
+ pointermove(event, ix, iy) {
29
+ if (!start) return;
30
+ current = [ix, iy];
31
+ editor.scene.invalidate();
32
+ },
33
+
34
+ pointerup() {
35
+ if (!start || !current) {
36
+ start = current = null;
37
+ return;
38
+ }
39
+ const [ax, ay] = start;
40
+ const [bx, by] = current;
41
+ start = current = null;
42
+ if (Math.abs(bx - ax) < MIN_SIZE || Math.abs(by - ay) < MIN_SIZE) {
43
+ editor.scene.invalidate();
44
+ return;
45
+ }
46
+ const x1 = Math.round(Math.min(ax, bx));
47
+ const y1 = Math.round(Math.min(ay, by));
48
+ const x2 = Math.round(Math.max(ax, bx));
49
+ const y2 = Math.round(Math.max(ay, by));
50
+ // Clockwise from the top-left, so edge 0 is the top: the rotation
51
+ // handle hangs off it, and DOTA readers expect a consistent order.
52
+ const corners = [[x1, y1], [x2, y1], [x2, y2], [x1, y2]];
53
+
54
+ const label = editor.currentLabel();
55
+ if (!label) {
56
+ editor.promptForLabel({ bbox: [x1, y1, x2, y2], polygon: corners, kind: "obb" });
57
+ return;
58
+ }
59
+ const shape = editor.scene.clampShape({
60
+ id: draftId(),
61
+ label: label.name,
62
+ labelIdx: label.idx,
63
+ bbox: [x1, y1, x2, y2],
64
+ polygon: corners,
65
+ kind: "obb",
66
+ origin: "human",
67
+ confidence: null,
68
+ edited: false,
69
+ });
70
+ editor.undo.run(addShape(editor.scene, shape));
71
+ editor.drew();
72
+ editor.queueSave();
73
+ editor.say("drag the handle above the box to rotate it · [ and ] turn it from the keyboard");
74
+ },
75
+
76
+ cancel() {
77
+ start = current = null;
78
+ editor.scene.invalidate();
79
+ },
80
+
81
+ draft() {
82
+ if (!start || !current) return null;
83
+ const [ax, ay] = start;
84
+ const [bx, by] = current;
85
+ return { kind: "box", bbox: [Math.min(ax, bx), Math.min(ay, by), Math.max(ax, bx), Math.max(ay, by)] };
86
+ },
87
+ };
88
+ }
@@ -0,0 +1,49 @@
1
+ // A point: one click.
2
+ //
3
+ // Keypoints, landmarks, "the thing is here" -- the fastest annotation there
4
+ // is, so it must cost exactly one click and nothing else. A point is stored as
5
+ // a degenerate box (x, y, x, y) with kind "point", which keeps every writer
6
+ // that only knows boxes from having to learn anything.
7
+
8
+ import { draftId } from "../scene.js";
9
+ import { addShape } from "../undo.js";
10
+
11
+ export const name = "point";
12
+ export const cursor = "crosshair";
13
+
14
+ export function create(editor) {
15
+ return {
16
+ name,
17
+
18
+ pointerdown(event, ix, iy) {
19
+ const x = Math.round(ix);
20
+ const y = Math.round(iy);
21
+ const label = editor.currentLabel();
22
+ if (!label) {
23
+ editor.promptForLabel({ bbox: [x, y, x, y], polygon: null, kind: "point" });
24
+ return;
25
+ }
26
+ const shape = editor.scene.clampShape({
27
+ id: draftId(),
28
+ label: label.name,
29
+ labelIdx: label.idx,
30
+ bbox: [x, y, x, y],
31
+ polygon: null,
32
+ kind: "point",
33
+ origin: "human",
34
+ confidence: null,
35
+ edited: false,
36
+ });
37
+ editor.undo.run(addShape(editor.scene, shape));
38
+ editor.drew();
39
+ editor.queueSave();
40
+ },
41
+
42
+ pointermove() {},
43
+ pointerup() {},
44
+ cancel() {},
45
+ draft() {
46
+ return null;
47
+ },
48
+ };
49
+ }