Spaces:
Sleeping
Sleeping
Download modeles/liveness_check_folder.py from VisionShield/VISIONSHIELD: direct link, hf CLI and curl.
- Browser
- Download file 1.46 kB
-
https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/liveness_check_folder.py
- Command line
-
hf download hf://spaces/VisionShield/VISIONSHIELD/modeles/liveness_check_folder.py
-
curl -L -o liveness_check_folder.py https://huggingface.co/spaces/VisionShield/VISIONSHIELD/resolve/main/modeles/liveness_check_folder.py
1.46 kB
| import os | |
| import cv2 | |
| import numpy as np | |
| from insightface.app import FaceAnalysis | |
| def check_liveness_on_folder(folder_path, frame_interval=1, yaw_thresh=5, pitch_thresh=5): | |
| model = FaceAnalysis(name='buffalo_l', providers=['CPUExecutionProvider']) | |
| model.prepare(ctx_id=0) | |
| poses = [] | |
| image_files = sorted([ | |
| f for f in os.listdir(folder_path) | |
| if f.lower().endswith((".jpg", ".jpeg", ".png")) | |
| ]) | |
| for idx, filename in enumerate(image_files): | |
| if idx % frame_interval != 0: | |
| continue | |
| img_path = os.path.join(folder_path, filename) | |
| img = cv2.imread(img_path) | |
| if img is None: | |
| print(f"Image illisible : {filename}") | |
| return False | |
| faces = model.get(img) | |
| if not faces: | |
| print(f"Aucun visage détecté sur {filename}") | |
| return False | |
| pose = faces[0].pose # [yaw, pitch, roll] | |
| poses.append(pose) | |
| if len(poses) < 3: | |
| print("Pas assez de données pour vérifier la vivacité.") | |
| return False | |
| poses = np.array(poses) | |
| yaw_std = np.std(poses[:, 0]) | |
| pitch_std = np.std(poses[:, 1]) | |
| print(f"Écarts-types — Yaw: {yaw_std:.2f}, Pitch: {pitch_std:.2f}") | |
| if yaw_std < yaw_thresh and pitch_std < pitch_thresh: | |
| print("Visage figé détecté — FAKE") | |
| return False | |
| else: | |
| print("🟢 Mouvement naturel détecté — REAL") | |
| return True | |