-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_model_insightface.py
More file actions
72 lines (60 loc) · 2.28 KB
/
face_model_insightface.py
File metadata and controls
72 lines (60 loc) · 2.28 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# face_model_insightface.py
import argparse
import cv2
import numpy as np
from insightface.app import FaceAnalysis
class InsightFaceModel:
def __init__(
self,
model_pack: str = "buffalo_l",
det_size=(640, 640),
det_thresh: float = 0.5,
use_gpu: bool = False,
):
if use_gpu:
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
ctx_id = 0
else:
providers = ["CPUExecutionProvider"]
ctx_id = -1
self.app = FaceAnalysis(name=model_pack, providers=providers)
self.app.prepare(ctx_id=ctx_id, det_thresh=det_thresh, det_size=det_size)
def embed_image(self, image_path: str) -> np.ndarray:
img = cv2.imread(image_path)
if img is None:
raise ValueError(f"Cannot read image: {image_path}")
faces = self.app.get(img)
if not faces:
raise ValueError(f"No face detected in: {image_path}")
def area(f):
x1, y1, x2, y2 = f.bbox
return float((x2 - x1) * (y2 - y1))
best = max(faces, key=area)
emb = np.asarray(best.normed_embedding, dtype=np.float32)
return emb
def count_faces(self, image_path: str) -> int:
img = cv2.imread(image_path)
if img is None:
raise ValueError(f"Cannot read image: {image_path}")
faces = self.app.get(img)
return len(faces)
if __name__ == "__main__":
import argparse
from pathlib import Path
ap = argparse.ArgumentParser()
ap.add_argument("--img", default="dataset/T0001/images/1.jpeg", help="Path to an image file")
ap.add_argument("--model_pack", default="buffalo_l")
ap.add_argument("--use_gpu", action="store_true")
ap.add_argument("--det_thresh", type=float, default=0.5)
args = ap.parse_args()
img_path = Path(args.img)
if not img_path.exists():
raise FileNotFoundError(f"Image not found: {img_path.resolve()}")
model = InsightFaceModel(
model_pack=args.model_pack,
det_thresh=args.det_thresh,
use_gpu=args.use_gpu
)
emb = model.embed_image(str(img_path))
print("Embedding shape:", emb.shape)
print("Embedding (first 10 vals):", emb[:10])