File size: 1,210 Bytes
c628352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import uuid
from pathlib import Path

import numpy as np
import pytest
from PIL import Image

from faceverification.core.image_processor import ImageProcessor
from faceverification.core.vectordb import VectorDB

pytestmark = [pytest.mark.integration, pytest.mark.e2e]

IMAGES_DIR = Path(__file__).parent / "images"


def test_real_image_embedding_can_be_enrolled_and_matched_in_vectordb():
    processor = ImageProcessor(device="cpu")
    vector_db = VectorDB(
        distance_metric="l2",
        name_collection=f"test_real_image_faces_{uuid.uuid4().hex}",
    )
    anchor_image = Image.open(IMAGES_DIR / "person_anchor.jpg").convert("RGB")
    positive_image = Image.open(IMAGES_DIR / "person_positive.jpg").convert("RGB")

    anchor_embedding = processor.get_embedding(anchor_image).cpu().numpy()
    positive_embedding = processor.get_embedding(positive_image).cpu().numpy()
    vector_db.add_embedding(anchor_embedding, {"name": "Ada"})

    metadata, distance = vector_db.query_embedding(
        positive_embedding,
        threshold=1.08,
        n_results=1,
    )

    assert metadata == {"name": "Ada"}
    assert distance == pytest.approx(np.linalg.norm(positive_embedding - anchor_embedding))