Drives insightface's model_zoo directly — no FaceAnalysis wrapper. FaceAnalysis is a thin 50-line orchestration (glob for ONNX files in ` /models/ /`, route each through `model_zoo.get_model`, build a `{taskname: model}` dict, then loop per-face at inference). We reimpleme
| 198 | |
| 199 | |
| 200 | class InsightFaceEngine: |
| 201 | """Drives insightface's model_zoo directly — no FaceAnalysis wrapper. |
| 202 | |
| 203 | FaceAnalysis is a thin 50-line orchestration (glob for ONNX files |
| 204 | in `<root>/models/<name>/`, route each through `model_zoo.get_model`, |
| 205 | build a `{taskname: model}` dict, then loop per-face at inference). |
| 206 | We reimplement the same loop here so we can: |
| 207 | |
| 208 | 1. Load packs from whatever directory LocalAI's gallery extracted |
| 209 | them into — flat (buffalo_l/s/sc — ONNX at `<dir>/*.onnx`) or |
| 210 | nested (buffalo_m/antelopev2 — ONNX at `<dir>/<name>/*.onnx`) |
| 211 | without needing a specific layout on disk. |
| 212 | 2. Skip insightface's built-in auto-download entirely: weight |
| 213 | delivery is LocalAI's gallery `files:` job now, checksum- |
| 214 | verified and cached alongside every other managed model. |
| 215 | |
| 216 | The actual inference classes (RetinaFace, ArcFaceONNX, Attribute, |
| 217 | Landmark) stay in insightface — we only reimplement the ~50 lines |
| 218 | of glue around them. |
| 219 | """ |
| 220 | |
| 221 | def __init__(self) -> None: |
| 222 | self.models: dict[str, Any] = {} |
| 223 | self.det_model: Any = None |
| 224 | self.model_pack: str = "buffalo_l" |
| 225 | self.det_size: tuple[int, int] = (640, 640) |
| 226 | self.det_thresh: float = 0.5 |
| 227 | self._providers: list[str] = ["CPUExecutionProvider"] |
| 228 | self._antispoofer: Antispoofer | None = None |
| 229 | |
| 230 | def prepare(self, options: dict[str, str]) -> None: |
| 231 | import glob |
| 232 | import os |
| 233 | |
| 234 | from insightface.model_zoo import model_zoo |
| 235 | |
| 236 | self.model_pack = options.get("model_pack", "buffalo_l") |
| 237 | self.det_size = _parse_det_size(options.get("det_size", "640x640")) |
| 238 | self.det_thresh = float(options.get("det_thresh", "0.5")) |
| 239 | self._antispoofer = _build_antispoofer(options, options.get("_model_dir")) |
| 240 | |
| 241 | pack_dir = _locate_insightface_pack(options, self.model_pack) |
| 242 | if pack_dir is None: |
| 243 | raise ValueError( |
| 244 | f"no insightface pack '{self.model_pack}' found — install via " |
| 245 | f"`local-ai models install insightface-{self.model_pack.replace('_', '-')}`" |
| 246 | ) |
| 247 | |
| 248 | onnx_files = sorted(glob.glob(os.path.join(pack_dir, "*.onnx"))) |
| 249 | # When the pack extracts flat into a shared models directory it |
| 250 | # mixes with ONNX files from other backends (opencv face engine, |
| 251 | # MiniFASNet antispoof, WeSpeaker voice embedding, other buffalo |
| 252 | # packs installed earlier). Feeding those into model_zoo.get_model() |
| 253 | # blows up inside insightface's router — it assumes a 4-D NCHW |
| 254 | # input and indexes `input_shape[2]` on tensors that aren't shaped |
| 255 | # like a face model, raising IndexError. For the upstream packs we |
| 256 | # know the exact ONNX manifest; scoping to it makes the load |
| 257 | # deterministic (without it, det_10g.onnx from buffalo_l sorts |