Spaces:
Configuration error
Configuration error
| import cv2 | |
| import numpy as np | |
| import torch | |
| def preprocess_face(face_bgr: np.ndarray) -> torch.Tensor: | |
| """ | |
| بياخد صورة وجه (BGR من OpenCV) ويرجعها Tensor جاهز للموديل | |
| """ | |
| face = cv2.resize(face_bgr, (112, 112)) | |
| face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) | |
| face = np.transpose(face, (2, 0, 1)) | |
| face_tensor = torch.from_numpy(face).unsqueeze(0).float() | |
| face_tensor.div_(255).sub_(0.5).div_(0.5) | |
| return face_tensor | |
| def cosine_similarity(a: np.ndarray, b: np.ndarray) -> float: | |
| """Cosine similarity بين embedding واحد وembedding تاني""" | |
| a = a.flatten() | |
| b = b.flatten() | |
| denom = (np.linalg.norm(a) * np.linalg.norm(b)) + 1e-8 | |
| return float(np.dot(a, b) / denom) | |