MCPcopy
hub / github.com/mudler/LocalAI / prepare

Method prepare

backend/python/insightface/engines.py:230–305  ·  view source on GitHub ↗
(self, options: dict[str, str])

Source from the content-addressed store, hash-verified

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
258 # before det_500m.onnx from buffalo_sc and silently wins).
259 manifest = _KNOWN_PACK_MANIFESTS.get(self.model_pack)
260 if manifest is not None:
261 scoped = [f for f in onnx_files if os.path.basename(f) in manifest]
262 if scoped:
263 onnx_files = scoped
264 if not onnx_files:
265 raise ValueError(f"no ONNX files in pack directory: {pack_dir}")
266
267 # CUDAExecutionProvider is picked automatically by onnxruntime-gpu
268 # when available; falling back to CPU keeps the CPU-only image
269 # working. ctx_id=0 means "first GPU if any, else CPU".
270 self._providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
271
272 self.models = {}
273 skipped: list[tuple[str, str]] = []
274 for onnx_file in onnx_files:
275 try:
276 m = model_zoo.get_model(onnx_file, providers=self._providers)
277 except Exception as err:
278 # Foreign ONNX (wrong rank/shape, non-insightface model) —
279 # older insightface versions raise IndexError / ValueError
280 # instead of returning None. Keep loading the rest.
281 skipped.append((os.path.basename(onnx_file), str(err)))
282 continue
283 if m is None:
284 skipped.append((os.path.basename(onnx_file), "unknown taskname"))
285 continue
286 # First occurrence of each taskname wins (matches FaceAnalysis).
287 if m.taskname not in self.models:

Callers

nothing calls this directly

Calls 8

_parse_det_sizeFunction · 0.85
_build_antispooferFunction · 0.85
_locate_insightface_packFunction · 0.85
replaceMethod · 0.80
appendMethod · 0.80
getMethod · 0.45
prepareMethod · 0.45
itemsMethod · 0.45

Tested by

no test coverage detected