VISIONSHIELD / modeles /insightface_template_utils.py
JustinePP's picture
Update modeles/insightface_template_utils.py
5441197 verified
Raw History Blame Contribute Delete
1.67 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
print("On force le chargement à partir du dossier local")
model_dir = os.path.join(os.path.dirname(__file__), "../")
print("On charge le model")
model = insightface.app.FaceAnalysis(name="buffalo_l", root=model_dir, providers=["CPUExecutionProvider"])
print("On prépare le model")
model.prepare(ctx_id=0)
return model
def create_insightface_template(folder_path, output_path):
print(folder_path,output_path)
print("on récupére le model")
model = get_model()
embeddings = []
print("On liste les fichiers")
for filename in os.listdir(folder_path):
print("fichier : ",filename)
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
img_path = os.path.join(folder_path, filename)
print("on lis l'image img_path : ",img_path)
img = cv2.imread(img_path)
print("on récupére les visages dans l'image: ",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.")
print("on fait la moyenne")
template = np.mean(np.array(embeddings), axis=0)
print("on sauvegarde le template")
np.save(output_path, template)
print(f"Template enregistré dans : {output_path}")