Spaces:
Sleeping
Sleeping
Download modeles/insightface_template_utils.py from VisionShield/VISIONSHIELD: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/refs%2Fpr%2F1/modeles/insightface_template_utils.py
- Command line
-
hf download hf://spaces/VisionShield/VISIONSHIELD@refs/pr/1/modeles/insightface_template_utils.py
-
curl -L -o insightface_template_utils.py https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/refs%2Fpr%2F1/modeles/insightface_template_utils.py
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}") |