FaceEmbedding class
Generates face embeddings (identity vectors) from aligned face crops.
Uses the MobileFaceNet model to produce 192-dimensional embedding vectors that represent a face's identity. These embeddings can be compared using cosine similarity to determine if two faces belong to the same person.
The model expects 112×112 RGB face images as input. For best results, faces should be aligned (eyes horizontal, centered, face fills the frame).
Usage
final embedding = await FaceEmbedding.create();
// Get embedding from an aligned face crop
final vector = await embedding(alignedFaceCrop);
print('Embedding dimension: ${vector.length}'); // 192
// Compare two embeddings
final similarity = FaceEmbedding.cosineSimilarity(vector1, vector2);
if (similarity > 0.6) {
print('Same person!');
}
// Clean up
embedding.dispose();
Integration with FaceDetector
For the full face recognition pipeline, use FaceDetector.getFaceEmbedding which handles face detection, alignment, and embedding extraction:
final detector = FaceDetector();
await detector.initialize();
final faces = await detector.detectFacesFromBytes(imageBytes);
final embedding = await detector.getFaceEmbedding(faces.first, imageBytes);
Properties
- embeddingDimension → int
-
The dimension of the output embedding vector.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- inputHeight → int
-
The expected input height for the embedding model (112 pixels).
no setter
- inputWidth → int
-
The expected input width for the embedding model (112 pixels).
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
Mat faceCrop, {Float32List? buffer}) → Future< Float32List> - Generates a face embedding from an aligned face crop using cv.Mat.
-
dispose(
) → void - Releases all TensorFlow Lite resources held by this model.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
cosineSimilarity(
Float32List a, Float32List b) → double - Computes the cosine similarity between two embedding vectors.
-
create(
{InterpreterOptions? options, PerformanceConfig? performanceConfig}) → Future< FaceEmbedding> - Creates and initializes a face embedding model instance.
-
createCompiledFromBuffer(
Uint8List modelBytes, {Set< Accelerator> accelerators = const {Accelerator.gpu, Accelerator.cpu}, Precision precision = Precision.fp16}) → Future<FaceEmbedding> - Creates a face embedding model backed by LiteRT CompiledModel.
-
createFromBuffer(
Uint8List modelBytes, {PerformanceConfig? performanceConfig}) → Future< FaceEmbedding> - Creates a face embedding model from pre-loaded model bytes.
-
euclideanDistance(
Float32List a, Float32List b) → double - Computes the Euclidean (L2) distance between two embedding vectors.