Views
No views yet
| File | Description |
|---|---|
svc_model.pkl | Trained SVM classifier on FaceNet embeddings (105 classes) |
centroids.npy | Class centroids (mean embeddings per identity) |
classes.npy | List of identity labels (class order used by the SVM) |
README.md | Model documentation |
1from huggingface_hub import hf_hub_download
2import joblib
3import numpy as np
4
5REPO_ID = "AI-Solutions-KK/face_recognition"
6
7svc_path = hf_hub_download(REPO_ID, "svc_model.pkl")
8centroids_path = hf_hub_download(REPO_ID, "centroids.npy")
9classes_path = hf_hub_download(REPO_ID, "classes.npy")
10
11svc_model = joblib.load(svc_path)
12centroids = np.load(centroids_path)
13class_names = np.load(classes_path, allow_pickle=True)
14
15print("Model loaded successfully. Classes:", len(class_names))1from huggingface_hub import hf_hub_download
2import joblib, numpy as np, cv2, torch
3from facenet_pytorch import InceptionResnetV1, MTCNN
4
5REPO_ID = "AI-Solutions-KK/face_recognition"
6
7# Load classifier + metadata
8svc_path = hf_hub_download(REPO_ID, "svc_model.pkl")
9classes_path = hf_hub_download(REPO_ID, "classes.npy")
10
11obj = joblib.load(svc_path)
12svc_model = obj["clf"]
13normalizer = obj["norm"]
14label_encoder = obj["le"]
15class_names = np.load(classes_path, allow_pickle=True)
16
17# Load FaceNet backbone + face detector
18device = "cpu"
19mtcnn = MTCNN(keep_all=False, device=device)
20facenet = InceptionResnetV1(pretrained="vggface2").eval().to(device)
21
22def get_embedding(img_path: str) -> np.ndarray:
23 img_bgr = cv2.imread(img_path)
24 if img_bgr is None:
25 raise ValueError(f"Could not read image: {img_path}")
26 img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
27 face = mtcnn(img_rgb)
28 if face is None:
29 raise ValueError("No face detected.")
30 if face.dim() == 3:
31 face = face.unsqueeze(0)
32 with torch.no_grad():
33 emb = facenet(face.to(device)).cpu().numpy()
34 return emb
35
36def predict_face(img_path: str):
37 emb = get_embedding(img_path)
38 emb_norm = normalizer.transform(emb)
39 probs = svc_model.predict_proba(emb_norm)[0]
40 idx = np.argmax(probs)
41 label = label_encoder.inverse_transform([idx])[0]
42 confidence = float(probs[idx])
43 return label, confidence
44
45# -------- RUN ----------
46img_path = "test.jpg"
47label, prob = predict_face(img_path)
48print("Predicted Identity:", label)
49print("Confidence Score:", prob)AI-Solutions-KK/face_recognition_dataset.svc_model.pkl, classes.npy, centroids.npyroot/class_name/image.jpg)svc_model.pklclasses.npycentroids.npy