VISIONSHIELD / modeles /insightface_template_utils.py
JustinePP's picture
Update modeles/insightface_template_utils.py
d1afe9c verified
Raw History Blame
1.18 kB
import os
import cv2
import numpy as np
import insightface
model = None
def get_model():
global model
if model is None:
# On force le chargement à partir du dossier local
model_dir = os.path.join(os.path.dirname(__file__), "../models")
model = insightface.app.FaceAnalysis(name="buffalo_l", root=model_dir, providers=["CPUExecutionProvider"])
model.prepare(ctx_id=0)
return model
def create_insightface_template(folder_path, output_path):
model = get_model()
embeddings = []
for filename in os.listdir(folder_path):
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
img_path = os.path.join(folder_path, filename)
img = cv2.imread(img_path)
faces = model.get(img)
if not faces:
print(f"Aucun visage sur {filename}")
continue
face = faces[0]
embeddings.append(face.embedding)
if not embeddings:
raise RuntimeError("Aucun embedding extrait.")
template = np.mean(np.array(embeddings), axis=0)
np.save(output_path, template)
print(f"Template enregistré dans : {output_path}")