JustinePP commited on
Commit
4e52041
·
verified ·
1 Parent(s): 33c3b9d

Create insightface_template_utils.py

Browse files
Files changed (1) hide show
  1. insightface_template_utils.py +28 -0
insightface_template_utils.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ import insightface
5
+
6
+ model = insightface.app.FaceAnalysis(name="buffalo_l", providers=["CPUExecutionProvider"])
7
+ model.prepare(ctx_id=0)
8
+
9
+ def create_insightface_template(folder_path, output_path):
10
+ embeddings = []
11
+
12
+ for filename in os.listdir(folder_path):
13
+ if filename.lower().endswith((".jpg", ".jpeg", ".png")):
14
+ img_path = os.path.join(folder_path, filename)
15
+ img = cv2.imread(img_path)
16
+ faces = model.get(img)
17
+ if not faces:
18
+ print(f"⚠️ Aucun visage sur {filename}")
19
+ continue
20
+ face = faces[0]
21
+ embeddings.append(face.embedding)
22
+
23
+ if not embeddings:
24
+ raise RuntimeError("❌ Aucun embedding extrait.")
25
+
26
+ template = np.mean(np.array(embeddings), axis=0)
27
+ np.save(output_path, template)
28
+ print(f"✅ Template enregistré dans : {output_path}")