easyannotate-tools 0.4.0__tar.gz
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.
- easyannotate_tools-0.4.0/.gitignore +41 -0
- easyannotate_tools-0.4.0/PKG-INFO +34 -0
- easyannotate_tools-0.4.0/README.md +20 -0
- easyannotate_tools-0.4.0/easyannotate_tools/__init__.py +3 -0
- easyannotate_tools-0.4.0/easyannotate_tools/sidecar.py +529 -0
- easyannotate_tools-0.4.0/easyannotate_tools/train.py +87 -0
- easyannotate_tools-0.4.0/pyproject.toml +28 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# ── Data & model weights (never track binaries) ───────────────────────────
|
|
2
|
+
Projects/
|
|
3
|
+
Models/
|
|
4
|
+
*.pt
|
|
5
|
+
*.safetensors
|
|
6
|
+
*.zip
|
|
7
|
+
|
|
8
|
+
# ── Images / video ────────────────────────────────────────────────────────
|
|
9
|
+
*.jpg
|
|
10
|
+
*.jpeg
|
|
11
|
+
*.png
|
|
12
|
+
*.bmp
|
|
13
|
+
*.tiff
|
|
14
|
+
*.webp
|
|
15
|
+
*.mp4
|
|
16
|
+
*.avi
|
|
17
|
+
*.mov
|
|
18
|
+
|
|
19
|
+
# ── Python ────────────────────────────────────────────────────────────────
|
|
20
|
+
.venv/
|
|
21
|
+
__pycache__/
|
|
22
|
+
*.py[cod]
|
|
23
|
+
*.egg-info/
|
|
24
|
+
build/
|
|
25
|
+
dist/
|
|
26
|
+
.pytest_cache/
|
|
27
|
+
.ruff_cache/
|
|
28
|
+
.mypy_cache/
|
|
29
|
+
|
|
30
|
+
# ── Editors / OS ──────────────────────────────────────────────────────────
|
|
31
|
+
.vscode/
|
|
32
|
+
.idea/
|
|
33
|
+
Thumbs.db
|
|
34
|
+
desktop.ini
|
|
35
|
+
|
|
36
|
+
# Agent scratch: session settings and worktree stubs. Machine-local,
|
|
37
|
+
# and nothing here belongs to anyone but this checkout.
|
|
38
|
+
.claude/
|
|
39
|
+
|
|
40
|
+
# Machine-specific notes for Claude Code. Never committed.
|
|
41
|
+
CLAUDE.local.md
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: easyannotate-tools
|
|
3
|
+
Version: 0.4.0
|
|
4
|
+
Summary: Model-backed helpers for EasyAnnotate: SAM 2 masks, text-prompted detection, inference. Runs as a sidecar in its own environment.
|
|
5
|
+
Project-URL: Homepage, https://shivpratapsinghpanwar.github.io/EasyAnnotate/
|
|
6
|
+
Project-URL: Source, https://github.com/shivpratapsinghpanwar/EasyAnnotate/tree/main/tools
|
|
7
|
+
Project-URL: Issues, https://github.com/shivpratapsinghpanwar/EasyAnnotate/issues
|
|
8
|
+
Author: Shivpratap Singh Panwar
|
|
9
|
+
License: MIT
|
|
10
|
+
Requires-Python: >=3.12
|
|
11
|
+
Requires-Dist: easyannotate>=0.4.0
|
|
12
|
+
Requires-Dist: ultralytics>=8.3
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# easyannotate-tools
|
|
16
|
+
|
|
17
|
+
The model-backed half of EasyAnnotate, kept in its own environment:
|
|
18
|
+
|
|
19
|
+
- **SAM 2.1** — drag a box or click a point, get an editable outline.
|
|
20
|
+
- **Text-prompted detection (YOLO-World)** — type `handle`, get every handle
|
|
21
|
+
on the image, or on every unreviewed image, as predictions to review.
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
easyannotate tools install # makes ~/.datafactory/tools and installs this
|
|
25
|
+
easyannotate tools status # what the editor will find
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Weights download on first use (SAM 2.1 tiny ≈ 150 MB, YOLO-World small ≈ 25 MB)
|
|
29
|
+
into Ultralytics' cache. A GPU is used when PyTorch can see one; everything
|
|
30
|
+
works on CPU, slower.
|
|
31
|
+
|
|
32
|
+
The editor never imports this. It starts `python -m easyannotate_tools.sidecar`
|
|
33
|
+
from the tools environment and speaks newline-delimited JSON to it; if the
|
|
34
|
+
process is absent or dies, the editor keeps working without it.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# easyannotate-tools
|
|
2
|
+
|
|
3
|
+
The model-backed half of EasyAnnotate, kept in its own environment:
|
|
4
|
+
|
|
5
|
+
- **SAM 2.1** — drag a box or click a point, get an editable outline.
|
|
6
|
+
- **Text-prompted detection (YOLO-World)** — type `handle`, get every handle
|
|
7
|
+
on the image, or on every unreviewed image, as predictions to review.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
easyannotate tools install # makes ~/.datafactory/tools and installs this
|
|
11
|
+
easyannotate tools status # what the editor will find
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Weights download on first use (SAM 2.1 tiny ≈ 150 MB, YOLO-World small ≈ 25 MB)
|
|
15
|
+
into Ultralytics' cache. A GPU is used when PyTorch can see one; everything
|
|
16
|
+
works on CPU, slower.
|
|
17
|
+
|
|
18
|
+
The editor never imports this. It starts `python -m easyannotate_tools.sidecar`
|
|
19
|
+
from the tools environment and speaks newline-delimited JSON to it; if the
|
|
20
|
+
process is absent or dies, the editor keeps working without it.
|
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
"""The sidecar: SAM 2 and text-prompted detection, over the editor's protocol.
|
|
2
|
+
|
|
3
|
+
Run as ``python -m easyannotate_tools.sidecar`` from the tools environment.
|
|
4
|
+
Everything heavy is imported lazily, inside the functions that need it, so
|
|
5
|
+
``describe`` and ``health`` answer in milliseconds and a missing CUDA build
|
|
6
|
+
fails one tool call rather than the handshake.
|
|
7
|
+
|
|
8
|
+
Models load on a background thread the first time they are asked for. Until
|
|
9
|
+
then a call answers ``not_ready`` with a retry hint -- the editor shows
|
|
10
|
+
"warming up" and the person keeps drawing. Blocking the handshake on a model
|
|
11
|
+
load would make the editor look hung for the length of a weights download.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import hashlib
|
|
17
|
+
import inspect
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import threading
|
|
21
|
+
import time
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
from data_factory.annotate.tools import postprocess
|
|
26
|
+
from data_factory.annotate.tools.protocol import ToolError, ToolNotReady, ToolSpec
|
|
27
|
+
from data_factory.annotate.tools.serve import serve
|
|
28
|
+
|
|
29
|
+
SAM_WEIGHTS = "sam2.1_t.pt"
|
|
30
|
+
WORLD_WEIGHTS = "yolov8s-worldv2.pt"
|
|
31
|
+
#: Below this a YOLO-World hit is noise. The editor's slider filters further.
|
|
32
|
+
DEFAULT_MIN_CONFIDENCE = 0.15
|
|
33
|
+
MAX_DETECTIONS = 300
|
|
34
|
+
#: How long one request waits for its answer before replying not_ready and
|
|
35
|
+
#: letting the work continue. Under the wire's 15s invoke timeout, with room.
|
|
36
|
+
INLINE_WAIT_S = 2.5
|
|
37
|
+
#: Finished answers are kept this long for the retry that collects them.
|
|
38
|
+
RESULT_TTL_S = 600.0
|
|
39
|
+
|
|
40
|
+
SPECS = [
|
|
41
|
+
ToolSpec(
|
|
42
|
+
name="sam.box",
|
|
43
|
+
title="Outline (SAM)",
|
|
44
|
+
summary="Drag a box around an object; SAM 2 traces its outline.",
|
|
45
|
+
kind="interactive",
|
|
46
|
+
inputs={
|
|
47
|
+
"type": "object",
|
|
48
|
+
"properties": {"box": {"type": "array", "items": {"type": "number"}}},
|
|
49
|
+
"required": ["box"],
|
|
50
|
+
},
|
|
51
|
+
version="sam2.1-tiny",
|
|
52
|
+
requires=("prepare",),
|
|
53
|
+
),
|
|
54
|
+
ToolSpec(
|
|
55
|
+
name="sam.point",
|
|
56
|
+
title="Outline from a click (SAM)",
|
|
57
|
+
summary="Click an object; SAM 2 traces its outline.",
|
|
58
|
+
kind="interactive",
|
|
59
|
+
inputs={
|
|
60
|
+
"type": "object",
|
|
61
|
+
"properties": {"points": {"type": "array", "items": {"type": "array"}}},
|
|
62
|
+
"required": ["points"],
|
|
63
|
+
},
|
|
64
|
+
version="sam2.1-tiny",
|
|
65
|
+
requires=("prepare",),
|
|
66
|
+
),
|
|
67
|
+
ToolSpec(
|
|
68
|
+
name="detect.text",
|
|
69
|
+
title="Find every...",
|
|
70
|
+
summary="Type what to find; YOLO-World finds every instance as predictions to review.",
|
|
71
|
+
kind="prompt",
|
|
72
|
+
inputs={
|
|
73
|
+
"type": "object",
|
|
74
|
+
"properties": {"text": {"type": "string"}, "min_confidence": {"type": "number"}},
|
|
75
|
+
"required": ["text"],
|
|
76
|
+
},
|
|
77
|
+
version="yolov8s-worldv2",
|
|
78
|
+
requires=(),
|
|
79
|
+
),
|
|
80
|
+
# Command-line only: the editor never shows it and the HTTP routes refuse
|
|
81
|
+
# it, because loading a model file runs it. `easyannotate predict`.
|
|
82
|
+
ToolSpec(
|
|
83
|
+
name="detect.model",
|
|
84
|
+
title="Predict with a trained model",
|
|
85
|
+
summary="Runs an Ultralytics model (detect, segment or OBB) over an image.",
|
|
86
|
+
kind="model",
|
|
87
|
+
inputs={
|
|
88
|
+
"type": "object",
|
|
89
|
+
"properties": {"model": {"type": "string"}, "conf": {"type": "number"}},
|
|
90
|
+
"required": ["model"],
|
|
91
|
+
},
|
|
92
|
+
version="ultralytics",
|
|
93
|
+
requires=(),
|
|
94
|
+
),
|
|
95
|
+
]
|
|
96
|
+
#: Custom models kept loaded, most recent last. Two: the one in use, and the
|
|
97
|
+
#: one it is being compared against.
|
|
98
|
+
MAX_CUSTOM_MODELS = 2
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class _Lazy:
|
|
102
|
+
"""A model that loads itself on a thread, once, and says when it is not ready."""
|
|
103
|
+
|
|
104
|
+
def __init__(self, name: str, loader: Any) -> None:
|
|
105
|
+
self.name = name
|
|
106
|
+
self._loader = loader
|
|
107
|
+
self._lock = threading.Lock()
|
|
108
|
+
self._thread: threading.Thread | None = None
|
|
109
|
+
self.model: Any = None
|
|
110
|
+
self.error: str = ""
|
|
111
|
+
|
|
112
|
+
def get(self) -> Any:
|
|
113
|
+
with self._lock:
|
|
114
|
+
if self.model is not None:
|
|
115
|
+
return self.model
|
|
116
|
+
if self.error:
|
|
117
|
+
raise ToolError(f"{self.name} failed to load: {self.error}", code="unavailable")
|
|
118
|
+
if self._thread is None:
|
|
119
|
+
self._thread = threading.Thread(target=self._load, daemon=True, name=f"load-{self.name}")
|
|
120
|
+
self._thread.start()
|
|
121
|
+
raise ToolNotReady(f"{self.name} is loading", retry_after_ms=1500)
|
|
122
|
+
|
|
123
|
+
def _load(self) -> None:
|
|
124
|
+
try:
|
|
125
|
+
model = self._loader()
|
|
126
|
+
except Exception as exc: # torch, download, CUDA -- all land here
|
|
127
|
+
with self._lock:
|
|
128
|
+
self.error = f"{type(exc).__name__}: {exc}"
|
|
129
|
+
return
|
|
130
|
+
with self._lock:
|
|
131
|
+
self.model = model
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def state(self) -> str:
|
|
135
|
+
with self._lock:
|
|
136
|
+
if self.model is not None:
|
|
137
|
+
return "ready"
|
|
138
|
+
if self.error:
|
|
139
|
+
return "unavailable"
|
|
140
|
+
return "loading" if self._thread else "cold"
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _profile() -> dict[str, Any]:
|
|
144
|
+
"""What `easyannotate tools install` chose for this machine: device and
|
|
145
|
+
model sizes. Absent, the defaults are the small CPU-friendly models."""
|
|
146
|
+
home = Path(os.environ.get("DATAFACTORY_TOOLS_HOME") or Path.home() / ".datafactory" / "tools")
|
|
147
|
+
try:
|
|
148
|
+
return json.loads((home / "profile.json").read_text(encoding="utf-8"))
|
|
149
|
+
except (OSError, ValueError):
|
|
150
|
+
return {}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _device() -> str:
|
|
154
|
+
try:
|
|
155
|
+
import torch
|
|
156
|
+
|
|
157
|
+
profile = _profile()
|
|
158
|
+
wanted = profile.get("device", "")
|
|
159
|
+
if wanted == "mps" and getattr(torch.backends, "mps", None) and torch.backends.mps.is_available():
|
|
160
|
+
return "mps"
|
|
161
|
+
if wanted == "cpu" or not torch.cuda.is_available():
|
|
162
|
+
return "cpu"
|
|
163
|
+
# The card the install verified and sized the models for -- not
|
|
164
|
+
# whichever CUDA happens to number 0. With no profile (the tools
|
|
165
|
+
# Python named by DATAFACTORY_TOOLS_PYTHON), the machine decides.
|
|
166
|
+
index = int(profile.get("gpu_index") or 0) if wanted == "cuda" else 0
|
|
167
|
+
return f"cuda:{index}" if index else "cuda"
|
|
168
|
+
except Exception:
|
|
169
|
+
return "cpu"
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _load_sam() -> Any:
|
|
173
|
+
import numpy as np
|
|
174
|
+
from ultralytics import SAM
|
|
175
|
+
|
|
176
|
+
model = SAM(_profile().get("sam") or SAM_WEIGHTS)
|
|
177
|
+
# Build the predictor once, on a tiny dummy image. After this the sidecar
|
|
178
|
+
# only ever uses predictor.set_image + prompt calls: a full model.predict
|
|
179
|
+
# on anything replaces the cached image features (measured: the next
|
|
180
|
+
# prompt then returns an empty mask), so it must never run again. The
|
|
181
|
+
# device here is where the predictor lives from then on.
|
|
182
|
+
model.predict(np.zeros((64, 64, 3), np.uint8), bboxes=[[2, 2, 30, 30]], device=_device(), verbose=False)
|
|
183
|
+
return model
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def _load_world() -> Any:
|
|
187
|
+
from ultralytics import YOLOWorld
|
|
188
|
+
|
|
189
|
+
model = YOLOWorld(_profile().get("world") or WORLD_WEIGHTS)
|
|
190
|
+
# The first set_classes installs and loads CLIP: a 338 MB download and a
|
|
191
|
+
# torch.jit.load that holds the GIL for seconds. Doing it here, on the
|
|
192
|
+
# loader thread, means it happens once while the tool reports "loading",
|
|
193
|
+
# not in the middle of somebody's first prompt.
|
|
194
|
+
_set_world_classes(model, ["object"])
|
|
195
|
+
return model
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def _set_world_classes(model: Any, classes: list[str]) -> None:
|
|
199
|
+
# YOLOWorld.set_classes rebuilds CLIP (338 MB) from disk on every call:
|
|
200
|
+
# measured 6.5 s per new prompt on a GPU. Keep it loaded.
|
|
201
|
+
inner = getattr(model, "model", None)
|
|
202
|
+
if inner is not None and "cache_clip_model" in inspect.signature(inner.set_classes).parameters:
|
|
203
|
+
inner.set_classes(classes, cache_clip_model=True)
|
|
204
|
+
inner.names = list(classes)
|
|
205
|
+
model.predictor = None # what set_classes does: rebuild with the new names
|
|
206
|
+
else: # an Ultralytics without the flag
|
|
207
|
+
model.set_classes(classes)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class _Pending:
|
|
211
|
+
"""One computation in flight, or finished and waiting to be collected."""
|
|
212
|
+
|
|
213
|
+
def __init__(self) -> None:
|
|
214
|
+
self.done = threading.Event()
|
|
215
|
+
self.result: Any = None
|
|
216
|
+
self.error: BaseException | None = None
|
|
217
|
+
self.finished_at = 0.0
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
class SidecarTool:
|
|
221
|
+
def __init__(self) -> None:
|
|
222
|
+
self.sam = _Lazy("SAM 2.1", _load_sam)
|
|
223
|
+
self.world = _Lazy("YOLO-World", _load_world)
|
|
224
|
+
self._world_classes: list[str] = []
|
|
225
|
+
# Inference on a CPU can outlast the wire's timeout. A request waits a
|
|
226
|
+
# while, then answers not_ready and leaves the work running; the
|
|
227
|
+
# retry for the same request collects the finished answer. The wire
|
|
228
|
+
# rule -- one reply per request, promptly -- holds on any hardware.
|
|
229
|
+
self._pending: dict[str, _Pending] = {}
|
|
230
|
+
self._pending_lock = threading.Lock()
|
|
231
|
+
# Models are not thread-safe; one inference at a time.
|
|
232
|
+
self._model_lock = threading.Lock()
|
|
233
|
+
# Encode-ahead. SAM spends ~2.5s on CPU encoding an image and ~0.15s
|
|
234
|
+
# answering each prompt against it, so the image the person has open
|
|
235
|
+
# is encoded once, ahead of the first click. One image only: the
|
|
236
|
+
# features live inside Ultralytics' predictor, and snapshotting them
|
|
237
|
+
# across versions would be brittle.
|
|
238
|
+
self._sam_key: str | None = None
|
|
239
|
+
self._want_key: str | None = None
|
|
240
|
+
self._custom_models: dict[str, Any] = {}
|
|
241
|
+
|
|
242
|
+
# -- protocol ----------------------------------------------------------
|
|
243
|
+
|
|
244
|
+
def describe(self) -> list[dict[str, Any]]:
|
|
245
|
+
# Report the model sizes actually in use, from the install profile.
|
|
246
|
+
profile = _profile()
|
|
247
|
+
specs = []
|
|
248
|
+
for spec in SPECS:
|
|
249
|
+
entry = spec.as_dict()
|
|
250
|
+
if spec.name.startswith("sam."):
|
|
251
|
+
entry["version"] = Path(profile.get("sam") or SAM_WEIGHTS).stem
|
|
252
|
+
elif spec.name == "detect.text":
|
|
253
|
+
entry["version"] = Path(profile.get("world") or WORLD_WEIGHTS).stem
|
|
254
|
+
specs.append(entry)
|
|
255
|
+
return specs
|
|
256
|
+
|
|
257
|
+
def health(self, params: dict[str, Any]) -> dict[str, Any]:
|
|
258
|
+
name = str(params.get("tool") or "")
|
|
259
|
+
lazy = self.world if name.startswith("detect") else self.sam
|
|
260
|
+
return {
|
|
261
|
+
"state": "ready" if lazy.state in ("ready", "cold", "loading") else "unavailable",
|
|
262
|
+
"detail": f"{lazy.name}: {lazy.state}, device {_device()}" + (f" ({lazy.error})" if lazy.error else ""),
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
def prepare(self, params: dict[str, Any]) -> None:
|
|
266
|
+
"""Start loading the model and, for SAM, encoding this image. Returns
|
|
267
|
+
at once: the work happens on a thread, and only the newest request
|
|
268
|
+
is worth finishing -- holding N must not queue up a backlog of
|
|
269
|
+
encodes for images already left behind."""
|
|
270
|
+
name = str(params.get("tool") or "")
|
|
271
|
+
lazy = self.world if name.startswith("detect") else self.sam
|
|
272
|
+
try:
|
|
273
|
+
lazy.get()
|
|
274
|
+
except ToolNotReady:
|
|
275
|
+
pass
|
|
276
|
+
except ToolError:
|
|
277
|
+
return
|
|
278
|
+
if not name.startswith("sam"):
|
|
279
|
+
return
|
|
280
|
+
source = params.get("source") or params.get("image_path")
|
|
281
|
+
if not source or not Path(str(source)).is_file():
|
|
282
|
+
return
|
|
283
|
+
path = Path(str(source))
|
|
284
|
+
key = _image_key(path)
|
|
285
|
+
self._want_key = key
|
|
286
|
+
threading.Thread(target=self._encode_when_ready, args=(path, key), daemon=True,
|
|
287
|
+
name="sam-encode").start()
|
|
288
|
+
|
|
289
|
+
def _encode_when_ready(self, path: Path, key: str) -> None:
|
|
290
|
+
deadline = time.time() + 600 # first use downloads the weights
|
|
291
|
+
while time.time() < deadline:
|
|
292
|
+
if self._want_key != key:
|
|
293
|
+
return # the person moved on; a newer prepare owns the work
|
|
294
|
+
try:
|
|
295
|
+
self.sam.get()
|
|
296
|
+
break
|
|
297
|
+
except ToolNotReady:
|
|
298
|
+
time.sleep(0.5)
|
|
299
|
+
except ToolError:
|
|
300
|
+
return
|
|
301
|
+
with self._model_lock:
|
|
302
|
+
if self._want_key != key or self._sam_key == key:
|
|
303
|
+
return
|
|
304
|
+
try:
|
|
305
|
+
self.sam.model.predictor.set_image(str(path))
|
|
306
|
+
self._sam_key = key
|
|
307
|
+
except Exception:
|
|
308
|
+
self._sam_key = None
|
|
309
|
+
|
|
310
|
+
def invoke(self, params: dict[str, Any]) -> dict[str, Any]:
|
|
311
|
+
name = str(params.get("tool") or "")
|
|
312
|
+
path = _image_path(params)
|
|
313
|
+
key = _request_key(name, path, params)
|
|
314
|
+
|
|
315
|
+
with self._pending_lock:
|
|
316
|
+
self._sweep_locked()
|
|
317
|
+
pending = self._pending.get(key)
|
|
318
|
+
if pending is None:
|
|
319
|
+
pending = _Pending()
|
|
320
|
+
self._pending[key] = pending
|
|
321
|
+
threading.Thread(
|
|
322
|
+
target=self._compute, args=(key, pending, name, path, dict(params)),
|
|
323
|
+
daemon=True, name=f"invoke-{name}",
|
|
324
|
+
).start()
|
|
325
|
+
|
|
326
|
+
if not pending.done.wait(INLINE_WAIT_S):
|
|
327
|
+
raise ToolNotReady(f"{name} is still working on this image", retry_after_ms=1500)
|
|
328
|
+
with self._pending_lock:
|
|
329
|
+
self._pending.pop(key, None)
|
|
330
|
+
if pending.error is not None:
|
|
331
|
+
raise pending.error
|
|
332
|
+
return pending.result
|
|
333
|
+
|
|
334
|
+
def _compute(self, key: str, pending: _Pending, name: str, path: Path, params: dict[str, Any]) -> None:
|
|
335
|
+
try:
|
|
336
|
+
with self._model_lock:
|
|
337
|
+
pending.result = self._run(name, path, params)
|
|
338
|
+
except BaseException as exc: # noqa: BLE001 -- every failure must reach the reply
|
|
339
|
+
pending.error = exc if isinstance(exc, Exception) else ToolError(str(exc) or type(exc).__name__)
|
|
340
|
+
finally:
|
|
341
|
+
pending.finished_at = time.time()
|
|
342
|
+
pending.done.set()
|
|
343
|
+
|
|
344
|
+
def _sweep_locked(self) -> None:
|
|
345
|
+
now = time.time()
|
|
346
|
+
for key in [k for k, p in self._pending.items() if p.done.is_set() and now - p.finished_at > RESULT_TTL_S]:
|
|
347
|
+
del self._pending[key]
|
|
348
|
+
|
|
349
|
+
def _run(self, name: str, path: Path, params: dict[str, Any]) -> dict[str, Any]:
|
|
350
|
+
started = time.time()
|
|
351
|
+
if name == "sam.box":
|
|
352
|
+
box = params.get("box")
|
|
353
|
+
if not (isinstance(box, (list, tuple)) and len(box) == 4):
|
|
354
|
+
raise ToolError("sam.box needs box=[x1, y1, x2, y2]")
|
|
355
|
+
shapes = self._sam(path, params, bboxes=[[float(v) for v in box]])
|
|
356
|
+
elif name == "sam.point":
|
|
357
|
+
points = params.get("points")
|
|
358
|
+
if not (isinstance(points, (list, tuple)) and points):
|
|
359
|
+
raise ToolError("sam.point needs points=[[x, y], ...]")
|
|
360
|
+
shapes = self._sam(path, params, points=[[float(x), float(y)] for x, y in points], labels=[1] * len(points))
|
|
361
|
+
elif name == "detect.model":
|
|
362
|
+
shapes = self._custom(path, params)
|
|
363
|
+
elif name == "detect.text":
|
|
364
|
+
text = str(params.get("text") or "").strip()
|
|
365
|
+
if not text:
|
|
366
|
+
raise ToolError("detect.text needs text")
|
|
367
|
+
shapes = self._world(path, text, float(params.get("min_confidence") or DEFAULT_MIN_CONFIDENCE))
|
|
368
|
+
else:
|
|
369
|
+
raise ToolError(f"no such tool: {name}", code="unknown_tool")
|
|
370
|
+
return {"shapes": shapes, "ms": int((time.time() - started) * 1000)}
|
|
371
|
+
|
|
372
|
+
# -- models ------------------------------------------------------------
|
|
373
|
+
|
|
374
|
+
def _sam(self, path: Path, params: dict[str, Any], **prompt: Any) -> list[dict[str, Any]]:
|
|
375
|
+
model = self.sam.get()
|
|
376
|
+
predictor = model.predictor
|
|
377
|
+
key = _image_key(path)
|
|
378
|
+
if self._sam_key != key:
|
|
379
|
+
# Not encoded ahead (no prepare, or a different image): encode now.
|
|
380
|
+
self._sam_key = None
|
|
381
|
+
predictor.set_image(str(path))
|
|
382
|
+
self._sam_key = key
|
|
383
|
+
results = predictor(**prompt)
|
|
384
|
+
width, height = int(params.get("width") or 0), int(params.get("height") or 0)
|
|
385
|
+
shapes: list[dict[str, Any]] = []
|
|
386
|
+
for result in results:
|
|
387
|
+
if result.masks is None:
|
|
388
|
+
continue
|
|
389
|
+
for mask in result.masks.data:
|
|
390
|
+
array = mask.detach().cpu().numpy()
|
|
391
|
+
# A click on empty floor makes SAM segment the floor -- the
|
|
392
|
+
# whole picture. That is never the object somebody meant, so
|
|
393
|
+
# it reads as "nothing there", which the editor already says.
|
|
394
|
+
if array.size and float((array > 0.5).mean() if array.dtype.kind == "f" else (array > 0).mean()) > 0.9:
|
|
395
|
+
continue
|
|
396
|
+
# Ours, not the model's: the editor needs an outline with a
|
|
397
|
+
# manageable number of draggable vertices, not a raster.
|
|
398
|
+
shape = postprocess.mask_to_shape(array, "object", width, height)
|
|
399
|
+
if shape is not None:
|
|
400
|
+
shapes.append(shape.as_dict())
|
|
401
|
+
return shapes
|
|
402
|
+
|
|
403
|
+
def _custom(self, path: Path, params: dict[str, Any]) -> list[dict[str, Any]]:
|
|
404
|
+
weights = Path(str(params.get("model") or ""))
|
|
405
|
+
if not weights.is_file():
|
|
406
|
+
raise ToolError(f"no model file at {weights}")
|
|
407
|
+
key = f"{weights}|{weights.stat().st_mtime_ns}"
|
|
408
|
+
model = self._custom_models.pop(key, None)
|
|
409
|
+
if model is None:
|
|
410
|
+
from ultralytics import YOLO
|
|
411
|
+
|
|
412
|
+
model = YOLO(str(weights))
|
|
413
|
+
self._custom_models[key] = model
|
|
414
|
+
while len(self._custom_models) > MAX_CUSTOM_MODELS:
|
|
415
|
+
self._custom_models.pop(next(iter(self._custom_models)))
|
|
416
|
+
|
|
417
|
+
conf = float(params.get("conf") or 0.25)
|
|
418
|
+
results = model.predict(str(path), device=_device(), conf=conf, max_det=MAX_DETECTIONS, verbose=False)
|
|
419
|
+
names = getattr(model, "names", {}) or {}
|
|
420
|
+
shapes: list[dict[str, Any]] = []
|
|
421
|
+
for result in results:
|
|
422
|
+
obb = getattr(result, "obb", None)
|
|
423
|
+
if obb is not None and len(obb):
|
|
424
|
+
for corners, score, cls in zip(obb.xyxyxyxy.tolist(), obb.conf.tolist(), obb.cls.tolist()):
|
|
425
|
+
points = [[int(round(x)), int(round(y))] for x, y in corners]
|
|
426
|
+
xs = [p[0] for p in points]
|
|
427
|
+
ys = [p[1] for p in points]
|
|
428
|
+
shapes.append({
|
|
429
|
+
"label": str(names.get(int(cls), int(cls))),
|
|
430
|
+
"bbox": [min(xs), min(ys), max(xs), max(ys)],
|
|
431
|
+
"polygon": points,
|
|
432
|
+
"kind": "obb",
|
|
433
|
+
"confidence": round(float(score), 4),
|
|
434
|
+
})
|
|
435
|
+
continue
|
|
436
|
+
boxes = result.boxes
|
|
437
|
+
if boxes is None:
|
|
438
|
+
continue
|
|
439
|
+
outlines = result.masks.xy if getattr(result, "masks", None) is not None else None
|
|
440
|
+
for index, (xyxy, score, cls) in enumerate(zip(boxes.xyxy.tolist(), boxes.conf.tolist(), boxes.cls.tolist())):
|
|
441
|
+
x1, y1, x2, y2 = (int(round(v)) for v in xyxy)
|
|
442
|
+
polygon = None
|
|
443
|
+
if outlines is not None and index < len(outlines) and len(outlines[index]) >= 3:
|
|
444
|
+
# Already in image coordinates -- unlike masks.data, which
|
|
445
|
+
# is at the model's input size.
|
|
446
|
+
polygon = [[int(round(x)), int(round(y))] for x, y in outlines[index].tolist()]
|
|
447
|
+
shapes.append({
|
|
448
|
+
"label": str(names.get(int(cls), int(cls))),
|
|
449
|
+
"bbox": [x1, y1, x2, y2],
|
|
450
|
+
"polygon": polygon,
|
|
451
|
+
"confidence": round(float(score), 4),
|
|
452
|
+
})
|
|
453
|
+
return shapes
|
|
454
|
+
|
|
455
|
+
def _world(self, path: Path, text: str, min_confidence: float) -> list[dict[str, Any]]:
|
|
456
|
+
model = self.world.get()
|
|
457
|
+
# One prompt, or several separated by commas: "door, handle".
|
|
458
|
+
classes = [part.strip() for part in text.split(",") if part.strip()]
|
|
459
|
+
if classes != self._world_classes:
|
|
460
|
+
_set_world_classes(model, classes)
|
|
461
|
+
self._world_classes = classes
|
|
462
|
+
results = model.predict(
|
|
463
|
+
str(path), device=_device(), conf=min_confidence, max_det=MAX_DETECTIONS, verbose=False
|
|
464
|
+
)
|
|
465
|
+
shapes: list[dict[str, Any]] = []
|
|
466
|
+
for result in results:
|
|
467
|
+
if result.boxes is None:
|
|
468
|
+
continue
|
|
469
|
+
boxes = result.boxes
|
|
470
|
+
for xyxy, conf, cls in zip(boxes.xyxy.tolist(), boxes.conf.tolist(), boxes.cls.tolist()):
|
|
471
|
+
x1, y1, x2, y2 = (int(round(v)) for v in xyxy)
|
|
472
|
+
shapes.append(
|
|
473
|
+
{
|
|
474
|
+
"label": classes[int(cls)] if int(cls) < len(classes) else text,
|
|
475
|
+
"bbox": [x1, y1, x2, y2],
|
|
476
|
+
"polygon": None,
|
|
477
|
+
"confidence": round(float(conf), 4),
|
|
478
|
+
}
|
|
479
|
+
)
|
|
480
|
+
return shapes
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
def _image_key(path: Path) -> str:
|
|
484
|
+
try:
|
|
485
|
+
stamp = path.stat().st_mtime_ns
|
|
486
|
+
except OSError:
|
|
487
|
+
stamp = 0
|
|
488
|
+
return f"{path}|{stamp}"
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
def models_dir() -> Path:
|
|
492
|
+
"""Where weights live. Ultralytics downloads into the working directory,
|
|
493
|
+
so the sidecar runs from here -- otherwise every place it happened to be
|
|
494
|
+
started from collected another 100 MB of .pt files."""
|
|
495
|
+
path = Path(os.environ.get("EASYANNOTATE_MODELS") or Path.home() / ".datafactory" / "models")
|
|
496
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
497
|
+
return path
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
def _request_key(name: str, path: Path, params: dict[str, Any]) -> str:
|
|
501
|
+
"""The same request, asked again, is the same work: the retry that follows
|
|
502
|
+
a not_ready must find what the first call started."""
|
|
503
|
+
prompt = {k: v for k, v in params.items()
|
|
504
|
+
if k in ("box", "points", "text", "min_confidence", "model", "conf")}
|
|
505
|
+
try:
|
|
506
|
+
stamp = path.stat().st_mtime_ns
|
|
507
|
+
except OSError:
|
|
508
|
+
stamp = 0
|
|
509
|
+
raw = json.dumps([name, str(path), stamp, prompt], sort_keys=True, default=str)
|
|
510
|
+
return hashlib.sha1(raw.encode("utf-8")).hexdigest()
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
def _image_path(params: dict[str, Any]) -> Path:
|
|
514
|
+
raw = params.get("image_path")
|
|
515
|
+
if not raw:
|
|
516
|
+
raise ToolError("no image_path in the request")
|
|
517
|
+
path = Path(str(raw))
|
|
518
|
+
if not path.is_file():
|
|
519
|
+
raise ToolError(f"image not found: {path.name}")
|
|
520
|
+
return path
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def main() -> int:
|
|
524
|
+
os.chdir(models_dir())
|
|
525
|
+
return serve(SidecarTool())
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
if __name__ == "__main__":
|
|
529
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Train a detector on an exported project. Run by ``easyannotate train``.
|
|
2
|
+
|
|
3
|
+
Runs in the tools environment as its own process, so torch never enters the
|
|
4
|
+
editor's, and a training run that dies takes nothing else with it. Speaks to
|
|
5
|
+
its parent in two kinds of line among Ultralytics' own output:
|
|
6
|
+
|
|
7
|
+
EASYANNOTATE_EPOCH 3/30 mAP50 0.412 box_loss 1.23
|
|
8
|
+
EASYANNOTATE_RESULT {"best": "...", "metrics": {...}}
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
import json
|
|
15
|
+
import os
|
|
16
|
+
import sys
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def main(argv: list[str] | None = None) -> int:
|
|
21
|
+
parser = argparse.ArgumentParser(prog="easyannotate_tools.train")
|
|
22
|
+
parser.add_argument("--data", required=True)
|
|
23
|
+
parser.add_argument("--model", default="yolov8n.pt")
|
|
24
|
+
parser.add_argument("--epochs", type=int, default=30)
|
|
25
|
+
parser.add_argument("--imgsz", type=int, default=640)
|
|
26
|
+
parser.add_argument("--out", required=True)
|
|
27
|
+
parser.add_argument("--name", required=True)
|
|
28
|
+
parser.add_argument("--device", default="")
|
|
29
|
+
args = parser.parse_args(argv)
|
|
30
|
+
|
|
31
|
+
os.environ.setdefault("YOLO_VERBOSE", "False")
|
|
32
|
+
import torch
|
|
33
|
+
from ultralytics import YOLO
|
|
34
|
+
|
|
35
|
+
# The device the install verified, when the app says; else a guess.
|
|
36
|
+
device = args.device or (0 if torch.cuda.is_available() else "cpu")
|
|
37
|
+
model = YOLO(args.model)
|
|
38
|
+
|
|
39
|
+
def on_epoch(trainer) -> None:
|
|
40
|
+
# A progress line must never be able to kill a training run, and
|
|
41
|
+
# Ultralytics changes these internals between releases (tloss went
|
|
42
|
+
# from a tensor to a dict), so every field is read defensively.
|
|
43
|
+
epoch, epochs = getattr(trainer, "epoch", 0) + 1, getattr(trainer, "epochs", 0)
|
|
44
|
+
if epochs and epoch > epochs:
|
|
45
|
+
return # the final validation of the best weights fires this again
|
|
46
|
+
parts = [f"{epoch}/{epochs or '?'}"]
|
|
47
|
+
try:
|
|
48
|
+
mAP = (trainer.metrics or {}).get("metrics/mAP50(B)")
|
|
49
|
+
if mAP is not None:
|
|
50
|
+
parts.append(f"mAP50 {float(mAP):.3f}")
|
|
51
|
+
except Exception:
|
|
52
|
+
pass
|
|
53
|
+
try:
|
|
54
|
+
loss = trainer.tloss
|
|
55
|
+
if isinstance(loss, dict):
|
|
56
|
+
loss = sum(float(v) for v in loss.values())
|
|
57
|
+
elif hasattr(loss, "sum"):
|
|
58
|
+
loss = float(loss.sum())
|
|
59
|
+
if loss is not None:
|
|
60
|
+
parts.append(f"loss {float(loss):.3f}")
|
|
61
|
+
except Exception:
|
|
62
|
+
pass
|
|
63
|
+
print("EASYANNOTATE_EPOCH " + " ".join(parts), flush=True)
|
|
64
|
+
|
|
65
|
+
model.add_callback("on_fit_epoch_end", on_epoch)
|
|
66
|
+
model.train(
|
|
67
|
+
data=args.data,
|
|
68
|
+
epochs=args.epochs,
|
|
69
|
+
imgsz=args.imgsz,
|
|
70
|
+
project=args.out,
|
|
71
|
+
name=args.name,
|
|
72
|
+
exist_ok=True,
|
|
73
|
+
device=device,
|
|
74
|
+
# Windows cannot fork; dataloader workers there cost more than they buy
|
|
75
|
+
# on the small sets a first model is trained on.
|
|
76
|
+
workers=0 if sys.platform == "win32" else 2,
|
|
77
|
+
plots=False,
|
|
78
|
+
verbose=False,
|
|
79
|
+
)
|
|
80
|
+
best = Path(model.trainer.best)
|
|
81
|
+
final = {k: round(float(v), 4) for k, v in (model.trainer.metrics or {}).items()}
|
|
82
|
+
print("EASYANNOTATE_RESULT " + json.dumps({"best": str(best), "metrics": final}), flush=True)
|
|
83
|
+
return 0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "easyannotate-tools"
|
|
3
|
+
version = "0.4.0"
|
|
4
|
+
description = "Model-backed helpers for EasyAnnotate: SAM 2 masks, text-prompted detection, inference. Runs as a sidecar in its own environment."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = { text = "MIT" }
|
|
8
|
+
authors = [{ name = "Shivpratap Singh Panwar" }]
|
|
9
|
+
|
|
10
|
+
# A separate distribution on purpose. torch's version matrix must never be
|
|
11
|
+
# resolved together with the editor's; this installs into its own venv
|
|
12
|
+
# (`easyannotate tools install`) and talks to the editor over a pipe.
|
|
13
|
+
dependencies = [
|
|
14
|
+
"easyannotate>=0.4.0",
|
|
15
|
+
"ultralytics>=8.3",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Homepage = "https://shivpratapsinghpanwar.github.io/EasyAnnotate/"
|
|
20
|
+
Source = "https://github.com/shivpratapsinghpanwar/EasyAnnotate/tree/main/tools"
|
|
21
|
+
Issues = "https://github.com/shivpratapsinghpanwar/EasyAnnotate/issues"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["hatchling"]
|
|
25
|
+
build-backend = "hatchling.build"
|
|
26
|
+
|
|
27
|
+
[tool.hatch.build.targets.wheel]
|
|
28
|
+
packages = ["easyannotate_tools"]
|