Image vectors and faces
src.plugin_api.media lends a plugin the image models of the application, so that it can compare
images without loading models of its own. These functions read images the plugin already holds, each
a path on this server or a PIL image, and name no media of the user. To look at a media the user
picked, take its file first with media_local_path(user, ref) or copy_media_file(user, ref, target)
(see Working with the user's media), which reach only what the user may
see. Never pass them a path taken from a request.
images and faces are lists, even of one item: a single string, path or image in their place
raises TypeError before anything is read. An item that is neither a path nor a PIL image raises
TypeError, and an image that cannot be read, missing, truncated or not an image, raises ValueError
named by its place in the list ("images[2] cannot be read as an image", or faces[2] for the image
of a face), never by its path; FileNotFoundError only ever means that a model is missing.
detect_faces and embed_faces read a path as it is displayed, its EXIF orientation applied, so the
boxes and landmarks of a phone JPEG are in the frame it shows in; they read a PIL image as it is
given, its pixels as they are. Both put a transparent image on white first, as the media loader does.
embed_images reads an image the way the gallery search indexes it, orientation not applied and
transparency dropped, so that its vectors compare with the gallery's.
| Function | Does |
|---|---|
image_embedder() | The application's image embedder, the SigLIP model of the gallery search: model (the media_vision_model setting, google/siglip-base-patch16-224 by default), slug (the namespace of its vectors: compare only vectors of one slug), device (the media_vision_device setting), available (its weights are on this server) and loaded (it is in memory now). available is the test the gallery itself makes, a weights folder that is not empty: the core keeps no sign on disk that a download of the weights finished, so a download stopped half way counts too, and embed_images then fails when the model loads |
embed_images(images) | One vector per image from that embedder, L2-normalized so that the dot product of two vectors is their cosine. SigLIP compares the overall look of two images (framing, setting, light, clothes); it does not tell whether two faces are one person |
face_detector() | The face detector, OpenCV's YuNet: model (its file name) and available (that file is in a detection_bbox folder of an online model root) |
detect_faces(images, min_score=0.5) | The faces YuNet finds in each image, on the CPU, best first: for each, x, y, width and height in pixels of the image, score (0 to 1, faces under min_score left out) and landmarks, five [x, y] points: right eye, left eye, nose tip, right and left corners of the mouth, as the face sees them. Each image is read at its own size, so that the small face of a wide shot is found too. min_score is a number from 0 to 1; anything else, a boolean included, raises ValueError before an image is read |
face_embedder() | The face recognition model, AuraFace: model (its file name) and available (that file is in face_recognition/auraface of the home model root and onnxruntime imports) |
embed_faces(faces) | The identity vector of each face, from AuraFace on the CPU through ONNX Runtime. Each face is a pair: an image and the five landmarks detect_faces found for that face in it. The face is aligned on those landmarks to the 112×112 ArcFace template before it is read, so a turned or tilted face is read upright. The vectors (512 values) are L2-normalized: two faces of one person have a high cosine, two people a cosine near 0. A profile reads poorly; keep the faces YuNet scores high, with both eyes in view. A face that is not such a pair, landmarks that are not five points, and landmarks that give no alignment raise ValueError before the model loads |
The functions never download a model. embed_images raises FileNotFoundError while the embedder's
weights are not on this server, even when the gallery may fetch them (media_vision_auto_download).
The face models are files an administrator puts in place by hand; a function whose file is missing
raises FileNotFoundError, and its face_detector() or face_embedder() says available: false.
A file is only used when its bytes are the published file's, so a corrupted copy, another model
saved under the same name or a file the server cannot read counts as missing:
| Model | File | Where | License | Bytes | SHA-256 |
|---|---|---|---|---|---|
| YuNet, from opencv/opencv_zoo | face_detection_yunet_2023mar.onnx | any detection_bbox folder of the model roots | MIT | 232,589 | 8f2383e4dd3cfbb4553ea8718107fc0423210dc964f9f4280604804ed2552fa4 |
| AuraFace, from fal/AuraFace-v1 | glintr100.onnx | face_recognition/auraface of the home model root | Apache-2.0 | 260,694,151 | a7933ea5330113b01c9b60351d8f4c33003f145d8470ac5f0e52ee2effe25c60 |
InsightFace's model packs also hold a glintr100.onnx, under a license for non-commercial use;
that file is not AuraFace's and is not used. A file's size and digest are checked once for each
state of the file (its size and modification time), so the first call after a file is put in place
reads it through. face_detector() and face_embedder() make that check too: the first of them
to run after the file changes reads it through, 260 MB for AuraFace, so call them from a thread as
well, like the functions that run the models. A call that arrives while a file is being checked waits
for that check instead of reading the file again; the check of one file does not wait for the other.
embed_faces aligns each face on the 112×112 ArcFace template of InsightFace, the five points
arcface_dst of python-package/insightface/utils/face_align.py in
deepinsight/insightface:
the eyes, the nose tip and the corners of the mouth of an upright face in a 112×112 crop. InsightFace
releases that code under the MIT License; only its models and training data are non-commercial.
The embedder and AuraFace go through the model cache of the application for each call, like any
other model: they count against its memory budget, stay cached between calls and are evicted under
memory pressure or when an administrator clears the cache. AuraFace's ONNX Runtime session is
cached under the key media/face-identity/auraface and uses no GPU. YuNet, 232 KB, is loaded for
each call of detect_faces and released at its end. The calls block while the models run, so call
them from a thread (await asyncio.to_thread(embed_images, paths)), never on the event loop. A call
made inside a generation, from a step hook for example, holds its own lease on its model and leaves
the generation's lease alone.
The core keeps nothing of what these functions read or return: no image, no face box, no vector,
and no log of them. What a plugin stores is its own. On an instance with several users, the vectors
a plugin computes from people's own pictures are biometric data, and the identity vectors of
embed_faces identify a person: whoever runs the instance is responsible for that. A plugin that
keeps them scopes them to the user, deletes them with the user's data, and keeps them only as long as
the feature that needs them.