Spaces:
Sleeping
Sleeping
Download modeles/insightface_template_utils.py from VisionShield/VISIONSHIELD: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/insightface_template_utils.py
- Command line
-
hf download hf://spaces/VisionShield/VISIONSHIELD/modeles/insightface_template_utils.py
-
curl -L -o insightface_template_utils.py https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/insightface_template_utils.py
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}") |