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,116 @@
|
|
|
1
|
+
"""Loading a model's output back onto a project.
|
|
2
|
+
|
|
3
|
+
The upload half of the review loop. A client sends a file -- a zip of YOLO
|
|
4
|
+
``.txt``, VOC ``.xml`` or LabelMe ``.json``, or a single COCO json in either
|
|
5
|
+
the dataset or the detection-results form -- and never a path. The same
|
|
6
|
+
zip-slip, zip-bomb and member-count guards the dataset upload uses apply here,
|
|
7
|
+
because an archive from a training run is no more trustworthy than any other.
|
|
8
|
+
|
|
9
|
+
The response is a **matching report**, and it is the point of the endpoint.
|
|
10
|
+
Predictions match images by filename stem; when they do not line up the result
|
|
11
|
+
is zero shapes imported and no error anywhere, which looks exactly like a model
|
|
12
|
+
that predicted nothing. Saying so plainly is the difference between a feature
|
|
13
|
+
that works and one people quietly stop using.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import shutil
|
|
19
|
+
import tempfile
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
|
24
|
+
from starlette.concurrency import run_in_threadpool
|
|
25
|
+
|
|
26
|
+
from ...annotate import projects as projects_mod
|
|
27
|
+
from ...annotate import review
|
|
28
|
+
from ...annotate.store import Store, StoreError
|
|
29
|
+
from ..deps import get_store, principal, require_key
|
|
30
|
+
from ..workspace import MAX_UPLOAD_BYTES, Workspace, WorkspaceError, extract_zip
|
|
31
|
+
|
|
32
|
+
router = APIRouter(prefix="/api", tags=["annotate"], dependencies=[Depends(require_key)])
|
|
33
|
+
|
|
34
|
+
#: Anything else is not a shape this endpoint knows how to read, and guessing
|
|
35
|
+
#: from bytes would be worse than saying so.
|
|
36
|
+
ALLOWED_SUFFIXES = (".zip", ".json")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@router.post("/projects/{project_id}/predictions", status_code=201)
|
|
40
|
+
async def import_predictions(
|
|
41
|
+
project_id: str,
|
|
42
|
+
file: UploadFile = File(...),
|
|
43
|
+
name: str = Form(default=""),
|
|
44
|
+
min_confidence: float = Form(default=0.0),
|
|
45
|
+
overwrite_reviewed: bool = Form(default=False),
|
|
46
|
+
store: Store = Depends(get_store),
|
|
47
|
+
owner: str = Depends(principal),
|
|
48
|
+
) -> dict[str, Any]:
|
|
49
|
+
try:
|
|
50
|
+
project = projects_mod.get(store, project_id, owner)
|
|
51
|
+
except StoreError as exc:
|
|
52
|
+
raise HTTPException(404, str(exc)) from None
|
|
53
|
+
|
|
54
|
+
suffix = Path(file.filename or "").suffix.lower()
|
|
55
|
+
if suffix not in ALLOWED_SUFFIXES:
|
|
56
|
+
raise HTTPException(
|
|
57
|
+
400,
|
|
58
|
+
f"send a .zip of label files or a .json of predictions, not "
|
|
59
|
+
f"{suffix or 'a file with no extension'}",
|
|
60
|
+
)
|
|
61
|
+
if not (0.0 <= min_confidence <= 1.0):
|
|
62
|
+
raise HTTPException(400, "min_confidence must be between 0 and 1")
|
|
63
|
+
|
|
64
|
+
holding = Path(tempfile.mkdtemp(prefix="predictions-"))
|
|
65
|
+
try:
|
|
66
|
+
payload = holding / "upload" / (Path(file.filename or "upload").name)
|
|
67
|
+
payload.parent.mkdir(parents=True, exist_ok=True)
|
|
68
|
+
|
|
69
|
+
written = 0
|
|
70
|
+
with payload.open("wb") as handle:
|
|
71
|
+
while chunk := await file.read(1 << 20):
|
|
72
|
+
written += len(chunk)
|
|
73
|
+
if written > MAX_UPLOAD_BYTES:
|
|
74
|
+
raise HTTPException(413, "that file is too large")
|
|
75
|
+
handle.write(chunk)
|
|
76
|
+
if written == 0:
|
|
77
|
+
raise HTTPException(400, "that file was empty")
|
|
78
|
+
|
|
79
|
+
if suffix == ".zip":
|
|
80
|
+
# Rooted at the temp directory so extract_zip's containment check
|
|
81
|
+
# is against somewhere disposable, not the real workspace.
|
|
82
|
+
sandbox = Workspace(holding / "unpacked")
|
|
83
|
+
target = sandbox.root / "run"
|
|
84
|
+
try:
|
|
85
|
+
await run_in_threadpool(extract_zip, payload, target, workspace=sandbox)
|
|
86
|
+
except WorkspaceError as exc:
|
|
87
|
+
raise HTTPException(400, str(exc)) from None
|
|
88
|
+
source = target
|
|
89
|
+
else:
|
|
90
|
+
source = payload
|
|
91
|
+
|
|
92
|
+
try:
|
|
93
|
+
# Off the event loop: matching and saving thousands of images is
|
|
94
|
+
# seconds of work that would otherwise stall every other request.
|
|
95
|
+
report = await run_in_threadpool(
|
|
96
|
+
review.import_predictions,
|
|
97
|
+
store,
|
|
98
|
+
project,
|
|
99
|
+
source,
|
|
100
|
+
name=name or Path(file.filename or "").stem,
|
|
101
|
+
min_confidence=min_confidence,
|
|
102
|
+
overwrite_reviewed=overwrite_reviewed,
|
|
103
|
+
)
|
|
104
|
+
except StoreError as exc:
|
|
105
|
+
raise HTTPException(400, str(exc)) from None
|
|
106
|
+
finally:
|
|
107
|
+
shutil.rmtree(holding, ignore_errors=True)
|
|
108
|
+
|
|
109
|
+
from ...annotate import images as images_mod
|
|
110
|
+
from ...annotate import labels as labels_mod
|
|
111
|
+
from ...annotate import shapes as shapes_mod
|
|
112
|
+
|
|
113
|
+
report["counts"] = images_mod.counts(store, project)
|
|
114
|
+
report["agreement"] = shapes_mod.agreement(store, project)
|
|
115
|
+
report["labels"] = [label.as_dict() for label in labels_mod.listing(store, project)]
|
|
116
|
+
return report
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""Turning an uploaded dataset into an annotation project.
|
|
2
|
+
|
|
3
|
+
This is the funnel. A stranger drops a zip on the front page, looks at what the
|
|
4
|
+
tool says about it, clicks "Start annotating", and only *then* is there a
|
|
5
|
+
project worth asking them to sign in for. Nothing before this point required an
|
|
6
|
+
account, a schema, or a decision.
|
|
7
|
+
|
|
8
|
+
It is also the migration path. The browser annotator at annotationfree4u.com
|
|
9
|
+
exports Pascal VOC, and this reads VOC, so "upload what you already have and
|
|
10
|
+
carry on" works on day one rather than after a converter is written.
|
|
11
|
+
|
|
12
|
+
**What happens to state on import.** Shapes that arrive with a confidence score
|
|
13
|
+
came from a detector and land as ``predicted`` -- unreviewed, and excluded from
|
|
14
|
+
export until a person has looked. Shapes without one were drawn by a human in
|
|
15
|
+
some other tool, so they land as ``reviewed``. That is the same distinction the
|
|
16
|
+
whole review layer rests on, applied at the door.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import shutil
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
25
|
+
from pydantic import BaseModel, Field
|
|
26
|
+
|
|
27
|
+
from ...annotate import images as images_mod
|
|
28
|
+
from ...annotate import labels as labels_mod
|
|
29
|
+
from ...annotate import projects as projects_mod
|
|
30
|
+
from ...annotate import shapes as shapes_mod
|
|
31
|
+
from ...annotate.store import Store, StoreError
|
|
32
|
+
from ...core import fs, imaging
|
|
33
|
+
from ...core.annotations import formats, yolo
|
|
34
|
+
from ...core.annotations.model import AnnotationError
|
|
35
|
+
from ..deps import get_store, media_root, principal, require_key
|
|
36
|
+
from ..workspace import Workspace, WorkspaceError, resolve_workspace_root
|
|
37
|
+
|
|
38
|
+
router = APIRouter(prefix="/api", tags=["annotate"], dependencies=[Depends(require_key)])
|
|
39
|
+
|
|
40
|
+
#: A promotion copies files. Past this many, it is not a browser upload any
|
|
41
|
+
#: more and belongs on the command line.
|
|
42
|
+
MAX_IMAGES = 50_000
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class PromoteIn(BaseModel):
|
|
46
|
+
name: str = Field(default="", max_length=200)
|
|
47
|
+
import_annotations: bool = True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@router.post("/datasets/{dataset_id}/promote", status_code=201)
|
|
51
|
+
def promote(
|
|
52
|
+
dataset_id: str,
|
|
53
|
+
body: PromoteIn,
|
|
54
|
+
store: Store = Depends(get_store),
|
|
55
|
+
owner: str = Depends(principal),
|
|
56
|
+
) -> dict[str, Any]:
|
|
57
|
+
"""Create a project from an uploaded dataset."""
|
|
58
|
+
# Built per request rather than held at module scope: the workspace root is
|
|
59
|
+
# an environment variable, and a cached one would outlive a change to it.
|
|
60
|
+
workspace = Workspace(resolve_workspace_root())
|
|
61
|
+
try:
|
|
62
|
+
dataset = workspace.get(dataset_id)
|
|
63
|
+
except WorkspaceError as exc:
|
|
64
|
+
raise HTTPException(404, str(exc)) from None
|
|
65
|
+
|
|
66
|
+
source = dataset.source
|
|
67
|
+
files = sorted(p for p in fs.iter_images(source, recursive=True))
|
|
68
|
+
if not files:
|
|
69
|
+
raise HTTPException(400, "that dataset has no images to annotate")
|
|
70
|
+
if len(files) > MAX_IMAGES:
|
|
71
|
+
raise HTTPException(
|
|
72
|
+
413,
|
|
73
|
+
f"{len(files):,} images is past what a browser upload should carry. "
|
|
74
|
+
f"Use 'datafactory annotate' on the folder directly.",
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
project = projects_mod.create(store, body.name or f"Dataset {dataset_id[:8]}", owner_id=owner)
|
|
78
|
+
root = media_root(project.id)
|
|
79
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
80
|
+
|
|
81
|
+
warnings: list[str] = []
|
|
82
|
+
loaded = None
|
|
83
|
+
if body.import_annotations:
|
|
84
|
+
try:
|
|
85
|
+
loaded = formats.read_dataset(source, "auto", recursive=True)
|
|
86
|
+
warnings.extend(loaded.warnings[:10])
|
|
87
|
+
except AnnotationError as exc:
|
|
88
|
+
# A folder of bare images is a completely normal thing to annotate.
|
|
89
|
+
warnings.append(f"no annotations imported: {exc}")
|
|
90
|
+
|
|
91
|
+
# Adopt an existing class order before anything else. If this dataset came
|
|
92
|
+
# with a data.yaml, its indices are already baked into .txt files that
|
|
93
|
+
# exist in the world, and inventing our own order would invalidate them.
|
|
94
|
+
if loaded is not None and loaded.classes:
|
|
95
|
+
try:
|
|
96
|
+
labels_mod.adopt(store, project, list(loaded.classes))
|
|
97
|
+
except StoreError as exc:
|
|
98
|
+
warnings.append(f"could not adopt the existing class order: {exc}")
|
|
99
|
+
elif yolo.find_data_yaml(source) is not None:
|
|
100
|
+
warnings.append("a data.yaml was present but its class list could not be read")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
copied = 0
|
|
104
|
+
registered: list[tuple[str, int, int]] = []
|
|
105
|
+
for path in files:
|
|
106
|
+
rel = path.relative_to(source).as_posix()
|
|
107
|
+
target = root / rel
|
|
108
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
109
|
+
try:
|
|
110
|
+
shutil.copy2(path, target)
|
|
111
|
+
copied += 1
|
|
112
|
+
except OSError as exc:
|
|
113
|
+
warnings.append(f"{rel}: could not copy ({exc})")
|
|
114
|
+
continue
|
|
115
|
+
try:
|
|
116
|
+
# Header read, EXIF honoured, no decode -- 20,000 images stays fast.
|
|
117
|
+
width, height = imaging.image_size(target)
|
|
118
|
+
except Exception:
|
|
119
|
+
width = height = 0
|
|
120
|
+
registered.append((rel, width, height))
|
|
121
|
+
|
|
122
|
+
made = images_mod.add_many(store, project, registered)
|
|
123
|
+
|
|
124
|
+
# Match labels to images by path; a bare stem only when it is unique.
|
|
125
|
+
found: dict = {}
|
|
126
|
+
if loaded is not None:
|
|
127
|
+
found, unsure = images_mod.assign(
|
|
128
|
+
made, ((images_mod.item_name(item, source), item.annotation) for item in loaded.items)
|
|
129
|
+
)
|
|
130
|
+
if unsure:
|
|
131
|
+
warnings.append(
|
|
132
|
+
f"{len(unsure)} label file(s) could belong to more than one image and "
|
|
133
|
+
f"were not imported, for example: {', '.join(unsure[:5])}"
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
imported_shapes = 0
|
|
137
|
+
predicted = 0
|
|
138
|
+
reviewed = 0
|
|
139
|
+
for image in made:
|
|
140
|
+
annotation = found.get(image.id)
|
|
141
|
+
if annotation is None or not annotation.shapes:
|
|
142
|
+
continue
|
|
143
|
+
# A score means a detector produced it. No score means a person did.
|
|
144
|
+
from_model = any(s.confidence is not None for s in annotation.shapes)
|
|
145
|
+
try:
|
|
146
|
+
shapes_mod.replace_all(
|
|
147
|
+
store,
|
|
148
|
+
project,
|
|
149
|
+
image,
|
|
150
|
+
list(annotation.shapes),
|
|
151
|
+
origin="model" if from_model else "human",
|
|
152
|
+
)
|
|
153
|
+
except StoreError as exc:
|
|
154
|
+
warnings.append(f"{image.rel_path}: {exc}")
|
|
155
|
+
continue
|
|
156
|
+
imported_shapes += len(annotation.shapes)
|
|
157
|
+
state = "predicted" if from_model else "reviewed"
|
|
158
|
+
images_mod.set_state(store, project, image, state)
|
|
159
|
+
if from_model:
|
|
160
|
+
predicted += 1
|
|
161
|
+
else:
|
|
162
|
+
reviewed += 1
|
|
163
|
+
|
|
164
|
+
unmatched = len(made) - (predicted + reviewed)
|
|
165
|
+
if loaded is not None and loaded.items and unmatched:
|
|
166
|
+
# Never silent. Importing zero shapes because the filenames did not
|
|
167
|
+
# line up is the single most common way this goes wrong.
|
|
168
|
+
warnings.append(
|
|
169
|
+
f"{predicted + reviewed} image(s) matched an annotation file, "
|
|
170
|
+
f"{unmatched} did not and start empty"
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
return {
|
|
174
|
+
**project.as_dict(),
|
|
175
|
+
"images": len(made),
|
|
176
|
+
"images_copied": copied,
|
|
177
|
+
"shapes_imported": imported_shapes,
|
|
178
|
+
"reviewed": reviewed,
|
|
179
|
+
"predicted": predicted,
|
|
180
|
+
"labels": [label.as_dict() for label in labels_mod.listing(store, project)],
|
|
181
|
+
"warnings": warnings,
|
|
182
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"""Heavy tools, over HTTP: what is available, and running one.
|
|
2
|
+
|
|
3
|
+
The rule this router exists to keep, stated in the plan and worth repeating
|
|
4
|
+
because everything here follows from it:
|
|
5
|
+
|
|
6
|
+
**A heavy tool's absence is never an error.**
|
|
7
|
+
|
|
8
|
+
``GET /api/tools`` returning ``{"tools": []}`` is a normal, tested state -- it
|
|
9
|
+
is what a plain ``pip install data-factory`` produces, and the editor is fully
|
|
10
|
+
usable in it. Buttons appear only for a tool that is both listed *and* healthy,
|
|
11
|
+
so there are no dead controls and no install nags in the toolbar.
|
|
12
|
+
|
|
13
|
+
Anything model-shaped runs in a separate process (see
|
|
14
|
+
``annotate/tools/local.py``). A CUDA out-of-memory or a native segfault kills
|
|
15
|
+
that process, the registry reports the tool unavailable, and **the person
|
|
16
|
+
carries on drawing boxes**. That is the entire reason for the boundary.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any
|
|
23
|
+
|
|
24
|
+
from fastapi import APIRouter, Depends, HTTPException
|
|
25
|
+
from pydantic import BaseModel, Field
|
|
26
|
+
|
|
27
|
+
from ...annotate import images as images_mod
|
|
28
|
+
from ...annotate import labels as labels_mod
|
|
29
|
+
from ...annotate import projects as projects_mod
|
|
30
|
+
from ...annotate import shapes as shapes_mod
|
|
31
|
+
from ...annotate.store import Store, StoreError
|
|
32
|
+
from ...annotate.tools.protocol import ToolError, ToolNotReady, ToolUnavailable
|
|
33
|
+
from ...annotate.tools.registry import REGISTRY
|
|
34
|
+
from ...annotate.tools.results import shapes_from_result as _shapes_from
|
|
35
|
+
from ..deps import get_store, media_root, principal, require_key, resolve_media
|
|
36
|
+
|
|
37
|
+
router = APIRouter(prefix="/api", tags=["tools"], dependencies=[Depends(require_key)])
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class InvokeIn(BaseModel):
|
|
41
|
+
image_id: str
|
|
42
|
+
#: Whatever the tool's declared input schema asks for -- a box, a set of
|
|
43
|
+
#: click points. Passed through rather than modelled here, because the
|
|
44
|
+
#: whole point of a declared spec is that a new tool needs no server change.
|
|
45
|
+
params: dict[str, Any] = Field(default_factory=dict)
|
|
46
|
+
label: str = ""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class BatchIn(BaseModel):
|
|
50
|
+
#: The tool's own inputs -- for a prompt tool, the text.
|
|
51
|
+
params: dict[str, Any] = Field(default_factory=dict)
|
|
52
|
+
label: str = ""
|
|
53
|
+
#: Which images to run over. Reviewed images are never touched here:
|
|
54
|
+
#: a person's finished work is not something a model gets to redo.
|
|
55
|
+
states: list[str] = Field(default_factory=lambda: ["todo"])
|
|
56
|
+
limit: int = 2000
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@router.post("/projects/{project_id}/tools/{name}/batch")
|
|
60
|
+
def invoke_tool_batch(
|
|
61
|
+
project_id: str,
|
|
62
|
+
name: str,
|
|
63
|
+
body: BatchIn,
|
|
64
|
+
store: Store = Depends(get_store),
|
|
65
|
+
owner: str = Depends(principal),
|
|
66
|
+
) -> dict[str, Any]:
|
|
67
|
+
"""Run a tool over many images as a job; its answers become predictions.
|
|
68
|
+
|
|
69
|
+
This is "type a class name, get every instance to review": each image's
|
|
70
|
+
results go through review.set_predictions, so they arrive dashed, with
|
|
71
|
+
confidence, counted by the agreement metric, and never exported until
|
|
72
|
+
somebody has looked at them. The same path a model run imported from a
|
|
73
|
+
file takes -- a tool is just a model run that happens here.
|
|
74
|
+
"""
|
|
75
|
+
import secrets
|
|
76
|
+
|
|
77
|
+
from ...annotate import review
|
|
78
|
+
from ...core.types import OpResult
|
|
79
|
+
from ..deps import jobs_manager
|
|
80
|
+
|
|
81
|
+
try:
|
|
82
|
+
project = projects_mod.get(store, project_id, owner)
|
|
83
|
+
except StoreError as exc:
|
|
84
|
+
raise HTTPException(404, str(exc)) from None
|
|
85
|
+
jobs = jobs_manager()
|
|
86
|
+
if jobs is None:
|
|
87
|
+
raise HTTPException(503, "no job runner in this process")
|
|
88
|
+
wanted = [s for s in body.states if s in images_mod.STATES and s != "reviewed"]
|
|
89
|
+
if not wanted:
|
|
90
|
+
raise HTTPException(
|
|
91
|
+
400, "states must name unreviewed states: todo, predicted, in_progress, skipped"
|
|
92
|
+
)
|
|
93
|
+
if name not in {spec.name for spec in REGISTRY.available()}:
|
|
94
|
+
raise HTTPException(404, f"no tool called {name!r} is installed")
|
|
95
|
+
_refuse_local_only(name)
|
|
96
|
+
_refuse_reserved(body.params)
|
|
97
|
+
|
|
98
|
+
targets: list[Any] = []
|
|
99
|
+
for state in wanted:
|
|
100
|
+
targets.extend(images_mod.listing(store, project, state=state, limit=body.limit))
|
|
101
|
+
targets = targets[: body.limit]
|
|
102
|
+
root = Path(project.root) if project.is_attached and project.root else media_root(project.id)
|
|
103
|
+
label = (body.label or "").strip()
|
|
104
|
+
spec = next(s for s in REGISTRY.available() if s.name == name)
|
|
105
|
+
needs_prepare = "prepare" in (spec.requires or ())
|
|
106
|
+
|
|
107
|
+
def work(context) -> OpResult:
|
|
108
|
+
result = OpResult(op=f"tool:{name}")
|
|
109
|
+
predicted = shapes_total = failed = 0
|
|
110
|
+
for index, image in enumerate(targets):
|
|
111
|
+
context.check_cancelled()
|
|
112
|
+
context.emit(index, len(targets), image.name)
|
|
113
|
+
path = resolve_media(root, image.rel_path)
|
|
114
|
+
if not path.is_file():
|
|
115
|
+
failed += 1
|
|
116
|
+
continue
|
|
117
|
+
payload = {
|
|
118
|
+
**body.params,
|
|
119
|
+
"image_id": image.id,
|
|
120
|
+
"image_path": str(path),
|
|
121
|
+
"width": image.width,
|
|
122
|
+
"height": image.height,
|
|
123
|
+
}
|
|
124
|
+
if needs_prepare:
|
|
125
|
+
try:
|
|
126
|
+
REGISTRY.prepare(name, image.id, path)
|
|
127
|
+
except Exception:
|
|
128
|
+
pass # the invoke below reports the real problem
|
|
129
|
+
raw = _invoke_patiently(name, payload)
|
|
130
|
+
if raw is None:
|
|
131
|
+
failed += 1
|
|
132
|
+
continue
|
|
133
|
+
made = _shapes_from(raw, label, image.width, image.height)
|
|
134
|
+
# Re-read: a person may have reviewed this image while the batch
|
|
135
|
+
# ran, and their work is not the batch's to replace.
|
|
136
|
+
try:
|
|
137
|
+
current = images_mod.get(store, project, image.id)
|
|
138
|
+
if current.state not in wanted:
|
|
139
|
+
continue
|
|
140
|
+
review.set_predictions(store, project, current, made, base_version=current.version)
|
|
141
|
+
except StoreError:
|
|
142
|
+
continue
|
|
143
|
+
predicted += 1
|
|
144
|
+
shapes_total += len(made)
|
|
145
|
+
context.emit(len(targets), len(targets), "done")
|
|
146
|
+
result.data = {
|
|
147
|
+
"images": len(targets),
|
|
148
|
+
"predicted": predicted,
|
|
149
|
+
"shapes": shapes_total,
|
|
150
|
+
"failed": failed,
|
|
151
|
+
"agreement": shapes_mod.agreement(store, project),
|
|
152
|
+
}
|
|
153
|
+
if failed:
|
|
154
|
+
result.warnings.append(f"{failed} image(s) could not be processed")
|
|
155
|
+
return result
|
|
156
|
+
|
|
157
|
+
job = jobs.submit(secrets.token_hex(8), f"tool:{name}", project.id, work)
|
|
158
|
+
return job.as_dict()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _invoke_patiently(name: str, payload: dict[str, Any]) -> Any:
|
|
162
|
+
"""A tool still loading its weights says not_ready; wait for it. Anything
|
|
163
|
+
else wrong with one image is that image's problem, not the job's."""
|
|
164
|
+
try:
|
|
165
|
+
return REGISTRY.invoke_waiting(name, payload, budget_s=120.0)
|
|
166
|
+
except Exception:
|
|
167
|
+
return None
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
#: Keys the server sets itself. A client's copy is refused, not overwritten:
|
|
171
|
+
#: a request that tries to name the tool, a model file or an image path is
|
|
172
|
+
#: not a mistake worth tolerating.
|
|
173
|
+
RESERVED_PARAMS = frozenset({"tool", "model", "source", "image_path", "image_id", "width", "height"})
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _refuse_reserved(params: dict[str, Any]) -> None:
|
|
177
|
+
clash = sorted(RESERVED_PARAMS.intersection(params))
|
|
178
|
+
if clash:
|
|
179
|
+
raise HTTPException(400, f"these are set by the server, not the request: {', '.join(clash)}")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _refuse_local_only(name: str) -> None:
|
|
183
|
+
"""A "model" tool loads a model file, and a model file is code: loading a
|
|
184
|
+
.pt unpickles it. Over HTTP that would let a client name any file to run
|
|
185
|
+
on the server. Like attached folders, these run from the command line
|
|
186
|
+
only -- `easyannotate predict` -- where the path comes from a person
|
|
187
|
+
typing it into their own terminal."""
|
|
188
|
+
spec = REGISTRY.spec(name)
|
|
189
|
+
if spec is not None and spec.kind == "model":
|
|
190
|
+
raise HTTPException(
|
|
191
|
+
403, f"{name!r} runs a model file, which is code; use `easyannotate predict` instead"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class PrepareIn(BaseModel):
|
|
196
|
+
image_id: str
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@router.post("/projects/{project_id}/tools/{name}/prepare")
|
|
200
|
+
def prepare_tool(
|
|
201
|
+
project_id: str,
|
|
202
|
+
name: str,
|
|
203
|
+
body: PrepareIn,
|
|
204
|
+
store: Store = Depends(get_store),
|
|
205
|
+
owner: str = Depends(principal),
|
|
206
|
+
) -> dict[str, Any]:
|
|
207
|
+
"""Warm a tool for the image that is open: encode-ahead.
|
|
208
|
+
|
|
209
|
+
Best-effort by contract and always quick -- the editor fires this on
|
|
210
|
+
every navigation once a SAM tool is in use, and a failure here must
|
|
211
|
+
never be something the person sees.
|
|
212
|
+
"""
|
|
213
|
+
_refuse_local_only(name)
|
|
214
|
+
if REGISTRY.spec(name) is None:
|
|
215
|
+
raise HTTPException(404, f"no tool called {name!r} is installed")
|
|
216
|
+
try:
|
|
217
|
+
project = projects_mod.get(store, project_id, owner)
|
|
218
|
+
image = images_mod.get(store, project, body.image_id)
|
|
219
|
+
except StoreError as exc:
|
|
220
|
+
raise HTTPException(404, str(exc)) from None
|
|
221
|
+
root = Path(project.root) if project.is_attached and project.root else media_root(project.id)
|
|
222
|
+
path = resolve_media(root, image.rel_path)
|
|
223
|
+
if path.is_file():
|
|
224
|
+
REGISTRY.prepare(name, image.id, path)
|
|
225
|
+
return {"ok": True}
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
@router.get("/tools")
|
|
229
|
+
def list_tools() -> dict[str, Any]:
|
|
230
|
+
"""Every usable tool. An empty list is the normal, healthy default."""
|
|
231
|
+
return REGISTRY.as_dict()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
@router.post("/projects/{project_id}/tools/{name}")
|
|
235
|
+
def invoke_tool(
|
|
236
|
+
project_id: str,
|
|
237
|
+
name: str,
|
|
238
|
+
body: InvokeIn,
|
|
239
|
+
store: Store = Depends(get_store),
|
|
240
|
+
owner: str = Depends(principal),
|
|
241
|
+
) -> dict[str, Any]:
|
|
242
|
+
"""Run a tool against one image and turn its answer into shapes.
|
|
243
|
+
|
|
244
|
+
The mask-to-polygon step is ours, not the tool's: a model returns a raster
|
|
245
|
+
mask, and what the editor needs is an outline with a manageable number of
|
|
246
|
+
draggable vertices. See ``annotate/tools/postprocess.py``.
|
|
247
|
+
"""
|
|
248
|
+
_refuse_local_only(name)
|
|
249
|
+
_refuse_reserved(body.params)
|
|
250
|
+
try:
|
|
251
|
+
project = projects_mod.get(store, project_id, owner)
|
|
252
|
+
image = images_mod.get(store, project, body.image_id)
|
|
253
|
+
except StoreError as exc:
|
|
254
|
+
raise HTTPException(404, str(exc)) from None
|
|
255
|
+
|
|
256
|
+
root = (
|
|
257
|
+
Path(project.root)
|
|
258
|
+
if project.is_attached and project.root
|
|
259
|
+
else media_root(project.id)
|
|
260
|
+
)
|
|
261
|
+
path = resolve_media(root, image.rel_path)
|
|
262
|
+
if not path.is_file():
|
|
263
|
+
raise HTTPException(404, f"{image.rel_path} is no longer on disk")
|
|
264
|
+
|
|
265
|
+
payload = {
|
|
266
|
+
**body.params,
|
|
267
|
+
"image_id": image.id,
|
|
268
|
+
"image_path": str(path),
|
|
269
|
+
"width": image.width,
|
|
270
|
+
"height": image.height,
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
try:
|
|
274
|
+
result = REGISTRY.invoke(name, payload)
|
|
275
|
+
except ToolNotReady as exc:
|
|
276
|
+
# Not an error: the sidecar is still encoding this image. The client
|
|
277
|
+
# is told when to come back rather than being made to guess.
|
|
278
|
+
raise HTTPException(
|
|
279
|
+
503,
|
|
280
|
+
detail={"message": str(exc), "retry_after_ms": getattr(exc, "retry_after_ms", 500)},
|
|
281
|
+
) from None
|
|
282
|
+
except ToolUnavailable as exc:
|
|
283
|
+
raise HTTPException(503, str(exc)) from None
|
|
284
|
+
except ToolError as exc:
|
|
285
|
+
raise HTTPException(400, str(exc)) from None
|
|
286
|
+
except Exception:
|
|
287
|
+
# A tool crashing must not surface a traceback, and must not look like
|
|
288
|
+
# the editor breaking.
|
|
289
|
+
raise HTTPException(502, f"the {name!r} tool failed; drawing by hand still works") from None
|
|
290
|
+
|
|
291
|
+
label = (body.label or "").strip()
|
|
292
|
+
made = _shapes_from(result, label, image.width, image.height)
|
|
293
|
+
if not made:
|
|
294
|
+
return {"shapes": [], "detail": "the tool returned nothing for that input"}
|
|
295
|
+
|
|
296
|
+
if label:
|
|
297
|
+
try:
|
|
298
|
+
labels_mod.ensure(store, project, label)
|
|
299
|
+
except StoreError as exc:
|
|
300
|
+
raise HTTPException(400, str(exc)) from None
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
"shapes": [
|
|
304
|
+
{**shape.as_dict(), "origin": "model"}
|
|
305
|
+
for shape in made
|
|
306
|
+
],
|
|
307
|
+
"detail": result.get("detail", "") if isinstance(result, dict) else "",
|
|
308
|
+
}
|