Class FaceDetector
Finds the faces in an image: their boxes, the model's confidence and the landmarks the model places on each (eyes, nose tip, mouth, ears). Detection runs locally on the CPU with a face detection model (HasFaceDetection), with no language model involved.
public sealed class FaceDetector
- Inheritance
-
FaceDetector
- Inherited Members
Examples
using LMKit.Media.Image;
using LMKit.Model;
using LMKit.Vision;
var detector = new FaceDetector(LM.LoadFromModelID("blazeface-full-range"));
using var image = ImageBuffer.Load("team.jpg");
foreach (DetectedFace face in detector.Detect(image))
{
Console.WriteLine($"{face.Bounds.Left:0},{face.Bounds.Top:0} {face.Bounds.Width:0}x{face.Bounds.Height:0} ({face.Confidence:P0})");
}
Remarks
A detection model sees a face only above a fraction of the image it reads. To find small faces (a group photo, a crowd), the detector reads the whole image, then overlapping tiles of it, as fine as MinimumFaceSize needs, and merges what every window saw into one detection per face. Lowering MinimumFaceSize finds smaller faces and costs more windows.
The detector is model-agnostic: everything a model needs (its input, its decoder, its landmarks, its default thresholds) is read from the model file. An instance is safe to use from several threads at once.
Constructors
- FaceDetector(LM)
Creates a face detector over a face detection model.
Properties
- MaximumFaces
The most faces to return, the most confident first; 0 (the default) returns them all.
- MinimumConfidence
The confidence, from 0 to 1, below which a detection is not a face. Defaults to the value the model's authors recommend, read from the model file. Lower it to miss fewer faces at the cost of more false detections, for instance before a person reviews them.
- MinimumFaceSize
The smallest face to find, as a fraction of the image's shorter side; defaults to 0.02 (a face 20 pixels wide on a 1000 pixel tall photo). Smaller faces need finer tiles: lowering it finds them and reads more windows. 0 reads the whole image alone, the fastest.
- Model
The face detection model.
- OverlapThreshold
The overlap (intersection over union) above which two detections are one face and merge. Defaults to the model's own value, read from the model file. Raise it to keep faces that overlap in a dense crowd apart.
Methods
- Detect(ImageBuffer, CancellationToken)
Finds the faces in an image.
- DetectAsync(ImageBuffer, CancellationToken)
Finds the faces in an image, on a worker thread.