Faces: detect, count and match
Find and count the faces in a photo, check whether two pictures show the same person, and store a short face id per face, locally in PHP with InsightFace packs and no PyTorch.
Tell whether a picture has a face in it, count the faces, check whether two pictures show the same person, and turn each face into a short face id you can store in a database and match against later — all from one InsightFace model pack.
Setup#
vendor/bin/loves-ai setup face
vendor/bin/loves-ai pull immich-app/buffalo_l
This runner needs no PyTorch. It is onnxruntime, numpy and Pillow, so setup face downloads ~40 MB (115 MB unpacked) against 218 MB for text-to-image, and a photo with three faces takes about half a second on a CPU. The same runner serves both face and face-match.
Supported models#
Pull an InsightFace pack. They hold two ONNX graphs: a detector that finds faces and their landmarks, and a recognizer that turns each aligned face into 512 numbers.
| Model | Pull size | Notes |
|---|---|---|
immich-app/buffalo_l | ~182 MB | The default recommendation; what the numbers below were measured with |
immich-app/buffalo_s | ~113 MB | Smaller and faster, a little less accurate |
immich-app/antelopev2 | ~360 MB | Larger; some report it finding fewer faces than buffalo_l |
These are the InsightFace models, republished for Immich in a layout the runner reads. Check the model card for the licence before shipping one in a product: InsightFace releases its pretrained models for non-commercial research use.
Faces are personal data almost everywhere. Storing face ids or embeddings for identifiable people brings GDPR, BIPA and similar obligations with it, whatever the model licence says.
From the command line#
vendor/bin/loves-ai face immich-app/buffalo_l crew.jpg
vendor/bin/loves-ai face immich-app/buffalo_l crew.jpg --count
🙂 Looking for faces with immich-app/buffalo_l… Should be quick — no time for a whole cookie 🍪
🎉 Found 3 faces in crew.jpg
👤 1 91.3% confident, 80×114 px at 201,254
v1:256:34ae1b70385ab1df212d044a4c36cd46421b2bc1cb5b80ae01fbf87709288745
👤 2 91.0% confident, 75×108 px at 426,117
v1:256:22bc1f64552ec40f8a1d5c9e3b7740d1e8a2f05b6c3d9417ab28e6f5103c7d82
👤 3 89.6% confident, 81×115 px at 657,266
v1:256:4a6e0df6c0617b2f94e8d13a5026cb8f7d41920e3b6a5c8df0e71248a93b6054
--count only runs the detector, which skips loading the 166 MB recognizer — use it when all you need is "is there a face here?".
Options#
| Option | Meaning |
|---|---|
--count |
Only report how many faces there are; skips recognition, and is much faster |
--embeddings |
Include each face's 512 numbers; only useful together with --json |
--json |
Print the result as one JSON object, for piping into a backend |
--min-score=N |
Detector confidence from 0 to 1 (default: min_score in config/face.php) |
--max-faces=N |
Keep only the N most confident faces (default: all of them) |
--hash-bits=N |
Bits in a face id: 64, 128, 256 or 512 (default: hash_bits in config/face.php) |
--det-size=PX |
Size the image is resized to for detection (default: detection_size in config/face.php); ignored by a detector built for one fixed size, as the InsightFace packs are |
--device=DEVICE |
cpu, cuda, coreml… (default: the best available) |
--log-file=PATH |
Append the runner's output to this file |
--debug |
Show the runner's output while looking |
JSON output#
For a backend, --json prints one object and nothing else, so it pipes straight into your code:
vendor/bin/loves-ai face immich-app/buffalo_l crew.jpg --json --embeddings
{"model":"immich-app/buffalo_l","image":"crew.jpg","count":3,"faces":[
{"id":"v1:256:34ae1b70…","score":0.9125,"box":[201,254,281,368],
"landmarks":[[232,302],[268,303],[255,323],[229,335],[266,336]],
"embedding":[0.035172,0.021544,-0.029648, …]}]}
Matching two pictures#
face-match compares the faces in two images and reports how similar they are:
vendor/bin/loves-ai face-match immich-app/buffalo_l passport.jpg selfie.jpg
🤝 Comparing the two faces with immich-app/buffalo_l… Quick one — hold on to your cookie 🍪
🎉 Same person — 68.2% similar (cut-off 40.0%)
| Option | Meaning |
|---|---|
--threshold=N |
Similarity at which the two count as the same person, from -1 to 1 (default: threshold in config/face.php) |
--min-score=N |
Detector confidence from 0 to 1 (default: min_score in config/face.php) |
--det-size=PX |
Size images are resized to for detection (default: detection_size in config/face.php) |
--device=DEVICE |
cpu, cuda, coreml… (default: the best available) |
--json |
Print the result as one JSON object, for piping into a backend |
--log-file=PATH |
Append the runner's output to this file |
--debug |
Show the runner's output while matching |
When an image holds several faces, only its most confident one is matched, and the result says so. The exit code is 0 whether or not it is a match, so read the output to find out which.
From PHP#
use PhpLovesAi\Runner\Face;
// Finds the runner and the pulled model in the project's .local directory by itself.
$face = new Face();
$face->hasFace('immich-app/buffalo_l', storage_path('app/upload.jpg')); // true
$face->countFaces('immich-app/buffalo_l', storage_path('app/crew.jpg')); // 3
foreach ($face->analyze('immich-app/buffalo_l', storage_path('app/crew.jpg')) as $found) {
$found->id; // "v1:256:34ae1b70…" — store this per face
$found->embedding; // the 512 numbers it is derived from
$found->box; // [left, top, right, bottom] in pixels
$found->score; // 0.9125
$found->width(); // 80
}
hasFace() and countFaces() only run the detector; analyze() also runs the recognizer. Pass withEmbeddings: false to get the ids without the 512 numbers.
Comparing two pictures#
$match = $face->compare(
model: 'immich-app/buffalo_l',
imagePath: storage_path('app/passport.jpg'),
otherImagePath: storage_path('app/selfie.jpg'),
threshold: 0.45,
);
$match->samePerson; // true
$match->similarity; // 0.6819
$match->threshold; // 0.45 — stored results still say what they meant
$match->wasAmbiguous(); // true when either picture held more than one face
When a picture holds several faces, compare() uses the most confident one in each, and facesInA / facesInB say how many there were.
Face ids, and how to compare them#
A face id is a fingerprint, not a primary key. It is a locality-sensitive hash of the embedding: similar faces get similar bit strings, so two photos of one person give two different ids that differ in only some of their bits. Never compare them with === — compare the distance:
use PhpLovesAi\Runner\FaceId;
FaceId::distance($rowA->face_id, $rowB->face_id); // 68 -> same person
FaceId::distance($rowA->face_id, $rowC->face_id); // 109 -> someone else
Measured with buffalo_l at the default 256 bits:
| Pair | Cosine similarity | Distance |
|---|---|---|
| Same person, two different photos | 0.68 | 68 / 256 |
| Same photo at ⅓ scale, JPEG quality 60 | 0.72 | 63 / 256 |
| Same photo rotated 5°, or 35% brighter | 0.97 | 16–20 / 256 |
| Two different people | 0.01–0.02 | 109–122 / 256 |
So a cut-off around 90 separates them, and unrelated faces centre on half the bits (the expected distance is bits × θ / π, where θ is the angle between the embeddings).
Measure it on your own images before relying on it — only you know what a wrong match costs. The same goes for threshold: config/face.php ships 0.40 as a starting point, not a safe default.
Two ids only mean anything side by side when they came from the same model, the same hash_bits and the same hash version — FaceId::distance() refuses to compare ids that disagree, rather than returning a meaningless number. hash_bits lives in config/face.php rather than in each call for that reason: changing it invalidates every id you have stored.
On the CPU provider, ids are reproducible across machines: one photo hashed byte-identically on macOS arm64, Linux arm64 and Linux x86_64, so a stored id still matches after a move from a developer's Mac to a container on either chip, and a queue worker agrees with a web server. They are not identical across execution providers — CPU and CoreML came out a bit or two apart on the same image — which is one more reason to compare by distance, never equality.
Fewer bits is a shorter string but a blunter one. At 64 bits the same-person and different-person distances overlap, which is why the default is 256 — a 71-character id, prefix included:
$table->string('face_id', 72)->index(); // "v1:256:" + 64 hex characters
For an exact answer, keep the embedding too and compare that: FaceId::similarity() does it in PHP, and a vector column (pgvector, or any vector store) does it at scale. The usual shape is to shortlist candidates by id distance, then re-rank the shortlist by cosine similarity on the embeddings.
Errors#
analyze() throws:
ImageNotFoundExceptionwhen the image does not existBinaryNotInstalledExceptionwhensetup facehas not been runModelNotFoundExceptionwhen the model was not pulled yetUnsupportedModelExceptionwhen the model is not an InsightFace pack\InvalidArgumentExceptionfor an unknown bit count or an out-of-range scoreRunFailedException, with the runner's error output, when the run itself fails
compare() additionally throws NoFaceDetectedException when either picture has no face in it, and FaceId::distance() throws InvalidFaceIdException for a malformed id, or two that cannot be compared.
Configuration#
Defaults come from config/face.php:
| Key | Default | Overridden by |
|---|---|---|
hash_bits | 256 | --hash-bits |
threshold | 0.40 | --threshold |
min_score | 0.5 | --min-score |
detection_size | 640 | --det-size |
log_file | null (output discarded) | --log-file |