The main class for face verification operations.
from faceverify import FaceVerifier
class FaceVerifier:
def __init__(self, config: Optional[VerifierConfig] = None):
...
| Parameter | Type | Default | Description |
|---|---|---|---|
config |
VerifierConfig |
None |
Configuration object. Uses defaults if not provided. |
# Default configuration
verifier = FaceVerifier()
# Custom configuration
from faceverify.config import VerifierConfig
config = VerifierConfig(
detector_backend="retinaface",
embedding_model="facenet",
threshold=0.70
)
verifier = FaceVerifier(config)
Verify if two images contain the same person.
def verify(
self,
image1: ImageInput,
image2: ImageInput,
) -> VerificationResult
| Parameter | Type | Description |
|---|---|---|
image1 |
str, Path, np.ndarray, PIL.Image |
First image |
image2 |
str, Path, np.ndarray, PIL.Image |
Second image |
VerificationResult - Contains verification outcome, confidence, and metadata.
result = verifier.verify("person1.jpg", "person2.jpg")
if result.verified:
print(f"Same person! Confidence: {result.confidence:.2%}")
else:
print(f"Different people. Similarity: {result.similarity:.4f}")
[!NOTE] The
imageparameter accepts multiple input types: file paths (str or Path), numpy arrays, or PIL Image objects.
Verify multiple image pairs.
def verify_batch(
self,
pairs: List[Tuple[ImageInput, ImageInput]],
parallel: bool = True,
) -> List[VerificationResult]
| Parameter | Type | Default | Description |
|---|---|---|---|
pairs |
List[Tuple] |
required | List of (image1, image2) tuples |
parallel |
bool |
True |
Process in parallel |
pairs = [
("person1_a.jpg", "person1_b.jpg"),
("person2_a.jpg", "person2_b.jpg"),
]
results = verifier.verify_batch(pairs)
for i, result in enumerate(results):
print(f"Pair {i+1}: {result.verified}")
Identify a face against a database (1:N matching).
def identify(
self,
query_image: ImageInput,
database: Union[str, Path, List[ImageInput]],
top_k: int = 5,
threshold: Optional[float] = None,
) -> IdentificationResult
| Parameter | Type | Default | Description |
|---|---|---|---|
query_image |
ImageInput |
required | Image to identify |
database |
str, Path, List |
required | Directory or list of reference images |
top_k |
int |
5 |
Number of top matches to return |
threshold |
float |
None |
Minimum similarity (uses config default) |
result = verifier.identify("unknown.jpg", "./known_faces/", top_k=3)
if result.best_match:
print(f"Best match: {result.best_match['identity']}")
print(f"Similarity: {result.best_match['similarity']:.2%}")
Detect faces in an image.
def detect_faces(
self,
image: ImageInput,
return_all: bool = False,
) -> Union[DetectionResult, List[DetectionResult]]
| Parameter | Type | Default | Description |
|---|---|---|---|
image |
ImageInput |
required | Input image |
return_all |
bool |
False |
Return all faces or just the largest |
# Get largest face
face = verifier.detect_faces("group_photo.jpg")
print(f"Face at: {face.bounding_box}")
# Get all faces
faces = verifier.detect_faces("group_photo.jpg", return_all=True)
print(f"Found {len(faces)} faces")
Extract face embedding from an image.
def extract_embedding(
self,
image: ImageInput,
) -> EmbeddingResult
| Parameter | Type | Description |
|---|---|---|
image |
ImageInput |
Image containing a face |
EmbeddingResult - Contains 512-dimensional embedding vector.
result = verifier.extract_embedding("face.jpg")
print(f"Embedding shape: {result.embedding.shape}")
print(f"Model: {result.model_name}")
[!TIP] Embeddings can be stored and compared later without re-processing images.
Compare two face embeddings directly.
def compare_embeddings(
self,
embedding1: np.ndarray,
embedding2: np.ndarray,
) -> Tuple[float, float]
| Parameter | Type | Description |
|---|---|---|
embedding1 |
np.ndarray |
First embedding vector |
embedding2 |
np.ndarray |
Second embedding vector |
Tuple[float, float] - (similarity, distance)
emb1 = verifier.extract_embedding("face1.jpg").embedding
emb2 = verifier.extract_embedding("face2.jpg").embedding
similarity, distance = verifier.compare_embeddings(emb1, emb2)
print(f"Similarity: {similarity:.4f}")
Get or set the verification threshold.
# Get current threshold
print(verifier.threshold)
# Set new threshold
verifier.threshold = 0.70
[!CAUTION] Threshold must be between 0 and 1. Values outside this range raise
ValueError.