landmarkerio-server 0.5.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.
- landmarkerio/__init__.py +56 -0
- landmarkerio/_version.py +24 -0
- landmarkerio/asset.py +71 -0
- landmarkerio/cache.py +344 -0
- landmarkerio/collection.py +70 -0
- landmarkerio/default_templates/ibug68.yml +85 -0
- landmarkerio/http_auth/__init__.py +0 -0
- landmarkerio/http_auth/sanic_httpauth.py +354 -0
- landmarkerio/http_auth/sanic_httpauth_compat.py +306 -0
- landmarkerio/landmark.py +148 -0
- landmarkerio/mode.py +5 -0
- landmarkerio/response.py +20 -0
- landmarkerio/scripts/__init__.py +0 -0
- landmarkerio/scripts/lmio.py +153 -0
- landmarkerio/scripts/lmiocache.py +79 -0
- landmarkerio/scripts/lmioconvert.py +39 -0
- landmarkerio/scripts/lmioserve.py +98 -0
- landmarkerio/servers/__init__.py +0 -0
- landmarkerio/servers/api/__init__.py +0 -0
- landmarkerio/servers/api/v2.py +118 -0
- landmarkerio/servers/auth.py +35 -0
- landmarkerio/servers/serve.py +77 -0
- landmarkerio/template.py +193 -0
- landmarkerio/test/__init__.py +0 -0
- landmarkerio/test/test_server.py +154 -0
- landmarkerio/test/test_templates.py +16 -0
- landmarkerio/types.py +4 -0
- landmarkerio/utils.py +15 -0
- landmarkerio_server-0.5.0.dist-info/METADATA +157 -0
- landmarkerio_server-0.5.0.dist-info/RECORD +34 -0
- landmarkerio_server-0.5.0.dist-info/WHEEL +5 -0
- landmarkerio_server-0.5.0.dist-info/entry_points.txt +5 -0
- landmarkerio_server-0.5.0.dist-info/licenses/LICENSE +27 -0
- landmarkerio_server-0.5.0.dist-info/top_level.txt +1 -0
landmarkerio/__init__.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
class FileExt(object):
|
|
2
|
+
lm = ".ljson"
|
|
3
|
+
template = ".yml"
|
|
4
|
+
collection = ".txt"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class CacheFile(object):
|
|
8
|
+
texture = "texture.jpg"
|
|
9
|
+
image = "image.json"
|
|
10
|
+
thumbnail = "thumbnail.jpg"
|
|
11
|
+
mesh_tmp = "mesh.raw.tmp"
|
|
12
|
+
mesh = "mesh.raw.gz"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Server(object):
|
|
16
|
+
endpoint = "/api/v2/"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class Endpoints(object):
|
|
20
|
+
mode = "mode"
|
|
21
|
+
images = "images"
|
|
22
|
+
collections = "collections"
|
|
23
|
+
landmarks = "landmarks"
|
|
24
|
+
meshes = "meshes"
|
|
25
|
+
templates = "templates"
|
|
26
|
+
points = "points"
|
|
27
|
+
trilist = "trilist"
|
|
28
|
+
tcoords = "tcoords"
|
|
29
|
+
normals = "normals"
|
|
30
|
+
textures = "textures"
|
|
31
|
+
thumbnail = "thumbnails"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Mimetype(object):
|
|
35
|
+
json = "application/json"
|
|
36
|
+
jpeg = "image/jpeg"
|
|
37
|
+
binary = "application/octet-stream"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
LM_DIRNAME = "lmiolandmarks"
|
|
41
|
+
TEMPLATE_DINAME = ".lmiotemplates"
|
|
42
|
+
|
|
43
|
+
ALL_COLLECTION_ID = "all"
|
|
44
|
+
|
|
45
|
+
dirs_in_dir = lambda path: sorted([p for p in path.iterdir() if p.is_dir()])
|
|
46
|
+
|
|
47
|
+
try:
|
|
48
|
+
from ._version import __version__
|
|
49
|
+
except ImportError: # a source checkout that was never installed
|
|
50
|
+
from importlib.metadata import PackageNotFoundError
|
|
51
|
+
from importlib.metadata import version as _version
|
|
52
|
+
|
|
53
|
+
try:
|
|
54
|
+
__version__ = _version("landmarkerio-server")
|
|
55
|
+
except PackageNotFoundError:
|
|
56
|
+
__version__ = "unknown"
|
landmarkerio/_version.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# file generated by vcs-versioning
|
|
2
|
+
# don't change, don't track in version control
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"__version_tuple__",
|
|
8
|
+
"version",
|
|
9
|
+
"version_tuple",
|
|
10
|
+
"__commit_id__",
|
|
11
|
+
"commit_id",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
version: str
|
|
15
|
+
__version__: str
|
|
16
|
+
__version_tuple__: tuple[int | str, ...]
|
|
17
|
+
version_tuple: tuple[int | str, ...]
|
|
18
|
+
commit_id: str | None
|
|
19
|
+
__commit_id__: str | None
|
|
20
|
+
|
|
21
|
+
__version__ = version = '0.5.0'
|
|
22
|
+
__version_tuple__ = version_tuple = (0, 5, 0)
|
|
23
|
+
|
|
24
|
+
__commit_id__ = commit_id = 'gf20a7974b'
|
landmarkerio/asset.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
import os
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Sequence
|
|
5
|
+
|
|
6
|
+
from landmarkerio import CacheFile
|
|
7
|
+
from landmarkerio.types import PathLike
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ImageAdapter(abc.ABC):
|
|
11
|
+
@abc.abstractmethod
|
|
12
|
+
def texture_path(self, asset_id: str) -> Path:
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
@abc.abstractmethod
|
|
16
|
+
def thumbnail_path(self, asset_id: str) -> Path:
|
|
17
|
+
pass
|
|
18
|
+
|
|
19
|
+
@abc.abstractmethod
|
|
20
|
+
def asset_ids(self) -> Sequence[str]:
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class MeshAdapter(abc.ABC):
|
|
25
|
+
@abc.abstractmethod
|
|
26
|
+
def asset_ids(self) -> Sequence[str]:
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
@abc.abstractmethod
|
|
30
|
+
def mesh_path(self, asset_id: str) -> Path:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CacheAdapter:
|
|
35
|
+
def __init__(self, cache_dir: PathLike) -> None:
|
|
36
|
+
self.cache_dir = Path(os.path.abspath(os.path.expanduser(cache_dir)))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ImageCacheAdapter(CacheAdapter, ImageAdapter):
|
|
40
|
+
def __init__(self, cache_dir):
|
|
41
|
+
CacheAdapter.__init__(self, cache_dir)
|
|
42
|
+
self._image_asset_ids = [
|
|
43
|
+
a.parent.name
|
|
44
|
+
for a in self.cache_dir.glob(os.path.join("*", CacheFile.texture))
|
|
45
|
+
if a.parent.parent == self.cache_dir
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
def texture_path(self, asset_id: str) -> Path:
|
|
49
|
+
return self.cache_dir / asset_id / CacheFile.texture
|
|
50
|
+
|
|
51
|
+
def thumbnail_path(self, asset_id: str) -> Path:
|
|
52
|
+
return self.cache_dir / asset_id / CacheFile.thumbnail
|
|
53
|
+
|
|
54
|
+
def asset_ids(self) -> Sequence[str]:
|
|
55
|
+
return self._image_asset_ids
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class MeshCacheAdapter(CacheAdapter, MeshAdapter):
|
|
59
|
+
def __init__(self, cache_dir: PathLike) -> None:
|
|
60
|
+
CacheAdapter.__init__(self, cache_dir)
|
|
61
|
+
self._mesh_asset_ids = [
|
|
62
|
+
a.parent.name
|
|
63
|
+
for a in self.cache_dir.glob(os.path.join("*", CacheFile.mesh))
|
|
64
|
+
if a.parent.parent == self.cache_dir
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
def mesh_path(self, asset_id: str) -> Path:
|
|
68
|
+
return self.cache_dir / asset_id / CacheFile.mesh
|
|
69
|
+
|
|
70
|
+
def asset_ids(self) -> Sequence[str]:
|
|
71
|
+
return self._mesh_asset_ids
|
landmarkerio/cache.py
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import gzip
|
|
2
|
+
import os
|
|
3
|
+
import os.path as p
|
|
4
|
+
import shutil
|
|
5
|
+
import struct
|
|
6
|
+
import time
|
|
7
|
+
from functools import partial
|
|
8
|
+
from os.path import abspath, expanduser
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Callable, Dict, Iterable, Optional, Sequence, Tuple, cast
|
|
11
|
+
|
|
12
|
+
import menpo
|
|
13
|
+
import numpy as np
|
|
14
|
+
from loguru import logger
|
|
15
|
+
from PIL import Image as PILImage
|
|
16
|
+
|
|
17
|
+
from landmarkerio import CacheFile
|
|
18
|
+
from landmarkerio.mode import UnexpectedMode
|
|
19
|
+
from landmarkerio.types import PathLike
|
|
20
|
+
|
|
21
|
+
PathAssetIDT = Sequence[Tuple[PathLike, str]]
|
|
22
|
+
IdentifierF = Callable[[PathLike], str]
|
|
23
|
+
CacheF = Callable[[PathLike, str], None]
|
|
24
|
+
DirCacheF = Callable[[PathLike, PathLike, str], None]
|
|
25
|
+
CacherF = Callable[[CacheF, PathAssetIDT], None]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def filename_as_asset_id(fp: PathLike) -> str:
|
|
29
|
+
# simply return the filename as the asset_id
|
|
30
|
+
return Path(fp).stem
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def filepath_as_asset_id_under_dir(asset_dir: PathLike) -> IdentifierF:
|
|
34
|
+
# find the filepath under asset_dir and return the full path as an asset id
|
|
35
|
+
asset_dir = Path(abspath(expanduser(asset_dir)))
|
|
36
|
+
|
|
37
|
+
def path_as_asset_id(fp: PathLike) -> str:
|
|
38
|
+
return "__".join(Path(fp).relative_to(asset_dir).parts)
|
|
39
|
+
|
|
40
|
+
return path_as_asset_id
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def build_asset_mapping(
|
|
44
|
+
identifier_f: IdentifierF, asset_paths_iter: Iterable[PathLike]
|
|
45
|
+
) -> Dict[str, Path]:
|
|
46
|
+
asset_mapping: Dict[str, Path] = {}
|
|
47
|
+
for path in asset_paths_iter:
|
|
48
|
+
asset_id = identifier_f(path)
|
|
49
|
+
if asset_id in asset_mapping:
|
|
50
|
+
raise ValueError(
|
|
51
|
+
f"asset_id {asset_id} is not unique - links to {asset_mapping[asset_id]} and {path}"
|
|
52
|
+
)
|
|
53
|
+
asset_mapping[asset_id] = Path(path)
|
|
54
|
+
|
|
55
|
+
return asset_mapping
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _menpo3d():
|
|
59
|
+
"""menpo3d is only needed to serve meshes: image servers do not require it."""
|
|
60
|
+
try:
|
|
61
|
+
import menpo3d
|
|
62
|
+
except ImportError as error:
|
|
63
|
+
raise ImportError(
|
|
64
|
+
"Serving meshes needs menpo3d: pip install 'landmarkerio-server[mesh]'"
|
|
65
|
+
) from error
|
|
66
|
+
return menpo3d
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def mesh_paths(asset_dir: PathLike, glob_pattern: str) -> Sequence[Path]:
|
|
70
|
+
return _menpo3d().io.mesh_paths(Path(asset_dir) / glob_pattern)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def image_paths(asset_dir: PathLike, glob_pattern: str) -> Sequence[Path]:
|
|
74
|
+
return menpo.io.image_paths(Path(asset_dir) / glob_pattern)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def build_glob_pattern(ext_str: str, recursive: bool) -> str:
|
|
78
|
+
file_glob = f"*{ext_str}"
|
|
79
|
+
if recursive:
|
|
80
|
+
return os.path.join("**", file_glob)
|
|
81
|
+
else:
|
|
82
|
+
return file_glob
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def ensure_asset_dir(asset_dir: PathLike) -> Path:
|
|
86
|
+
asset_dir = Path(p.abspath(p.expanduser(asset_dir)))
|
|
87
|
+
if not asset_dir.is_dir():
|
|
88
|
+
raise ValueError(f"{asset_dir} is not a directory")
|
|
89
|
+
logger.debug("assets: {}", asset_dir)
|
|
90
|
+
return asset_dir
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def cache_asset(
|
|
94
|
+
cache_dir: PathLike,
|
|
95
|
+
cache_f: Callable[[PathLike, PathLike, str], None],
|
|
96
|
+
path: PathLike,
|
|
97
|
+
asset_id: str,
|
|
98
|
+
) -> None:
|
|
99
|
+
asset_cache_dir = Path(cache_dir) / asset_id
|
|
100
|
+
asset_cache_dir.mkdir(parents=True, exist_ok=True)
|
|
101
|
+
cache_f(cache_dir, path, asset_id)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def cache_image(cache_dir: PathLike, path: PathLike, asset_id: str) -> None:
|
|
105
|
+
r"""Actually cache this asset_id."""
|
|
106
|
+
img = menpo.io.import_image(path)
|
|
107
|
+
_cache_image_for_id(cache_dir, asset_id, img)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _cache_image_for_id(
|
|
111
|
+
cache_dir: PathLike, asset_id: str, img: menpo.image.Image
|
|
112
|
+
) -> None:
|
|
113
|
+
asset_cache_dir = Path(cache_dir) / asset_id
|
|
114
|
+
texture_path = asset_cache_dir / CacheFile.texture
|
|
115
|
+
thumbnail_path = asset_cache_dir / CacheFile.thumbnail
|
|
116
|
+
img_path: Path = getattr(img, "path")
|
|
117
|
+
|
|
118
|
+
# WebGL only allows textures of maximum dimension 4096
|
|
119
|
+
ratio = 4096.0 / np.array(img.shape)
|
|
120
|
+
if np.any(ratio < 1):
|
|
121
|
+
# the largest axis of the img could be too big for older browsers.
|
|
122
|
+
# Give a warning.
|
|
123
|
+
logger.warning(
|
|
124
|
+
"Warning: {} has shape {}. Dims larger than 4096 may have "
|
|
125
|
+
"issues rendering in older browsers.",
|
|
126
|
+
asset_id,
|
|
127
|
+
img.shape,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# 2. Save out the image
|
|
131
|
+
# menpo < 0.13 exports with the formats that Pillow had registered so far, which after
|
|
132
|
+
# loading a PNG is PNG alone (menpo #904): load them all
|
|
133
|
+
PILImage.init()
|
|
134
|
+
if img_path.suffix == ".jpg":
|
|
135
|
+
# Original was a jpg that was suitable, save it
|
|
136
|
+
shutil.copyfile(img_path, texture_path)
|
|
137
|
+
else:
|
|
138
|
+
# Original wasn't a jpg or was too big - make it so
|
|
139
|
+
menpo.io.export_image(img, texture_path)
|
|
140
|
+
|
|
141
|
+
# 3. Save out the thumbnail
|
|
142
|
+
save_jpg_thumbnail_file(img, thumbnail_path)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def save_jpg_thumbnail_file(
|
|
146
|
+
img: menpo.image.Image, path: PathLike, width: int = 640
|
|
147
|
+
) -> None:
|
|
148
|
+
ip = img.as_PILImage()
|
|
149
|
+
w, h = ip.size
|
|
150
|
+
h2w = h * 1.0 / w
|
|
151
|
+
ips = ip.resize((width, int(h2w * width)))
|
|
152
|
+
ips.save(path, quality=20, format="jpeg")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def cache_mesh(cache_dir: PathLike, path: PathLike, asset_id: str) -> None:
|
|
156
|
+
mesh = _menpo3d().io.import_mesh(path)
|
|
157
|
+
if isinstance(mesh, menpo.shape.mesh.TexturedTriMesh):
|
|
158
|
+
_cache_image_for_id(cache_dir, asset_id, mesh.texture)
|
|
159
|
+
_cache_mesh_for_id(cache_dir, asset_id, mesh)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _cache_mesh_for_id(
|
|
163
|
+
cache_dir: PathLike, asset_id: str, mesh: menpo.shape.TriMesh
|
|
164
|
+
) -> None:
|
|
165
|
+
asset_cache_dir = Path(cache_dir) / asset_id
|
|
166
|
+
mesh_tmp_path = asset_cache_dir / CacheFile.mesh_tmp
|
|
167
|
+
mesh_path = asset_cache_dir / CacheFile.mesh
|
|
168
|
+
_export_raw_mesh(mesh_tmp_path, mesh)
|
|
169
|
+
|
|
170
|
+
with mesh_tmp_path.open("rb") as f_in:
|
|
171
|
+
with gzip.open(mesh_path, "wb", compresslevel=1) as f_out:
|
|
172
|
+
f_out.writelines(f_in)
|
|
173
|
+
|
|
174
|
+
mesh_tmp_path.unlink()
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _export_raw_mesh(path: PathLike, m: menpo.shape.TriMesh) -> None:
|
|
178
|
+
normals = False # for now we are just not exporting normals.
|
|
179
|
+
is_textured = hasattr(m, "tcoords")
|
|
180
|
+
with Path(path).open("wb") as f:
|
|
181
|
+
f.write(struct.pack("IIII", m.n_tris, is_textured, normals, False))
|
|
182
|
+
m.points[m.trilist].astype(np.float32).tofile(f)
|
|
183
|
+
if normals:
|
|
184
|
+
m.vertex_normals[m.trilist].astype(np.float32).tofile(f)
|
|
185
|
+
if is_textured:
|
|
186
|
+
m.tcoords.points[m.trilist].astype(np.float32).tofile(f)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def ensure_cache_dir(cache_dir: PathLike) -> Path:
|
|
190
|
+
cache_dir = Path(p.abspath(p.expanduser(cache_dir)))
|
|
191
|
+
if not cache_dir.is_dir():
|
|
192
|
+
logger.warning("The cache dir does not exist - creating...")
|
|
193
|
+
cache_dir.mkdir(parents=True, exist_ok=True)
|
|
194
|
+
logger.debug("cache: {}", cache_dir)
|
|
195
|
+
return cache_dir
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def serial_cacher(cache: CacheF, path_asset_id: PathAssetIDT) -> None:
|
|
199
|
+
for i, (path, asset_id) in enumerate(path_asset_id):
|
|
200
|
+
logger.debug("Caching {}/{} - {}", i + 1, len(path_asset_id), asset_id)
|
|
201
|
+
cache(path, asset_id)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def parallel_cacher(
|
|
205
|
+
cache: CacheF, path_asset_id: PathAssetIDT, n_jobs: int = -1
|
|
206
|
+
) -> None:
|
|
207
|
+
from joblib import Parallel, delayed
|
|
208
|
+
|
|
209
|
+
Parallel(n_jobs=n_jobs, verbose=5)(
|
|
210
|
+
delayed(cache)(path, asset_id) for path, asset_id in path_asset_id
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def build_cache(
|
|
215
|
+
cacher_f: CacherF,
|
|
216
|
+
asset_path_f: Callable[[PathLike, str], Sequence[Path]],
|
|
217
|
+
cache_f: DirCacheF,
|
|
218
|
+
identifier_f: IdentifierF,
|
|
219
|
+
asset_dir: PathLike,
|
|
220
|
+
cache_dir: PathLike,
|
|
221
|
+
recursive: bool = False,
|
|
222
|
+
ext: Optional[str] = None,
|
|
223
|
+
glob: Optional[str] = None,
|
|
224
|
+
) -> Tuple[Path, Dict[str, Path]]:
|
|
225
|
+
# 1. Ensure the asset_dir and cache_dir are present.
|
|
226
|
+
asset_dir = ensure_asset_dir(asset_dir)
|
|
227
|
+
cache_dir = ensure_cache_dir(cache_dir)
|
|
228
|
+
|
|
229
|
+
if recursive:
|
|
230
|
+
logger.debug("assets dir will be searched recursively.")
|
|
231
|
+
|
|
232
|
+
if ext is not None:
|
|
233
|
+
ext_str = "." + ext
|
|
234
|
+
logger.debug("only assets of type {} will be loaded.", ext_str)
|
|
235
|
+
else:
|
|
236
|
+
ext_str = ""
|
|
237
|
+
|
|
238
|
+
if glob is None:
|
|
239
|
+
# Figure out the glob pattern and save it
|
|
240
|
+
glob_ptn = build_glob_pattern(ext_str, recursive)
|
|
241
|
+
else:
|
|
242
|
+
glob_ptn = glob
|
|
243
|
+
|
|
244
|
+
logger.debug('Using glob: "{}"', glob_ptn)
|
|
245
|
+
|
|
246
|
+
# Construct a mapping from id's to file paths
|
|
247
|
+
asset_id_to_paths = build_asset_mapping(
|
|
248
|
+
identifier_f, asset_path_f(asset_dir, glob_ptn)
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
# Check cache for what needs to be updated
|
|
252
|
+
asset_ids = set(asset_id_to_paths.keys())
|
|
253
|
+
cached = set(os.listdir(cache_dir))
|
|
254
|
+
uncached = asset_ids - cached
|
|
255
|
+
|
|
256
|
+
logger.debug("{} assets need to be added to the cache", len(uncached))
|
|
257
|
+
cache: CacheF = cast(CacheF, partial(cache_asset, cache_dir, cache_f))
|
|
258
|
+
path_asset_id = [(asset_id_to_paths[a_id], a_id) for a_id in uncached]
|
|
259
|
+
|
|
260
|
+
start = time.time()
|
|
261
|
+
cacher_f(cache, path_asset_id)
|
|
262
|
+
elapsed = time.time() - start
|
|
263
|
+
if uncached:
|
|
264
|
+
logger.debug("{} assets cached in {:.0f} seconds", len(uncached), elapsed)
|
|
265
|
+
|
|
266
|
+
return cache_dir, asset_id_to_paths
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def build_image_cache(
|
|
270
|
+
identifier_f: IdentifierF,
|
|
271
|
+
asset_dir: PathLike,
|
|
272
|
+
cache_dir: PathLike,
|
|
273
|
+
recursive: bool = False,
|
|
274
|
+
ext: Optional[str] = None,
|
|
275
|
+
glob: Optional[str] = None,
|
|
276
|
+
parallel: bool = True,
|
|
277
|
+
) -> Tuple[Path, Dict[str, Path]]:
|
|
278
|
+
cacher_f: CacherF
|
|
279
|
+
if parallel:
|
|
280
|
+
cacher_f = parallel_cacher
|
|
281
|
+
else:
|
|
282
|
+
cacher_f = serial_cacher
|
|
283
|
+
|
|
284
|
+
return build_cache(
|
|
285
|
+
cacher_f=cacher_f,
|
|
286
|
+
asset_path_f=image_paths,
|
|
287
|
+
cache_f=cache_image,
|
|
288
|
+
identifier_f=identifier_f,
|
|
289
|
+
asset_dir=asset_dir,
|
|
290
|
+
cache_dir=cache_dir,
|
|
291
|
+
recursive=recursive,
|
|
292
|
+
ext=ext,
|
|
293
|
+
glob=glob,
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def build_mesh_cache(
|
|
298
|
+
identifier_f: IdentifierF,
|
|
299
|
+
asset_dir: PathLike,
|
|
300
|
+
cache_dir: PathLike,
|
|
301
|
+
recursive: bool = False,
|
|
302
|
+
ext: Optional[str] = None,
|
|
303
|
+
glob: Optional[str] = None,
|
|
304
|
+
parallel: bool = True,
|
|
305
|
+
) -> Tuple[Path, Dict[str, Path]]:
|
|
306
|
+
cacher_f: CacherF
|
|
307
|
+
if parallel:
|
|
308
|
+
cacher_f = parallel_cacher
|
|
309
|
+
else:
|
|
310
|
+
cacher_f = serial_cacher
|
|
311
|
+
|
|
312
|
+
return build_cache(
|
|
313
|
+
cacher_f=cacher_f,
|
|
314
|
+
asset_path_f=mesh_paths,
|
|
315
|
+
cache_f=cache_mesh,
|
|
316
|
+
identifier_f=identifier_f,
|
|
317
|
+
asset_dir=asset_dir,
|
|
318
|
+
cache_dir=cache_dir,
|
|
319
|
+
recursive=recursive,
|
|
320
|
+
ext=ext,
|
|
321
|
+
glob=glob,
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def cache_assets(
|
|
326
|
+
mode: str,
|
|
327
|
+
identifier_f: IdentifierF,
|
|
328
|
+
asset_dir: PathLike,
|
|
329
|
+
cache_dir: PathLike,
|
|
330
|
+
recursive: bool = False,
|
|
331
|
+
ext: Optional[str] = None,
|
|
332
|
+
glob: Optional[str] = None,
|
|
333
|
+
parallel: bool = True,
|
|
334
|
+
) -> Tuple[Path, Dict[str, Path]]:
|
|
335
|
+
if mode == "image":
|
|
336
|
+
cache_builder = partial(build_image_cache, parallel=parallel)
|
|
337
|
+
elif mode == "mesh":
|
|
338
|
+
cache_builder = partial(build_mesh_cache, parallel=parallel)
|
|
339
|
+
else:
|
|
340
|
+
raise UnexpectedMode(mode)
|
|
341
|
+
|
|
342
|
+
return cache_builder(
|
|
343
|
+
identifier_f, asset_dir, cache_dir, recursive=recursive, ext=ext, glob=glob
|
|
344
|
+
)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import abc
|
|
2
|
+
from os.path import abspath, expanduser
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Sequence
|
|
5
|
+
|
|
6
|
+
from loguru import logger
|
|
7
|
+
|
|
8
|
+
from landmarkerio import ALL_COLLECTION_ID, FileExt, dirs_in_dir
|
|
9
|
+
from landmarkerio.types import PathLike
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MissingCollection(ValueError):
|
|
13
|
+
def __init__(self, collection_id: str) -> None:
|
|
14
|
+
super().__init__(f"Cannot find collection with id '{collection_id}'")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_collection(path: PathLike) -> Sequence[str]:
|
|
18
|
+
with Path(path).open("rt") as f:
|
|
19
|
+
collection = [line.strip() for line in f.readlines()]
|
|
20
|
+
return [c for c in collection if c]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class CollectionAdapter(abc.ABC):
|
|
24
|
+
@abc.abstractmethod
|
|
25
|
+
def collection_ids(self) -> Sequence[str]:
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
@abc.abstractmethod
|
|
29
|
+
def collection(self, collection_id: str) -> Sequence[str]:
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
def __str__(self) -> str:
|
|
33
|
+
a = f"Serving {len(self.collection_ids())} collection(s):"
|
|
34
|
+
b = "\n".join(
|
|
35
|
+
f" - {c} ({len(self.collection(c))} assets)" for c in self.collection_ids()
|
|
36
|
+
)
|
|
37
|
+
return f"{a}\n{b}"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class FileCollectionAdapter(CollectionAdapter):
|
|
41
|
+
def __init__(self, collection_dir: PathLike) -> None:
|
|
42
|
+
self.collection_dir = Path(abspath(expanduser(collection_dir)))
|
|
43
|
+
logger.debug("Found collections: {}", self.collection_dir)
|
|
44
|
+
collection_paths = self.collection_dir.glob("*" + FileExt.collection)
|
|
45
|
+
self._collection = {c.stem: load_collection(c) for c in collection_paths}
|
|
46
|
+
|
|
47
|
+
def collection_ids(self) -> Sequence[str]:
|
|
48
|
+
return list(self._collection.keys())
|
|
49
|
+
|
|
50
|
+
def collection(self, collection_id: str) -> Sequence[str]:
|
|
51
|
+
try:
|
|
52
|
+
return self._collection[collection_id]
|
|
53
|
+
except KeyError:
|
|
54
|
+
raise MissingCollection(collection_id)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class AllCacheCollectionAdapter(CollectionAdapter):
|
|
58
|
+
def __init__(self, cache_dir: PathLike) -> None:
|
|
59
|
+
cache_dir = Path(abspath(expanduser(cache_dir)))
|
|
60
|
+
self._collection = [p.name for p in dirs_in_dir(cache_dir)]
|
|
61
|
+
self._collection_ids = [ALL_COLLECTION_ID]
|
|
62
|
+
|
|
63
|
+
def collection_ids(self) -> Sequence[str]:
|
|
64
|
+
return self._collection_ids
|
|
65
|
+
|
|
66
|
+
def collection(self, collection_id: str) -> Sequence[str]:
|
|
67
|
+
if collection_id == ALL_COLLECTION_ID:
|
|
68
|
+
return self._collection
|
|
69
|
+
else:
|
|
70
|
+
raise MissingCollection(collection_id)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
groups:
|
|
2
|
+
- connectivity:
|
|
3
|
+
- 0 1
|
|
4
|
+
- 1 2
|
|
5
|
+
- 2 3
|
|
6
|
+
- 3 4
|
|
7
|
+
- 4 5
|
|
8
|
+
- 5 6
|
|
9
|
+
- 6 7
|
|
10
|
+
- 7 8
|
|
11
|
+
- 8 9
|
|
12
|
+
- 9 10
|
|
13
|
+
- 10 11
|
|
14
|
+
- 11 12
|
|
15
|
+
- 12 13
|
|
16
|
+
- 13 14
|
|
17
|
+
- 14 15
|
|
18
|
+
- 15 16
|
|
19
|
+
label: jaw
|
|
20
|
+
points: 17
|
|
21
|
+
- connectivity:
|
|
22
|
+
- 0 1
|
|
23
|
+
- 1 2
|
|
24
|
+
- 2 3
|
|
25
|
+
- 3 4
|
|
26
|
+
label: left_eyebrow
|
|
27
|
+
points: 5
|
|
28
|
+
- connectivity:
|
|
29
|
+
- 0 1
|
|
30
|
+
- 1 2
|
|
31
|
+
- 2 3
|
|
32
|
+
- 3 4
|
|
33
|
+
label: right_eyebrow
|
|
34
|
+
points: 5
|
|
35
|
+
- connectivity:
|
|
36
|
+
- 0 1
|
|
37
|
+
- 1 2
|
|
38
|
+
- 2 3
|
|
39
|
+
- 4 5
|
|
40
|
+
- 5 6
|
|
41
|
+
- 6 7
|
|
42
|
+
- 7 8
|
|
43
|
+
label: nose
|
|
44
|
+
points: 9
|
|
45
|
+
- connectivity:
|
|
46
|
+
- 0 1
|
|
47
|
+
- 1 2
|
|
48
|
+
- 2 3
|
|
49
|
+
- 3 4
|
|
50
|
+
- 4 5
|
|
51
|
+
- 5 0
|
|
52
|
+
label: left_eye
|
|
53
|
+
points: 6
|
|
54
|
+
- connectivity:
|
|
55
|
+
- 0 1
|
|
56
|
+
- 1 2
|
|
57
|
+
- 2 3
|
|
58
|
+
- 3 4
|
|
59
|
+
- 4 5
|
|
60
|
+
- 5 0
|
|
61
|
+
label: right_eye
|
|
62
|
+
points: 6
|
|
63
|
+
- connectivity:
|
|
64
|
+
- 0 1
|
|
65
|
+
- 1 2
|
|
66
|
+
- 2 3
|
|
67
|
+
- 3 4
|
|
68
|
+
- 4 5
|
|
69
|
+
- 5 6
|
|
70
|
+
- 6 7
|
|
71
|
+
- 7 8
|
|
72
|
+
- 8 9
|
|
73
|
+
- 9 10
|
|
74
|
+
- 10 11
|
|
75
|
+
- 11 0
|
|
76
|
+
- 12 13
|
|
77
|
+
- 13 14
|
|
78
|
+
- 14 15
|
|
79
|
+
- 15 16
|
|
80
|
+
- 16 17
|
|
81
|
+
- 17 18
|
|
82
|
+
- 18 19
|
|
83
|
+
- 19 12
|
|
84
|
+
label: mouth
|
|
85
|
+
points: 20
|
|
File without changes
|