File size: 731 Bytes
c1ff2a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import torch
import numpy as np

from app.config import EDGEFACE_WEIGHTS, EDGEFACE_MODEL_NAME
from app.services.edgeface_model import get_model
from app.utils.preprocess import preprocess_face


# ---------------- تحميل الموديل مرة واحدة بس ----------------
_net = get_model(EDGEFACE_MODEL_NAME, fp16=False)
_net.load_state_dict(torch.load(EDGEFACE_WEIGHTS, map_location="cpu"))
_net.eval()


def get_embedding(face_bgr: np.ndarray) -> np.ndarray:
    """بياخد صورة وجه (crop بصيغة BGR) ويرجع الـ embedding بتاعه (512,)"""
    face_tensor = preprocess_face(face_bgr)


    with torch.no_grad():
        emb = _net(face_tensor).squeeze().numpy().astype(np.float32)
    return emb