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

Create liveness_check_folder.py

Browse files
Files changed (1) hide show
  1. liveness_check_folder.py +50 -0
liveness_check_folder.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import numpy as np
4
+ from insightface.app import FaceAnalysis
5
+
6
+ def check_liveness_on_folder(folder_path, frame_interval=1, yaw_thresh=5, pitch_thresh=5):
7
+ model = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider'])
8
+ model.prepare(ctx_id=0)
9
+
10
+ poses = []
11
+ image_files = sorted([
12
+ f for f in os.listdir(folder_path)
13
+ if f.lower().endswith((".jpg", ".jpeg", ".png"))
14
+ ])
15
+
16
+ for idx, filename in enumerate(image_files):
17
+ if idx % frame_interval != 0:
18
+ continue
19
+
20
+ img_path = os.path.join(folder_path, filename)
21
+ img = cv2.imread(img_path)
22
+ if img is None:
23
+ print(f"⚠️ Image illisible : {filename}")
24
+ continue
25
+
26
+ faces = model.get(img)
27
+ if not faces:
28
+ print(f"❌ Aucun visage détecté sur {filename}")
29
+ continue
30
+
31
+ pose = faces[0].pose # [yaw, pitch, roll]
32
+ poses.append(pose)
33
+
34
+ if len(poses) < 3:
35
+ print("⛔ Pas assez de données pour vérifier la vivacité.")
36
+ return False
37
+
38
+ poses = np.array(poses)
39
+ yaw_std = np.std(poses[:, 0])
40
+ pitch_std = np.std(poses[:, 1])
41
+
42
+ print(f"📈 Écarts-types — Yaw: {yaw_std:.2f}, Pitch: {pitch_std:.2f}")
43
+
44
+ if yaw_std < yaw_thresh and pitch_std < pitch_thresh:
45
+ print("🔴 Visage figé détecté — FAKE")
46
+ return False
47
+ else:
48
+ print("🟢 Mouvement naturel détecté — REAL")
49
+ return True
50
+